Team/User: UID migrations (#82298)

* Add user uid migration to run on every startup to protect against empty values in a upgrade downgrade scenario

* Add team uid migration to run on every startup to protect against empty values in a upgrade downgrade scenario

* Run team uid migration
This commit is contained in:
Karl Persson
2024-02-12 14:48:29 +01:00
committed by GitHub
parent 685e84b1f8
commit 1315c67c8b
12 changed files with 84 additions and 16 deletions
+27
View File
@@ -14,6 +14,7 @@ import (
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/quota"
"github.com/grafana/grafana/pkg/services/serviceaccounts"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/services/supportbundles"
"github.com/grafana/grafana/pkg/services/team"
"github.com/grafana/grafana/pkg/services/user"
@@ -60,6 +61,10 @@ func ProvideService(
return s, err
}
if err := s.uidMigration(db); err != nil {
return nil, err
}
bundleRegistry.RegisterSupportItemCollector(s.supportBundleCollector())
return s, nil
}
@@ -485,3 +490,25 @@ func (s *Service) supportBundleCollector() supportbundles.Collector {
Fn: collectorFn,
}
}
// This is just to ensure that all users have a valid uid.
// To protect against upgrade / downgrade we need to run this for a couple of releases.
// FIXME: Remove this migration and make uid field required https://github.com/grafana/identity-access-team/issues/552
func (s *Service) uidMigration(store db.DB) error {
return store.WithDbSession(context.Background(), func(sess *db.Session) error {
switch store.GetDBType() {
case migrator.SQLite:
_, err := sess.Exec("UPDATE user SET uid=printf('u%09d',id) WHERE uid IS NULL;")
return err
case migrator.Postgres:
_, err := sess.Exec("UPDATE `user` SET uid='u' || lpad('' || id::text,9,'0') WHERE uid IS NULL;")
return err
case migrator.MySQL:
_, err := sess.Exec("UPDATE user SET uid=concat('u',lpad(id,9,'0')) WHERE uid IS NULL;")
return err
default:
// this branch should be unreachable
return nil
}
})
}