Skip to content

Commit

Permalink
fix: Store empty waypoints as null
Browse files Browse the repository at this point in the history
  • Loading branch information
kumy committed May 25, 2024
1 parent 4b1d684 commit 2770424
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
43 changes: 43 additions & 0 deletions website/db/migrations/20240525132335_empty_waypoint_to_null.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class EmptyWaypointToNull extends AbstractMigration {
public function up(): void {
$this->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);
}
}
10 changes: 6 additions & 4 deletions website/db/tests/test-30-moves-waypoint.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')$$);
Expand All @@ -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.
Expand Down

0 comments on commit 2770424

Please sign in to comment.