From 6f255cb0891e552223beb3f8c2fba47b270f5840 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Mon, 5 Sep 2022 16:29:56 +0200 Subject: [PATCH] LibraryElements: Fix inability to delete library panels under MySQL (#54600) (#54711) Closes #53456 (cherry picked from commit 65c3ad6721622d1c7af0ee526b2745b2141bf6a4) Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> --- pkg/services/libraryelements/database.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/services/libraryelements/database.go b/pkg/services/libraryelements/database.go index b2596650442..a6563ef756e 100644 --- a/pkg/services/libraryelements/database.go +++ b/pkg/services/libraryelements/database.go @@ -28,11 +28,14 @@ SELECT DISTINCT , (SELECT COUNT(connection_id) FROM ` + models.LibraryElementConnectionTableName + ` WHERE element_id = le.id AND kind=1) AS connected_dashboards` ) +// redundant SELECT to trick mysql's optimizer const deleteInvalidConnections = ` DELETE FROM library_element_connection WHERE connection_id IN ( - SELECT connection_id as id FROM library_element_connection - WHERE element_id=? AND connection_id NOT IN (SELECT id as connection_id from dashboard) + SELECT connection_id FROM ( + SELECT connection_id as id FROM library_element_connection + WHERE element_id=? AND connection_id NOT IN (SELECT id as connection_id from dashboard) + ) as dummy )` func getFromLibraryElementDTOWithMeta(dialect migrator.Dialect) string {