From 9a552f53027f526df9e8ad43ec218708d5e3445c Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 7 Nov 2022 21:11:10 +0100 Subject: [PATCH] fix(server): remove NOT NULL constraint on branch (fixes #289) --- src/db/db.v | 4 ++- .../migrations/004-nullable-branch/down.sql | 26 +++++++++++++++++++ src/db/migrations/004-nullable-branch/up.sql | 23 ++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/db/migrations/004-nullable-branch/down.sql create mode 100644 src/db/migrations/004-nullable-branch/up.sql diff --git a/src/db/db.v b/src/db/db.v index b8b861a..6d9ab43 100644 --- a/src/db/db.v +++ b/src/db/db.v @@ -17,11 +17,13 @@ const ( $embed_file('migrations/001-initial/up.sql'), $embed_file('migrations/002-rename-to-targets/up.sql'), $embed_file('migrations/003-target-url-type/up.sql'), + $embed_file('migrations/004-nullable-branch/up.sql') ] migrations_down = [ $embed_file('migrations/001-initial/down.sql'), $embed_file('migrations/002-rename-to-targets/down.sql'), $embed_file('migrations/003-target-url-type/down.sql'), + $embed_file('migrations/004-nullable-branch/down.sql') ] ) @@ -60,7 +62,7 @@ pub fn init(db_path string) !VieterDb { res := conn.exec_none(part) if res != sqlite.sqlite_done { - return error('An error occurred while applying migration $version_num') + return error('An error occurred while applying migration $version_num: SQLite error code $res') } } diff --git a/src/db/migrations/004-nullable-branch/down.sql b/src/db/migrations/004-nullable-branch/down.sql new file mode 100644 index 0000000..2515593 --- /dev/null +++ b/src/db/migrations/004-nullable-branch/down.sql @@ -0,0 +1,26 @@ +-- This down won't really work because it'll throw NOT NULL errors, but I'm +-- just putting it here for future reference (still not sure whether I'm even + -- gonna use these) +PRAGMA foreign_keys=off; + +BEGIN TRANSACTION; + +ALTER TABLE Target RENAME TO _Target_old; + +CREATE TABLE Target ( + id INTEGER PRIMARY KEY, + url TEXT NOT NULL, + branch TEXT NOT NULL, + repo TEXT NOT NULL, + schedule TEXT, + kind TEXT NOT NULL DEFAULT 'git' +); + +INSERT INTO Target (id, url, branch, repo, schedule, kind) + SELECT id, url, branch, repo, schedule, kind FROM _Target_old; + +DROP TABLE _Target_old; + +COMMIT; + +PRAGMA foreign_keys=on; diff --git a/src/db/migrations/004-nullable-branch/up.sql b/src/db/migrations/004-nullable-branch/up.sql new file mode 100644 index 0000000..6333c37 --- /dev/null +++ b/src/db/migrations/004-nullable-branch/up.sql @@ -0,0 +1,23 @@ +PRAGMA foreign_keys=off; + +BEGIN TRANSACTION; + +ALTER TABLE Target RENAME TO _Target_old; + +CREATE TABLE Target ( + id INTEGER PRIMARY KEY, + url TEXT NOT NULL, + branch TEXT, + repo TEXT NOT NULL, + schedule TEXT, + kind TEXT NOT NULL DEFAULT 'git' +); + +INSERT INTO Target (id, url, branch, repo, schedule, kind) + SELECT id, url, branch, repo, schedule, kind FROM _Target_old; + +DROP TABLE _Target_old; + +COMMIT; + +PRAGMA foreign_keys=on;