From 122fd835dcc8bf737b59a16af72ea503b251f6d1 Mon Sep 17 00:00:00 2001 From: Oscar Zhou <100548325+oscarzhou-portainer@users.noreply.github.com> Date: Tue, 12 Sep 2023 15:55:15 +1200 Subject: [PATCH] fix(db/init): check server version and db schema version (#10299) --- api/cmd/portainer/main.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/api/cmd/portainer/main.go b/api/cmd/portainer/main.go index 854635878..73d0c8e17 100644 --- a/api/cmd/portainer/main.go +++ b/api/cmd/portainer/main.go @@ -157,6 +157,16 @@ func initDataStore(flags *portainer.CLIFlags, secretKey []byte, fileService port return store } +// checkDBSchemaServerVersionMatch checks if the server version matches the db scehma version +func checkDBSchemaServerVersionMatch(dbStore dataservices.DataStore, serverVersion string) bool { + v, err := dbStore.Version().Version() + if err != nil { + return false + } + + return v.SchemaVersion == serverVersion && v.Edition == int(portainer.Edition) +} + func initComposeStackManager(composeDeployer libstack.Deployer, proxyManager *proxy.Manager) portainer.ComposeStackManager { composeWrapper, err := exec.NewComposeStackManager(composeDeployer, proxyManager) if err != nil { @@ -388,6 +398,11 @@ func buildServer(flags *portainer.CLIFlags) portainer.Server { log.Fatal().Err(err).Msg("") } + // check if the db schema version matches with server version + if !checkDBSchemaServerVersionMatch(dataStore, portainer.APIVersion) { + 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.") + } + instanceID, err := dataStore.Version().InstanceID() if err != nil { log.Fatal().Err(err).Msg("failed getting instance id")