From 27704248923c82036e4e33f55ef6f15ec10151a1 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Sat, 25 May 2024 15:28:02 +0200 Subject: [PATCH] fix: Store empty waypoints as null --- .../20240525132335_empty_waypoint_to_null.php | 43 +++++++++++++++++++ website/db/tests/test-30-moves-waypoint.sql | 10 +++-- 2 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 website/db/migrations/20240525132335_empty_waypoint_to_null.php diff --git a/website/db/migrations/20240525132335_empty_waypoint_to_null.php b/website/db/migrations/20240525132335_empty_waypoint_to_null.php new file mode 100644 index 0000000000..7d9529aa9e --- /dev/null +++ b/website/db/migrations/20240525132335_empty_waypoint_to_null.php @@ -0,0 +1,43 @@ +execute(<<<'EOL' +CREATE OR REPLACE FUNCTION geokrety.moves_waypoint_uppercase() + RETURNS trigger + LANGUAGE 'plpgsql' + VOLATILE + COST 100 +AS $BODY$ +BEGIN + IF NEW.waypoint = '' THEN + NEW.waypoint = NULL; + END IF; + + NEW.waypoint = UPPER(NEW.waypoint); + RETURN NEW; +END; +$BODY$; +EOL); + } + + public function down(): void { + $this->execute(<<<'EOL' +CREATE OR REPLACE FUNCTION geokrety.moves_waypoint_uppercase() + RETURNS trigger + LANGUAGE 'plpgsql' + VOLATILE + COST 100 +AS $BODY$ +BEGIN + NEW.waypoint = UPPER(NEW.waypoint); + RETURN NEW; +END; +$BODY$; +EOL); + } +} diff --git a/website/db/tests/test-30-moves-waypoint.sql b/website/db/tests/test-30-moves-waypoint.sql index 6c0bf8cbff..4726061f55 100644 --- a/website/db/tests/test-30-moves-waypoint.sql +++ b/website/db/tests/test-30-moves-waypoint.sql @@ -2,8 +2,8 @@ BEGIN; -SELECT * FROM no_plan(); --- SELECT plan(6); +-- SELECT * FROM no_plan(); +SELECT plan(10); \set nice '\'0101000020E6100000F6285C8FC2F51C405C8FC2F528DC4540\'' \set move_type_dropped 0 \set move_type_grabbed 1 @@ -20,7 +20,9 @@ INSERT INTO "gk_moves" ("id", "geokret", "author", "position", "moved_on_datetim SELECT is(waypoint, NULL, 'may be null') from gk_moves WHERE id = 1::bigint; -- waypoint cannot be empty -SELECT throws_ok($$INSERT INTO "gk_moves" ("id", "geokret", "author", "position", "moved_on_datetime", "move_type", "waypoint") VALUES (2,, 1 1, '0101000020E6100000F6285C8FC2F51C405C8FC2F528DC4540', '2020-04-07 02:00:00+00', :move_type_dropped, '')$$); +INSERT INTO "gk_moves" ("id", "geokret", "author", "position", "moved_on_datetime", "move_type", "waypoint") +VALUES (2, 1, 1, '0101000020E6100000F6285C8FC2F51C405C8FC2F528DC4540', '2020-04-07 02:00:00+00', :move_type_dropped, ''); +SELECT is(waypoint, NULL, 'may be null') from gk_moves WHERE id = 2::bigint; -- waypoint accepted only for moves types with coordinates SELECT lives_ok($$INSERT INTO "gk_moves" ("id", "geokret", "author", "moved_on_datetime", "move_type", "waypoint", "position") VALUES (3, 1, 1, '2020-04-07 03:00:00+00', 0, 'GC5BRQK', '0101000020E6100000F6285C8FC2F51C405C8FC2F528DC4540')$$); @@ -31,7 +33,7 @@ SELECT throws_ok($$INSERT INTO "gk_moves" ("id", "geokret", "author", "moved_on_ SELECT lives_ok($$INSERT INTO "gk_moves" ("id", "geokret", "author", "moved_on_datetime", "move_type", "waypoint", "position") VALUES (8, 1, 1, '2020-04-07 08:00:00+00', 5, 'GC5BRQK', '0101000020E6100000F6285C8FC2F51C405C8FC2F528DC4540')$$); -- waypoint will be saved uppercase -SELECT throws_ok($$INSERT INTO "gk_moves" ("id", "geokret", "author", "position", "moved_on_datetime", "move_type", "waypoint") VALUES (9, 1, 1, '0101000020E6100000F6285C8FC2F51C405C8FC2F528DC4540', '2020-04-07 09:00:00+00', :move_type_dropped, 'gc5brqk')$$); +SELECT lives_ok($$INSERT INTO "gk_moves" ("id", "geokret", "author", "position", "moved_on_datetime", "move_type", "waypoint") VALUES (9, 1, 1, '0101000020E6100000F6285C8FC2F51C405C8FC2F528DC4540', '2020-04-07 09:00:00+00', 0, 'gc5brqk')$$); SELECT is(waypoint, 'GC5BRQK', 'will be saved uppercase') from gk_moves WHERE id = 9::bigint; -- Finish the tests and clean up.