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

This commit is contained in:
Marcus Efraimsson
2018-09-27 13:38:22 +02:00
parent c5278af6c4
commit d093244282
9 changed files with 1156 additions and 19 deletions
+8 -18
View File
@@ -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