From 06d2ae3ada3bb2c6c12065434c67be5660c6d632 Mon Sep 17 00:00:00 2001 From: Serge Zaitsev Date: Tue, 12 Dec 2023 17:13:47 +0100 Subject: [PATCH] Chore: Move folder sync logic into one-shot migration (#78985) Chore: move folder sync code into one-shot migration --- pkg/services/folder/folderimpl/folder.go | 40 ------------------- .../sqlstore/migrations/folder_mig.go | 20 ++++++++++ 2 files changed, 20 insertions(+), 40 deletions(-) diff --git a/pkg/services/folder/folderimpl/folder.go b/pkg/services/folder/folderimpl/folder.go index fc43492c0fc..ebf82e61ff8 100644 --- a/pkg/services/folder/folderimpl/folder.go +++ b/pkg/services/folder/folderimpl/folder.go @@ -21,8 +21,6 @@ import ( "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/folder" "github.com/grafana/grafana/pkg/services/guardian" - "github.com/grafana/grafana/pkg/services/sqlstore" - "github.com/grafana/grafana/pkg/services/sqlstore/migrator" "github.com/grafana/grafana/pkg/services/store/entity" "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" @@ -70,7 +68,6 @@ func ProvideService( registry: make(map[string]folder.RegistryService), metrics: newFoldersMetrics(r), } - srv.DBMigration(db) ac.RegisterScopeAttributeResolver(dashboards.NewFolderNameScopeResolver(folderStore, srv)) ac.RegisterScopeAttributeResolver(dashboards.NewFolderIDScopeResolver(folderStore, srv)) @@ -78,43 +75,6 @@ func ProvideService( return srv } -func (s *Service) DBMigration(db db.DB) { - ctx := context.Background() - err := db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error { - var err error - if db.GetDialect().DriverName() == migrator.SQLite { - _, err = sess.Exec(` - INSERT INTO folder (uid, org_id, title, created, updated) - SELECT uid, org_id, title, created, updated FROM dashboard WHERE is_folder = 1 - ON CONFLICT DO UPDATE SET title=excluded.title, updated=excluded.updated - `) - } else if db.GetDialect().DriverName() == migrator.Postgres { - _, err = sess.Exec(` - INSERT INTO folder (uid, org_id, title, created, updated) - SELECT uid, org_id, title, created, updated FROM dashboard WHERE is_folder = true - ON CONFLICT(uid, org_id) DO UPDATE SET title=excluded.title, updated=excluded.updated - `) - } else { - _, err = sess.Exec(` - INSERT INTO folder (uid, org_id, title, created, updated) - SELECT * FROM (SELECT uid, org_id, title, created, updated FROM dashboard WHERE is_folder = 1) AS derived - ON DUPLICATE KEY UPDATE title=derived.title, updated=derived.updated - `) - } - if err != nil { - return err - } - _, err = sess.Exec(` - DELETE FROM folder WHERE NOT EXISTS - (SELECT 1 FROM dashboard WHERE dashboard.uid = folder.uid AND dashboard.org_id = folder.org_id AND dashboard.is_folder = true) - `) - return err - }) - if err != nil { - s.log.Error("DB migration on folder service start failed.", "err", err) - } -} - func (s *Service) Get(ctx context.Context, cmd *folder.GetFolderQuery) (*folder.Folder, error) { if cmd.SignedInUser == nil { return nil, folder.ErrBadRequest.Errorf("missing signed in user") diff --git a/pkg/services/sqlstore/migrations/folder_mig.go b/pkg/services/sqlstore/migrations/folder_mig.go index 5d28313df00..f6ddbd5bd51 100644 --- a/pkg/services/sqlstore/migrations/folder_mig.go +++ b/pkg/services/sqlstore/migrations/folder_mig.go @@ -35,6 +35,26 @@ func addFolderMigrations(mg *migrator.Migrator) { Type: migrator.UniqueIndex, Cols: []string{"title", "parent_uid", "org_id"}, })) + + mg.AddMigration("Sync dashboard and folder table", migrator.NewRawSQLMigration(""). + Mysql(` + INSERT INTO folder (uid, org_id, title, created, updated) + SELECT * FROM (SELECT uid, org_id, title, created, updated FROM dashboard WHERE is_folder = 1) AS derived + ON DUPLICATE KEY UPDATE title=derived.title, updated=derived.updated + `).Postgres(` + INSERT INTO folder (uid, org_id, title, created, updated) + SELECT uid, org_id, title, created, updated FROM dashboard WHERE is_folder = true + ON CONFLICT(uid, org_id) DO UPDATE SET title=excluded.title, updated=excluded.updated + `).SQLite(` + INSERT INTO folder (uid, org_id, title, created, updated) + SELECT uid, org_id, title, created, updated FROM dashboard WHERE is_folder = 1 + ON CONFLICT DO UPDATE SET title=excluded.title, updated=excluded.updated + `)) + + mg.AddMigration("Remove ghost folders from the folder table", migrator.NewRawSQLMigration(` + DELETE FROM folder WHERE NOT EXISTS + (SELECT 1 FROM dashboard WHERE dashboard.uid = folder.uid AND dashboard.org_id = folder.org_id AND dashboard.is_folder = true) + `)) } func folderv1() migrator.Table {