diff --git a/pkg/services/sqlstore/alert.go b/pkg/services/sqlstore/alert.go index 76ea268bc04..b755177d2a3 100644 --- a/pkg/services/sqlstore/alert.go +++ b/pkg/services/sqlstore/alert.go @@ -46,13 +46,23 @@ func GetAllAlertQueryHandler(query *m.GetAllAlertsQuery) error { return nil } +func deleteAlertByIdInternal(alertId int64, reason string, sess *xorm.Session) error { + sqlog.Debug("Deleting alert", "id", alertId, "reason", reason) + + if _, err := sess.Exec("DELETE FROM alert WHERE id = ?", alertId); err != nil { + return err + } + + if _, err := sess.Exec("DELETE FROM annotation WHERE alert_id = ?", alertId); err != nil { + return err + } + + return nil +} + func DeleteAlertById(cmd *m.DeleteAlertCommand) error { return inTransaction(func(sess *xorm.Session) error { - if _, err := sess.Exec("DELETE FROM alert WHERE id = ?", cmd.AlertId); err != nil { - return err - } - - return nil + return deleteAlertByIdInternal(cmd.AlertId, "DeleteAlertCommand", sess) }) } @@ -110,12 +120,7 @@ func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error { sess.Where("dashboard_id = ?", dashboardId).Find(&alerts) for _, alert := range alerts { - _, err := sess.Exec("DELETE FROM alert WHERE id = ? ", alert.Id) - if err != nil { - return err - } - - sqlog.Debug("Alert deleted (due to dashboard deletion)", "name", alert.Name, "id", alert.Id) + deleteAlertByIdInternal(alert.Id, "Dashboard deleted", sess) } return nil @@ -195,12 +200,7 @@ func deleteMissingAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm } if missing { - _, err := sess.Exec("DELETE FROM alert WHERE id = ?", missingAlert.Id) - if err != nil { - return err - } - - sqlog.Debug("Alert deleted", "name", missingAlert.Name, "id", missingAlert.Id) + deleteAlertByIdInternal(missingAlert.Id, "Removed from dashboard", sess) } }