sqlstore: add support for checking if error is constraint validation error
This commit is contained in:
@@ -240,31 +240,21 @@ func InsertAlertNotificationState(ctx context.Context, cmd *m.InsertAlertNotific
|
||||
State: cmd.State,
|
||||
}
|
||||
|
||||
_, err := sess.Insert(notificationState)
|
||||
|
||||
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) {
|
||||
if _, err := sess.Insert(notificationState); err != nil {
|
||||
if dialect.IsUniqueConstraintViolation(err) {
|
||||
return m.ErrAlertNotificationStateAlreadyExist
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func SetAlertNotificationStateToCompleteCommand(ctx context.Context, cmd *m.SetAlertNotificationStateToCompleteCommand) error {
|
||||
return withDbSession(ctx, func(sess *DBSession) error {
|
||||
sql := `UPDATE alert_notification_state SET
|
||||
sql := `UPDATE alert_notification_state SET
|
||||
state= ?
|
||||
WHERE
|
||||
id = ?`
|
||||
@@ -286,8 +276,8 @@ func SetAlertNotificationStateToCompleteCommand(ctx context.Context, cmd *m.SetA
|
||||
|
||||
func SetAlertNotificationStateToPendingCommand(ctx context.Context, cmd *m.SetAlertNotificationStateToPendingCommand) error {
|
||||
return withDbSession(ctx, func(sess *DBSession) error {
|
||||
sql := `UPDATE alert_notification_state SET
|
||||
state= ?,
|
||||
sql := `UPDATE alert_notification_state SET
|
||||
state= ?,
|
||||
version = ?
|
||||
WHERE
|
||||
id = ? AND
|
||||
|
||||
@@ -44,6 +44,8 @@ type Dialect interface {
|
||||
|
||||
CleanDB() error
|
||||
NoOpSql() string
|
||||
|
||||
IsUniqueConstraintViolation(err error) bool
|
||||
}
|
||||
|
||||
func NewDialect(engine *xorm.Engine) Dialect {
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/VividCortex/mysqlerr"
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
@@ -125,3 +127,13 @@ func (db *Mysql) CleanDB() error {
|
||||
|
||||
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 (
|
||||
"strings"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
type Postgres struct {
|
||||
@@ -136,3 +137,13 @@ func (db *Postgres) CleanDB() error {
|
||||
|
||||
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 (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
sqlite3 "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type Sqlite3 struct {
|
||||
@@ -82,3 +83,13 @@ func (db *Sqlite3) DropIndexSql(tableName string, index *Index) string {
|
||||
func (db *Sqlite3) CleanDB() error {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user