From 95bc508462346cc6397244f5527f68551e442567 Mon Sep 17 00:00:00 2001 From: Matt Hook Date: Mon, 28 Nov 2022 19:28:10 +1300 Subject: [PATCH] fix(migrator): fix setting version struct fields after migration [EE-4613] (#8090) * fix setting version struct fields * fix go tests * remove versionUpdateRequired * remove old code that was originally for debugging purposes --- api/cmd/portainer/main.go | 10 ----- api/datastore/migrator/migrate_ce.go | 40 +++++++++---------- .../test_data/output_24_to_latest.json | 2 +- 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/api/cmd/portainer/main.go b/api/cmd/portainer/main.go index cc0ba59c4..66e01edbf 100644 --- a/api/cmd/portainer/main.go +++ b/api/cmd/portainer/main.go @@ -8,7 +8,6 @@ import ( "path" "strconv" "strings" - "time" "github.com/portainer/libhelm" portainer "github.com/portainer/portainer/api" @@ -142,15 +141,6 @@ func initDataStore(flags *portainer.CLIFlags, secretKey []byte, fileService port go func() { <-shutdownCtx.Done() defer connection.Close() - - exportFilename := path.Join(*flags.Data, fmt.Sprintf("export-%d.json", time.Now().Unix())) - - err := store.Export(exportFilename) - if err != nil { - log.Error().Str("filename", exportFilename).Err(err).Msg("failed to export") - } else { - log.Debug().Str("filename", exportFilename).Msg("exported") - } }() return store diff --git a/api/datastore/migrator/migrate_ce.go b/api/datastore/migrator/migrate_ce.go index 54d22d5e3..f6b85e67c 100644 --- a/api/datastore/migrator/migrate_ce.go +++ b/api/datastore/migrator/migrate_ce.go @@ -32,15 +32,13 @@ func (m *Migrator) Migrate() error { } newMigratorCount := 0 - versionUpdateRequired := false - if schemaVersion.Equal(semver.MustParse(portainer.APIVersion)) { + apiVersion := semver.MustParse(portainer.APIVersion) + if schemaVersion.Equal(apiVersion) { // detect and run migrations when the versions are the same. // e.g. development builds latestMigrations := m.latestMigrations() if latestMigrations.version.Equal(schemaVersion) && version.MigratorCount != len(latestMigrations.migrationFuncs) { - - versionUpdateRequired = true err := runMigrations(latestMigrations.migrationFuncs) if err != nil { return err @@ -51,7 +49,7 @@ func (m *Migrator) Migrate() error { // regular path when major/minor/patch versions differ for _, migration := range m.migrations { if schemaVersion.LessThan(migration.version) { - versionUpdateRequired = true + log.Info().Msgf("migrating data to %s", migration.version.String()) err := runMigrations(migration.migrationFuncs) if err != nil { @@ -59,27 +57,27 @@ func (m *Migrator) Migrate() error { } } - newMigratorCount = len(migration.migrationFuncs) + if apiVersion.Equal(migration.version) { + newMigratorCount = len(migration.migrationFuncs) + } } } - if versionUpdateRequired || newMigratorCount != version.MigratorCount { - err := m.Always() - if err != nil { - return migrationError(err, "Always migrations returned error") - } - - version.SchemaVersion = portainer.APIVersion - version.MigratorCount = newMigratorCount - - err = m.versionService.UpdateVersion(version) - if err != nil { - return migrationError(err, "StoreDBVersion") - } - - log.Info().Msgf("db migrated to %s", portainer.APIVersion) + err = m.Always() + if err != nil { + return migrationError(err, "Always migrations returned error") } + version.SchemaVersion = portainer.APIVersion + version.MigratorCount = newMigratorCount + + err = m.versionService.UpdateVersion(version) + if err != nil { + return migrationError(err, "StoreDBVersion") + } + + log.Info().Msgf("db migrated to %s", portainer.APIVersion) + return nil } diff --git a/api/datastore/test_data/output_24_to_latest.json b/api/datastore/test_data/output_24_to_latest.json index 0789b6002..96b0daa75 100644 --- a/api/datastore/test_data/output_24_to_latest.json +++ b/api/datastore/test_data/output_24_to_latest.json @@ -930,6 +930,6 @@ } ], "version": { - "VERSION": "{\"SchemaVersion\":\"2.17.0\",\"MigratorCount\":1,\"Edition\":1,\"InstanceID\":\"463d5c47-0ea5-4aca-85b1-405ceefee254\"}" + "VERSION": "{\"SchemaVersion\":\"2.17.0\",\"MigratorCount\":0,\"Edition\":1,\"InstanceID\":\"463d5c47-0ea5-4aca-85b1-405ceefee254\"}" } } \ No newline at end of file