diff --git a/pkg/services/sqlstore/migrations/ualert/ualert.go b/pkg/services/sqlstore/migrations/ualert/ualert.go index 697b177ea0e..80fc01b4762 100644 --- a/pkg/services/sqlstore/migrations/ualert/ualert.go +++ b/pkg/services/sqlstore/migrations/ualert/ualert.go @@ -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) diff --git a/pkg/services/sqlstore/migrator/migrator.go b/pkg/services/sqlstore/migrator/migrator.go index b40d7e0468b..deb1780030e 100644 --- a/pkg/services/sqlstore/migrator/migrator.go +++ b/pkg/services/sqlstore/migrator/migrator.go @@ -103,7 +103,7 @@ func (mg *Migrator) Start() error { Timestamp: time.Now(), } - err := mg.inTransaction(func(sess *xorm.Session) error { + err := mg.InTransaction(func(sess *xorm.Session) error { err := mg.exec(m, sess) if err != nil { mg.Logger.Error("Exec failed", "error", err, "sql", sql) @@ -183,7 +183,7 @@ func (mg *Migrator) ClearMigrationEntry(id string) error { type dbTransactionFunc func(sess *xorm.Session) error -func (mg *Migrator) inTransaction(callback dbTransactionFunc) error { +func (mg *Migrator) InTransaction(callback dbTransactionFunc) error { sess := mg.x.NewSession() defer sess.Close()