Commit 46bab0bb by Torkel Ödegaard

feat(alerting): remove annotations when deleting alert, closes #6244

parent c9d7eb0a
...@@ -46,13 +46,23 @@ func GetAllAlertQueryHandler(query *m.GetAllAlertsQuery) error { ...@@ -46,13 +46,23 @@ func GetAllAlertQueryHandler(query *m.GetAllAlertsQuery) error {
return nil 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 { func DeleteAlertById(cmd *m.DeleteAlertCommand) error {
return inTransaction(func(sess *xorm.Session) error { return inTransaction(func(sess *xorm.Session) error {
if _, err := sess.Exec("DELETE FROM alert WHERE id = ?", cmd.AlertId); err != nil { return deleteAlertByIdInternal(cmd.AlertId, "DeleteAlertCommand", sess)
return err
}
return nil
}) })
} }
...@@ -110,12 +120,7 @@ func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error { ...@@ -110,12 +120,7 @@ func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error {
sess.Where("dashboard_id = ?", dashboardId).Find(&alerts) sess.Where("dashboard_id = ?", dashboardId).Find(&alerts)
for _, alert := range alerts { for _, alert := range alerts {
_, err := sess.Exec("DELETE FROM alert WHERE id = ? ", alert.Id) deleteAlertByIdInternal(alert.Id, "Dashboard deleted", sess)
if err != nil {
return err
}
sqlog.Debug("Alert deleted (due to dashboard deletion)", "name", alert.Name, "id", alert.Id)
} }
return nil return nil
...@@ -195,12 +200,7 @@ func deleteMissingAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm ...@@ -195,12 +200,7 @@ func deleteMissingAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm
} }
if missing { if missing {
_, err := sess.Exec("DELETE FROM alert WHERE id = ?", missingAlert.Id) deleteAlertByIdInternal(missingAlert.Id, "Removed from dashboard", sess)
if err != nil {
return err
}
sqlog.Debug("Alert deleted", "name", missingAlert.Name, "id", missingAlert.Id)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment