From 46949508a4eb091aca5a37cf24cd1ad8db96cd47 Mon Sep 17 00:00:00 2001 From: Oscar Zhou <100548325+oscarzhou-portainer@users.noreply.github.com> Date: Mon, 18 Sep 2023 14:32:57 +1200 Subject: [PATCH] fix(db/migration): avoid fatal error from being overwritten (#10317) --- api/cmd/portainer/main.go | 6 +++--- api/datastore/migrate_data.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/api/cmd/portainer/main.go b/api/cmd/portainer/main.go index 73d0c8e17..b4f954298 100644 --- a/api/cmd/portainer/main.go +++ b/api/cmd/portainer/main.go @@ -158,13 +158,13 @@ func initDataStore(flags *portainer.CLIFlags, secretKey []byte, fileService port } // checkDBSchemaServerVersionMatch checks if the server version matches the db scehma version -func checkDBSchemaServerVersionMatch(dbStore dataservices.DataStore, serverVersion string) bool { +func checkDBSchemaServerVersionMatch(dbStore dataservices.DataStore, serverVersion string, serverEdition int) bool { v, err := dbStore.Version().Version() if err != nil { return false } - return v.SchemaVersion == serverVersion && v.Edition == int(portainer.Edition) + return v.SchemaVersion == serverVersion && v.Edition == serverEdition } func initComposeStackManager(composeDeployer libstack.Deployer, proxyManager *proxy.Manager) portainer.ComposeStackManager { @@ -399,7 +399,7 @@ func buildServer(flags *portainer.CLIFlags) portainer.Server { } // check if the db schema version matches with server version - if !checkDBSchemaServerVersionMatch(dataStore, portainer.APIVersion) { + if !checkDBSchemaServerVersionMatch(dataStore, portainer.APIVersion, int(portainer.Edition)) { log.Fatal().Msg("The database schema version does not align with the server version. Please consider reverting to the previous server version or addressing the database migration issue.") } diff --git a/api/datastore/migrate_data.go b/api/datastore/migrate_data.go index f5ec42f72..7c424a9e4 100644 --- a/api/datastore/migrate_data.go +++ b/api/datastore/migrate_data.go @@ -51,9 +51,9 @@ func (store *Store) MigrateData() error { err = errors.Wrap(err, "failed to migrate database") log.Warn().Err(err).Msg("migration failed, restoring database to previous version") - err = store.restoreWithOptions(&BackupOptions{BackupPath: backupPath}) - if err != nil { - return errors.Wrap(err, "failed to restore database") + restorErr := store.restoreWithOptions(&BackupOptions{BackupPath: backupPath}) + if restorErr != nil { + return errors.Wrap(restorErr, "failed to restore database") } log.Info().Msg("database restored to previous version")