diff --git a/pkg/migration/manager.go b/pkg/migration/manager.go index 57f357d9aaaa..cc129feb3c00 100644 --- a/pkg/migration/manager.go +++ b/pkg/migration/manager.go @@ -179,12 +179,19 @@ func (m *Manager) Migrate(ctx context.Context, from, to clusterversion.ClusterVe } } - // TODO(irfansharif): We'll want to retrieve the right migration off of - // our registry of migrations, if any, and execute it. // TODO(irfansharif): We'll want to be able to override which migration // is retrieved here within tests. We could make the registry be a part // of the manager, and all tests to provide their own. - _ = Registry[clusterVersion] + + // Finally, run the actual migration. + migration, ok := registry[clusterVersion] + if !ok { + log.Infof(ctx, "no migration registered for %s, skipping", clusterVersion) + continue + } + if err := migration.Run(ctx, h); err != nil { + return err + } } return nil diff --git a/pkg/migration/migrations.go b/pkg/migration/migrations.go index d664e93cb61a..9382ef0abe79 100644 --- a/pkg/migration/migrations.go +++ b/pkg/migration/migrations.go @@ -16,16 +16,15 @@ import ( "github.com/cockroachdb/cockroach/pkg/clusterversion" ) -// Registry defines the global mapping between a cluster version, and the +// registry defines the global mapping between a cluster version and the // associated migration. The migration is only executed after a cluster-wide -// bump of the version gate. -var Registry = make(map[clusterversion.ClusterVersion]Migration) +// bump of the corresponding version gate. +var registry = make(map[clusterversion.ClusterVersion]Migration) func init() { // TODO(irfansharif): We'll want to register individual migrations with // specific internal cluster versions here. - // - // Registry[clusterversion.ByKey(clusterversion.VersionWhatever)] = WhateverMigration + _ = register // register(clusterversion.WhateverMigration, WhateverMigration, "whatever migration") } // Migration defines a program to be executed once every node in the cluster is diff --git a/pkg/migration/util.go b/pkg/migration/util.go index b9c64405df20..b7ab5ed6c13e 100644 --- a/pkg/migration/util.go +++ b/pkg/migration/util.go @@ -64,3 +64,13 @@ func fenceVersionFor( fenceCV.Internal-- return fenceCV } + +// register is a short hand to register a given migration within the global +// registry. +func register(key clusterversion.Key, fn migrationFn, desc string) { + cv := clusterversion.ClusterVersion{Version: clusterversion.ByKey(key)} + if _, ok := registry[cv]; ok { + log.Fatalf(context.Background(), "doubly registering migration for %s", cv) + } + registry[cv] = Migration{cv: cv, fn: fn, desc: desc} +}