mirror of
https://github.com/grafana/grafana.git
synced 2025-01-09 23:53:25 -06:00
Chore: Move folder sync logic into one-shot migration (#78985)
Chore: move folder sync code into one-shot migration
This commit is contained in:
parent
bc8ad7b6e5
commit
06d2ae3ada
@ -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")
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user