Browse Source

cleanup alert_notification_state when deleting alert rules and channels

Marcus Efraimsson 7 years ago
parent
commit
1be8fb76b8
2 changed files with 13 additions and 2 deletions
  1. 4 0
      pkg/services/sqlstore/alert.go
  2. 9 2
      pkg/services/sqlstore/alert_notification.go

+ 4 - 0
pkg/services/sqlstore/alert.go

@@ -60,6 +60,10 @@ func deleteAlertByIdInternal(alertId int64, reason string, sess *DBSession) erro
 		return err
 	}
 
+	if _, err := sess.Exec("DELETE FROM alert_notification_state WHERE alert_id = ?", alertId); err != nil {
+		return err
+	}
+
 	return nil
 }
 

+ 9 - 2
pkg/services/sqlstore/alert_notification.go

@@ -27,8 +27,15 @@ func init() {
 func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error {
 	return inTransaction(func(sess *DBSession) error {
 		sql := "DELETE FROM alert_notification WHERE alert_notification.org_id = ? AND alert_notification.id = ?"
-		_, err := sess.Exec(sql, cmd.OrgId, cmd.Id)
-		return err
+		if _, err := sess.Exec(sql, cmd.OrgId, cmd.Id); err != nil {
+			return err
+		}
+
+		if _, err := sess.Exec("DELETE FROM alert_notification_state WHERE alert_notification_state.org_id = ? AND alert_notification_state.notifier_id = ?", cmd.OrgId, cmd.Id); err != nil {
+			return err
+		}
+
+		return nil
 	})
 }