From 9a552f53027f526df9e8ad43ec218708d5e3445c Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 7 Nov 2022 21:11:10 +0100 Subject: [PATCH 1/4] 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; From 5542be041890fb9f438a0b2f03a764579d2f119e Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 7 Nov 2022 21:13:40 +0100 Subject: [PATCH 2/4] fix(api): set arch if not provided or empty (fixes #278) --- src/server/api_targets.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/api_targets.v b/src/server/api_targets.v index 6f284af..c4d32d2 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -32,7 +32,7 @@ fn (mut app App) v1_post_target() web.Result { // If a repo is created without specifying the arch, we assume it's meant // for the default architecture. - if 'arch' !in params { + if 'arch' !in params || params['arch'] == '' { params['arch'] = app.conf.default_arch } From fc4dc30f741c882368c616298b5b6ba06d5d6ce1 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 7 Nov 2022 21:35:49 +0100 Subject: [PATCH 3/4] fix(api): always return JSON response on success (fixes #276) --- src/console/targets/targets.v | 7 ++----- src/server/api_targets.v | 8 +++++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 521ca23..4179363 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -227,8 +227,7 @@ fn remove(conf Config, id string) ! { if id_int != 0 { c := client.new(conf.address, conf.api_key) - res := c.remove_target(id_int)! - println(res.message) + c.remove_target(id_int)! } } @@ -245,9 +244,7 @@ fn patch(conf Config, id string, params map[string]string) ! { id_int := id.int() if id_int != 0 { c := client.new(conf.address, conf.api_key) - res := c.patch_target(id_int, params)! - - println(res.message) + c.patch_target(id_int, params)! } } diff --git a/src/server/api_targets.v b/src/server/api_targets.v index c4d32d2..16db7e9 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -47,7 +47,7 @@ fn (mut app App) v1_post_target() web.Result { id := app.db.add_target(new_repo) - return app.json(http.Status.ok, new_data_response(id)) + return app.json(.ok, new_data_response(id)) } // v1_delete_target removes a given target from the server's list. @@ -55,7 +55,7 @@ fn (mut app App) v1_post_target() web.Result { fn (mut app App) v1_delete_target(id int) web.Result { app.db.delete_target(id) - return app.status(.ok) + return app.json(.ok, new_response('')) } // v1_patch_target updates a target's data with the given query params. @@ -69,5 +69,7 @@ fn (mut app App) v1_patch_target(id int) web.Result { app.db.update_target_archs(id, arch_objs) } - return app.status(.ok) + repo := app.db.get_target(id) or { return app.status(.internal_server_error) } + + return app.json(.ok, new_data_response(repo)) } From 17e58c91ed95d227d12364c56469da5144db8757 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 7 Nov 2022 21:40:59 +0100 Subject: [PATCH 4/4] chore: updated changelog; ran formatter --- CHANGELOG.md | 8 ++++++++ src/db/db.v | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18311d7..a550524 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Migrated codebase to V 0.3.2 +### Fixed + +* Arch value for target is now properly set if not provided +* All API endpoints now return proper JSON on success + * CLI no longer exits with non-zero status code when removing/patching + target +* Allow NULL values for branch in database + ## [0.4.0](https://git.rustybever.be/vieter-v/vieter/src/tag/0.4.0) ### Added diff --git a/src/db/db.v b/src/db/db.v index 6d9ab43..1a0160e 100644 --- a/src/db/db.v +++ b/src/db/db.v @@ -17,13 +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') + $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') + $embed_file('migrations/004-nullable-branch/down.sql'), ] )