From 423331dae03e9abe33c6656d72d6185dd5a9bd06 Mon Sep 17 00:00:00 2001 From: bergquist Date: Mon, 5 Nov 2018 14:24:08 +0100 Subject: [PATCH] alerting: delete alerts when parent folder is deleted closes #13322 --- pkg/services/sqlstore/dashboard.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/pkg/services/sqlstore/dashboard.go b/pkg/services/sqlstore/dashboard.go index 1b853d17b5f97..bad46c10af495 100644 --- a/pkg/services/sqlstore/dashboard.go +++ b/pkg/services/sqlstore/dashboard.go @@ -327,20 +327,34 @@ func DeleteDashboard(cmd *m.DeleteDashboardCommand) error { if dashboard.IsFolder { deletes = append(deletes, "DELETE FROM dashboard_provisioning WHERE dashboard_id in (select id from dashboard where folder_id = ?)") deletes = append(deletes, "DELETE FROM dashboard WHERE folder_id = ?") - } - - for _, sql := range deletes { - _, err := sess.Exec(sql, dashboard.Id) + dashIds := []struct { + Id int64 + }{} + err := sess.SQL("select id from dashboard where folder_id = ?", dashboard.Id).Find(&dashIds) if err != nil { return err } + + for _, id := range dashIds { + if err := deleteAlertDefinition(id.Id, sess); err != nil { + return nil + } + } } if err := deleteAlertDefinition(dashboard.Id, sess); err != nil { return nil } + for _, sql := range deletes { + _, err := sess.Exec(sql, dashboard.Id) + + if err != nil { + return err + } + } + return nil }) }