summaryrefslogtreecommitdiff
path: root/model/database.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-05-09 22:04:06 +0200
committerxengineering <me@xengineering.eu>2024-05-09 22:04:06 +0200
commit0e8a199d6144fcfab05eb0c8a793073bc53783be (patch)
treef33ee4e967e14cc17728f9a9ccc23e3f43f3fac7 /model/database.go
parente5d57f2c4a9f01ac7a2b11c04b932311bd240611 (diff)
downloadceres-0e8a199d6144fcfab05eb0c8a793073bc53783be.tar
ceres-0e8a199d6144fcfab05eb0c8a793073bc53783be.tar.zst
ceres-0e8a199d6144fcfab05eb0c8a793073bc53783be.zip
model: Require same version for executable and DB
Currently only an empty database and an existing database with the same version are supported. Support for migrations based on semantic versioning will be added in future versions of Ceres.
Diffstat (limited to 'model/database.go')
-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 {