diff --git a/alert/store.go b/alert/store.go index 416878843d..92d19960e2 100644 --- a/alert/store.go +++ b/alert/store.go @@ -470,7 +470,7 @@ func (s *Store) UpdateManyAlertStatus(ctx context.Context, status Status, alertI return updatedIDs, nil } -func (s *Store) Create(ctx context.Context, a *Alert) (*Alert, error) { +func (s *Store) CreateTx(ctx context.Context, tx *sql.Tx, a *Alert) (*Alert, error) { n, err := a.Normalize() // validation if err != nil { return nil, err @@ -489,12 +489,6 @@ func (s *Store) Create(ctx context.Context, a *Alert) (*Alert, error) { return nil, err } - tx, err := s.db.BeginTx(ctx, nil) - if err != nil { - return nil, err - } - defer sqlutil.Rollback(ctx, "alert: create", tx) - _, err = tx.StmtContext(ctx, s.lockSvc).ExecContext(ctx, n.ServiceID) if err != nil { return nil, err @@ -507,11 +501,6 @@ func (s *Store) Create(ctx context.Context, a *Alert) (*Alert, error) { s.logDB.MustLogTx(ctx, tx, n.ID, alertlog.TypeCreated, meta) - err = tx.Commit() - if err != nil { - return nil, err - } - ctx = log.WithFields(ctx, log.Fields{"AlertID": n.ID, "ServiceID": n.ServiceID}) log.Logf(ctx, "Alert created.") metricCreatedTotal.WithLabelValues(n.ServiceID).Inc() diff --git a/graphql2/graphqlapp/alert.go b/graphql2/graphqlapp/alert.go index 609bb6b77b..d2f688850c 100644 --- a/graphql2/graphqlapp/alert.go +++ b/graphql2/graphqlapp/alert.go @@ -403,7 +403,17 @@ func (m *Mutation) CreateAlert(ctx context.Context, input graphql2.CreateAlertIn a.Dedup = alert.NewUserDedup(*input.Dedup) } - return m.AlertStore.Create(ctx, a) + var newAlert *alert.Alert + err := withContextTx(ctx, m.DB, func(ctx context.Context, tx *sql.Tx) error { + var err error + newAlert, err = m.AlertStore.CreateTx(ctx, tx, a) + return err + }) + if err != nil { + return nil, err + } + + return newAlert, nil } func (a *Alert) NoiseReason(ctx context.Context, raw *alert.Alert) (*string, error) {