summaryrefslogtreecommitdiff
path: root/model
diff options
context:
space:
mode:
Diffstat (limited to 'model')
-rw-r--r--model/database.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/model/database.go b/model/database.go
index 8e002d6..b6e64bb 100644
--- a/model/database.go
+++ b/model/database.go
@@ -93,6 +93,26 @@ VALUES
return err
}
+func getDatabaseVersion(tx *sql.Tx) string {
+ rows, err := tx.Query(`SELECT value FROM metadata WHERE key='version';`)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer rows.Close()
+
+ if !rows.Next() {
+ log.Fatalf("No rows on request of database version")
+ }
+
+ var version string
+ err = rows.Scan(&version)
+ if err != nil {
+ log.Fatalf("Failed to scan database version to string")
+ }
+
+ return version
+}
+
func MigrateDatabase(version string) {
err := Transaction(func(tx *sql.Tx) error {
if isDatabaseEmpty(tx) {
@@ -113,6 +133,15 @@ func MigrateDatabase(version string) {
}
}
+ dbVersion := getDatabaseVersion(tx)
+ if dbVersion != version {
+ log.Fatalf(
+ "Database version '%s' does not match executable version '%s'",
+ dbVersion,
+ version,
+ )
+ }
+
return nil
})
if err != nil {