Skip to content

Commit

Permalink
#855 add before insert triggers to exclude rows from either allowlist…
Browse files Browse the repository at this point in the history
…/denylist
  • Loading branch information
gabrielwol committed Jul 26, 2024
1 parent c3ccc5d commit 4f98605
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
CREATE OR REPLACE FUNCTION miovision_api.exclude_denylist_from_intersection_movements()
RETURNS TRIGGER

Check failure on line 2 in volumes/miovision/sql/function/function-exclude_denylist_from_intersection_movements.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

CP05: Datatypes must be lower case.
LANGUAGE plpgsql
AS $BODY$

DECLARE row_in_im numeric = (
SELECT COUNT(*)
FROM miovision_api.intersection_movements_denylist AS im
WHERE
im.intersection_uid = new.intersection_uid
AND im.leg = new.leg
AND im.movement_uid = new.movement_uid
AND im.classification_uid = new.classification_uid
);

BEGIN
IF (row_in_im) THEN
RAISE NOTICE 'Row % is not being inserted. Already in intersection_movements_denylist table.', new;
--Return null to stop insert.
RETURN NULL;
ELSE
RETURN new;
END IF;
END;
$BODY$;

ALTER FUNCTION miovision_api.exclude_denylist_from_intersection_movements OWNER TO miovision_admins;

Check notice on line 27 in volumes/miovision/sql/function/function-exclude_denylist_from_intersection_movements.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT01: Unexpected whitespace before 'OWNER' keyword.

GRANT EXECUTE ON miovision_api.exclude_denylist_from_intersection_movements TO miovision_api_bot;

COMMENT ON miovision_api.exclude_denylist_from_intersection_movements
IS 'Runs before insert into miovision_api.intersection_movements to prevent inserts that are on
the denylist (miovision_api.intersection_movements_denylist).';

/*Test:
INSERT INTO miovision_api.intersection_movements
SELECT * FROM miovision_api.intersection_movements_denylist LIMIT 1;
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
CREATE OR REPLACE FUNCTION miovision_api.exclude_intersection_movements_from_denylist()
RETURNS TRIGGER

Check failure on line 2 in volumes/miovision/sql/function/function-exclude_intersection_movements_from_denylist.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

CP05: Datatypes must be lower case.
LANGUAGE plpgsql
AS $BODY$

DECLARE row_in_im numeric = (
SELECT COUNT(*)
FROM miovision_api.intersection_movements AS im
WHERE
im.intersection_uid = new.intersection_uid
AND im.leg = new.leg
AND im.movement_uid = new.movement_uid
AND im.classification_uid = new.classification_uid
);

BEGIN
IF (row_in_im) THEN
RAISE NOTICE 'Row % is not being inserted. Already in intersection_movements table.', new;
--Return null to stop insert.
RETURN NULL;
ELSE
RETURN new;
END IF;
END;
$BODY$;

ALTER FUNCTION miovision_api.exclude_intersection_movements_from_denylist OWNER TO miovision_admins;

Check notice on line 27 in volumes/miovision/sql/function/function-exclude_intersection_movements_from_denylist.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT01: Unexpected whitespace before 'OWNER' keyword.

GRANT EXECUTE ON miovision_api.exclude_intersection_movements_from_denylist TO miovision_api_bot;

COMMENT ON miovision_api.exclude_intersection_movements_from_denylist
IS 'Runs before insert into miovision_api.intersection_movements_denylist to prevent inserts
that are already in intersections_movements (allowlist).';

/*Test:
INSERT INTO miovision_api.intersection_movements_denylist
SELECT * FROM miovision_api.intersection_movements LIMIT 1;
*/
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ SELECT DISTINCT intersection_uid, classification_uid, leg, movement_uid
ALTER TABLE miovision.intersection_movements ADD UNIQUE (intersection_uid, classification_uid, leg, movement_uid);

Check failure on line 4 in volumes/miovision/sql/table/create-table-intersection_movements.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Line should not be indented.

Check notice on line 4 in volumes/miovision/sql/table/create-table-intersection_movements.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT05: Line is too long (115 > 100).
COMMENT ON TABLE miovision.intersection_movements IS 'Unique movements for each intersection by classification';

Check failure on line 5 in volumes/miovision/sql/table/create-table-intersection_movements.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Line should not be indented.

Check notice on line 5 in volumes/miovision/sql/table/create-table-intersection_movements.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT05: Line is too long (113 > 100).
GRANT ALL ON TABLE miovision.intersection_movements TO miovision_admins;

Check failure on line 6 in volumes/miovision/sql/table/create-table-intersection_movements.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT02: Line should not be indented.
GRANT SELECT, REFERENCES, TRIGGER ON TABLE miovision.intersection_movements TO bdit_humans WITH GRANT OPTION;
GRANT SELECT, REFERENCES, TRIGGER ON TABLE miovision.intersection_movements TO bdit_humans WITH GRANT OPTION;

Check notice on line 7 in volumes/miovision/sql/table/create-table-intersection_movements.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

LT05: Line is too long (109 > 100).

CREATE TRIGGER denylist_intersection_movements_exclusion
BEFORE INSERT ON miovision_api.intersection_movements
FOR EACH ROW
EXECUTE FUNCTION miovision_api.exclude_denylist_from_intersection_movements();
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,4 @@ Uniqueness from intersection_movements is enforced via on insert trigger.';
CREATE TRIGGER intersection_movements_denylist_exclusion
BEFORE INSERT ON miovision_api.intersection_movements_denylist
FOR EACH ROW
EXECUTE FUNCTION miovision_api.exclude_intersection_movements_from_denylist();

CREATE OR REPLACE FUNCTION miovision_api.exclude_intersection_movements_from_denylist()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $BODY$

DECLARE row_in_im numeric = (
SELECT COUNT(*)
FROM miovision_api.intersection_movements AS im
WHERE
im.intersection_uid = new.intersection_uid
AND im.leg = new.leg
AND im.movement_uid = new.movement_uid
AND im.classification_uid = new.classification_uid
);

BEGIN
IF (row_in_im) THEN
RAISE NOTICE 'Row % is not being inserted. Already in intersection_movements table.', new;
--Return null to stop insert.
RETURN NULL;
ELSE
RETURN new;
END IF;
END;
$BODY$;

TRUNCATE miovision_api.intersection_movements_denylist;

INSERT INTO miovision_api.intersection_movements_denylist
SELECT * FROM miovision_api.intersection_movements LIMIT 1;

INSERT INTO miovision_api.intersection_movements_denylist (intersection_uid, classification_uid, leg, movement_uid)
VALUES (1, 2, 'E', 3);
EXECUTE FUNCTION miovision_api.exclude_intersection_movements_from_denylist();

0 comments on commit 4f98605

Please sign in to comment.