Alerting: fix postgres migration on conflict (#35556) (#35653)

(cherry picked from commit 423474325a)

Co-authored-by: Sofia Papagiannaki <papagian@users.noreply.github.com>
This commit is contained in:
Grot (@grafanabot)
2021-06-14 18:09:27 +03:00
committed by GitHub
co-authored by Sofia Papagiannaki
parent 8ac34f9c76
commit 732f794e16
2 changed files with 11 additions and 3 deletions
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
pb "github.com/prometheus/alertmanager/silence/silencepb"
"xorm.io/xorm"
@@ -217,7 +218,14 @@ func (m *migration) Exec(sess *xorm.Session, mg *migrator.Migrator) error {
return err
}
_, err = m.sess.Insert(rule)
if strings.HasPrefix(mg.Dialect.DriverName(), migrator.Postgres) {
err = mg.InTransaction(func(sess *xorm.Session) error {
_, err = sess.Insert(rule)
return err
})
} else {
_, err = m.sess.Insert(rule)
}
if err != nil {
// TODO better error handling, if constraint
rule.Title += fmt.Sprintf(" %v", rule.Uid)