Commit d0932442 by Marcus Efraimsson

sqlstore: add support for checking if error is constraint validation error

parent c5278af6
...@@ -20,6 +20,12 @@ ...@@ -20,6 +20,12 @@
revision = "7677a1d7c1137cd3dd5ba7a076d0c898a1ef4520" revision = "7677a1d7c1137cd3dd5ba7a076d0c898a1ef4520"
[[projects]] [[projects]]
branch = "master"
name = "github.com/VividCortex/mysqlerr"
packages = ["."]
revision = "6c6b55f8796f578c870b7e19bafb16103bc40095"
[[projects]]
name = "github.com/aws/aws-sdk-go" name = "github.com/aws/aws-sdk-go"
packages = [ packages = [
"aws", "aws",
...@@ -673,6 +679,6 @@ ...@@ -673,6 +679,6 @@
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "81a37e747b875cf870c1b9486fa3147e704dea7db8ba86f7cb942d3ddc01d3e3" inputs-digest = "6e9458f912a5f0eb3430b968f1b4dbc4e3b7671b282cf4fe1573419a6d9ba0d4"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1
...@@ -203,3 +203,7 @@ ignored = [ ...@@ -203,3 +203,7 @@ ignored = [
[[constraint]] [[constraint]]
name = "github.com/denisenkom/go-mssqldb" name = "github.com/denisenkom/go-mssqldb"
revision = "270bc3860bb94dd3a3ffd047377d746c5e276726" revision = "270bc3860bb94dd3a3ffd047377d746c5e276726"
[[constraint]]
name = "github.com/VividCortex/mysqlerr"
branch = "master"
...@@ -240,25 +240,15 @@ func InsertAlertNotificationState(ctx context.Context, cmd *m.InsertAlertNotific ...@@ -240,25 +240,15 @@ func InsertAlertNotificationState(ctx context.Context, cmd *m.InsertAlertNotific
State: cmd.State, State: cmd.State,
} }
_, err := sess.Insert(notificationState) if _, err := sess.Insert(notificationState); err != nil {
if dialect.IsUniqueConstraintViolation(err) {
if err == nil {
return nil
}
uniqenessIndexFailureCodes := []string{
"UNIQUE constraint failed",
"pq: duplicate key value violates unique constraint",
"Error 1062: Duplicate entry ",
}
for _, code := range uniqenessIndexFailureCodes {
if strings.HasPrefix(err.Error(), code) {
return m.ErrAlertNotificationStateAlreadyExist return m.ErrAlertNotificationStateAlreadyExist
} }
}
return err return err
}
return nil
}) })
} }
......
...@@ -44,6 +44,8 @@ type Dialect interface { ...@@ -44,6 +44,8 @@ type Dialect interface {
CleanDB() error CleanDB() error
NoOpSql() string NoOpSql() string
IsUniqueConstraintViolation(err error) bool
} }
func NewDialect(engine *xorm.Engine) Dialect { func NewDialect(engine *xorm.Engine) Dialect {
......
...@@ -5,6 +5,8 @@ import ( ...@@ -5,6 +5,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/VividCortex/mysqlerr"
"github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
) )
...@@ -125,3 +127,13 @@ func (db *Mysql) CleanDB() error { ...@@ -125,3 +127,13 @@ func (db *Mysql) CleanDB() error {
return nil return nil
} }
func (db *Mysql) IsUniqueConstraintViolation(err error) bool {
if driverErr, ok := err.(*mysql.MySQLError); ok {
if driverErr.Number == mysqlerr.ER_DUP_ENTRY {
return true
}
}
return false
}
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"strings" "strings"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
"github.com/lib/pq"
) )
type Postgres struct { type Postgres struct {
...@@ -136,3 +137,13 @@ func (db *Postgres) CleanDB() error { ...@@ -136,3 +137,13 @@ func (db *Postgres) CleanDB() error {
return nil return nil
} }
func (db *Postgres) IsUniqueConstraintViolation(err error) bool {
if driverErr, ok := err.(*pq.Error); ok {
if driverErr.Code == "23505" {
return true
}
}
return false
}
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
sqlite3 "github.com/mattn/go-sqlite3"
) )
type Sqlite3 struct { type Sqlite3 struct {
...@@ -82,3 +83,13 @@ func (db *Sqlite3) DropIndexSql(tableName string, index *Index) string { ...@@ -82,3 +83,13 @@ func (db *Sqlite3) DropIndexSql(tableName string, index *Index) string {
func (db *Sqlite3) CleanDB() error { func (db *Sqlite3) CleanDB() error {
return nil return nil
} }
func (db *Sqlite3) IsUniqueConstraintViolation(err error) bool {
if driverErr, ok := err.(sqlite3.Error); ok {
if driverErr.ExtendedCode == sqlite3.ErrConstraintUnique {
return true
}
}
return false
}
MIT License
Copyright (c) 2017 VividCortex
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment