Skip to content
Snippets Groups Projects
Commit 713f302f authored by Gabriel Zachmann's avatar Gabriel Zachmann
Browse files

fix db migration version check

parent c1ddd771
No related branches found
No related tags found
No related merge requests found
......@@ -48,7 +48,7 @@ func (state DBVersionState) Swap(i, j int) { state[i], state[j] = state[j], stat
// Less checks if a version is less than another
func (state DBVersionState) Less(i, j int) bool {
a, b := state[i].Version, state[j].Version
return semver.Compare(a, b) < 0
return semver.Compare("v"+a, "v"+b) < 0
}
// Sort sorts this DBVersionState by the version
......@@ -57,19 +57,20 @@ func (state DBVersionState) Sort() {
}
// dbHasAllVersions checks that the database is compatible with the current version; assumes that DBVersionState is ordered
func (state DBVersionState) dBHasAllVersions() bool {
func (state DBVersionState) dBHasAllVersions() (hasAllVersions bool, missingVersions []string) {
for v, cmds := range dbmigrate.Migrate {
if !state.dBHasVersion(v, cmds) {
return false
missingVersions = append(missingVersions, v)
}
}
return true
hasAllVersions = len(missingVersions) == 0
return
}
// dbHasVersion checks that the database is compatible with the passed version; assumes that DBVersionState is ordered
func (state DBVersionState) dBHasVersion(v string, cmds dbmigrate.Commands) bool {
i := sort.Search(len(state), func(i int) bool {
return semver.Compare(state[i].Version, v) >= 0
return semver.Compare("v"+state[i].Version, "v"+v) >= 0
})
if i >= len(state) || state[i].Version != v { // we have to check that i really points to v
return false
......@@ -103,7 +104,7 @@ func ConnectToVersion() {
if err != nil {
log.WithError(err).Fatal()
}
if !state.dBHasAllVersions() {
log.WithField("version", version.VERSION()).Fatal("database schema not updated to this server version")
if hasAllVersions, missingVersions := state.dBHasAllVersions(); !hasAllVersions {
log.WithField("server_version", version.VERSION()).WithField("missing_versions_in_db", missingVersions).Fatal("database schema not updated to this server version")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment