From 628dc917d61dde2bce2b7bd8d8d3abf0f4428d25 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc kop@karlpinc.com" Date: Sun, 14 Jun 2026 22:41:07 +0000 Subject: [PATCH] Change from just a update trigger to insert or update --- db/schemas/lib/triggers/create/events.m4 | 71 ++++++++++++------------ 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/db/schemas/lib/triggers/create/events.m4 b/db/schemas/lib/triggers/create/events.m4 index 44876d9..4738a3b 100644 --- a/db/schemas/lib/triggers/create/events.m4 +++ b/db/schemas/lib/triggers/create/events.m4 @@ -23,45 +23,48 @@ include(`constants.m4')dnl include(`macros.m4')dnl -RAISE INFO 'events_update_func'; -CREATE OR REPLACE FUNCTION events_update_func () + +RAISE INFO 'events_func'; +CREATE OR REPLACE FUNCTION events_func () RETURNS trigger LANGUAGE plpgsql sdb_function_set_search_path AS $$ BEGIN - -- Function for events update trigger + -- Function for events insert and update triggers -- -- AGPL_notice(` --', `2025, 2026', `The Meme Factory, Inc., www.karlpinc.com') - -- Allowing the EID to change makes life too complicated. - cannot_change(`EVENTS', `EID') - - -- If the WID is allowed to change, at minimum -- there may be other - -- cases, the check that ROLES.Participant must be under study - -- becomes complicated. It would require code in the triggers - -- to trace the BIOGRAPHY_DATA.Animid <- ROLES.Participant -> - -- EVENTS.EID --> WATCHES.WID chain to check WATCHES.Date - -- against biography. Simpler to dis-allow change. - cannot_change(`EVENTS', `WID') - - -- Likewise, allowing the AtID to change adds too much complication. - cannot_change(`EVENTS', `AtID') - - -- If the Behavior is allowed to change, we need to add code here - -- that checks all the tables related to the participants and the - -- various event detail tables. This is way too much code; it is - -- easier to prevent the behavior from changing and instead - -- require the old rows be deleted and then new ones added. - cannot_change(`EVENTS', `Behavior') - - IF NEW.behavior = 'sdb_arrival' - AND NEW.start <> OLD.start THEN - -- Changing the start time can change the ARRIVALS.Seq value - UPDATE arrivals - SET seq = NULL -- Recompute - WHERE arrivals.eid = NEW.eid; + IF TG_OP = 'UPDATE' THEN + -- Allowing the EID to change makes life too complicated. + cannot_change(`EVENTS', `EID') + + -- If the WID is allowed to change, at minimum -- there may be other + -- cases, the check that ROLES.Participant must be under study + -- becomes complicated. It would require code in the triggers + -- to trace the BIOGRAPHY_DATA.Animid <- ROLES.Participant -> + -- EVENTS.EID --> WATCHES.WID chain to check WATCHES.Date + -- against biography. Simpler to dis-allow change. + cannot_change(`EVENTS', `WID') + + -- Likewise, allowing the AtID to change adds too much complication. + cannot_change(`EVENTS', `AtID') + + -- If the Behavior is allowed to change, we need to add code here + -- that checks all the tables related to the participants and the + -- various event detail tables. This is way too much code; it is + -- easier to prevent the behavior from changing and instead + -- require the old rows be deleted and then new ones added. + cannot_change(`EVENTS', `Behavior') + + IF NEW.behavior = 'sdb_arrival' + AND NEW.start <> OLD.start THEN + -- Changing the start time can change the ARRIVALS.Seq value + UPDATE arrivals + SET seq = NULL -- Recompute + WHERE arrivals.eid = NEW.eid; + END IF; END IF; RETURN NULL; @@ -198,11 +201,11 @@ CREATE OR REPLACE FUNCTION events_delete_commit_func () $$; -RAISE INFO 'events_update_trigger'; -CREATE TRIGGER events_update_trigger - AFTER UPDATE +RAISE INFO 'events_trigger'; +CREATE TRIGGER events_trigger + AFTER INSERT OR UPDATE ON events FOR EACH ROW - EXECUTE PROCEDURE events_update_func(); + EXECUTE PROCEDURE events_func(); RAISE INFO 'events_delete_commit_trigger'; CREATE CONSTRAINT TRIGGER events_delete_commit_trigger -- 2.34.1