diff --git a/database/task-containers.sql b/database/task-containers.sql index edb44ad..7403d2c 100644 --- a/database/task-containers.sql +++ b/database/task-containers.sql @@ -139,3 +139,32 @@ REVOKE EXECUTE CREATE TRIGGER tasks_item_au AFTER UPDATE OF item_id ON tasks FOR EACH ROW EXECUTE PROCEDURE tasks_item_au( ); + + +/* + * After an update on some task's logical container, set the task's parent ID. + */ +DROP FUNCTION IF EXISTS tasks_item_au( ) CASCADE; +CREATE FUNCTION tasks_ltc_au( ) + RETURNS TRIGGER + LANGUAGE PLPGSQL + SECURITY DEFINER + AS $tasks_ltc_au$ +BEGIN + UPDATE tasks + SET task_id_parent = ( + SELECT task_id + FROM logical_task_containers + WHERE ltc_id = NEW.ltc_id ) + WHERE task_id = NEW.task_id; + RETURN NEW; +END; +$tasks_ltc_au$; + +REVOKE EXECUTE + ON FUNCTION tasks_ltc_au( ) + FROM PUBLIC; + +CREATE TRIGGER tasks_ltc_au + AFTER UPDATE OF ltc_id ON tasks + FOR EACH ROW EXECUTE PROCEDURE tasks_ltc_au( ); diff --git a/database/upgrade/from-bca6b99068cbf1dd4148d6bd5cb60e926162cfe1.sql b/database/upgrade/from-bca6b99068cbf1dd4148d6bd5cb60e926162cfe1.sql new file mode 100644 index 0000000..b44ea05 --- /dev/null +++ b/database/upgrade/from-bca6b99068cbf1dd4148d6bd5cb60e926162cfe1.sql @@ -0,0 +1,37 @@ +-- +-- Upgrade the database from commit ID bca6b99068cbf1dd4148d6bd5cb60e926162cfe1 +-- +-- Run this from the top-level directory +-- + + +\i database/config.sql +\c :db_name + +BEGIN; + + DROP FUNCTION IF EXISTS tasks_item_au( ) CASCADE; + CREATE FUNCTION tasks_ltc_au( ) + RETURNS TRIGGER + LANGUAGE PLPGSQL + SECURITY DEFINER + AS $tasks_ltc_au$ + BEGIN + UPDATE tasks + SET task_id_parent = ( + SELECT task_id + FROM logical_task_containers + WHERE ltc_id = NEW.ltc_id ) + WHERE task_id = NEW.task_id; + RETURN NEW; + END; + $tasks_ltc_au$; + + REVOKE EXECUTE + ON FUNCTION tasks_ltc_au( ) + FROM PUBLIC; + + CREATE TRIGGER tasks_ltc_au + AFTER UPDATE OF ltc_id ON tasks + FOR EACH ROW EXECUTE PROCEDURE tasks_ltc_au( ); +COMMIT;