Created
October 29, 2025 18:22
-
-
Save flowxcode/7157af77d004ee4d0e0712c1e0855bef to your computer and use it in GitHub Desktop.
plpgsql triggers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- DROP FUNCTION public.archive_requirements_history(); | |
| CREATE OR REPLACE FUNCTION public.archive_requirements_history() | |
| RETURNS trigger | |
| LANGUAGE plpgsql | |
| AS $function$ | |
| BEGIN | |
| -- Archive old row to history only if version has changed | |
| IF OLD.version <> NEW.version THEN | |
| INSERT INTO public.requirements_history ( | |
| req_id, "text", "version", categories, created_at, updated_at | |
| ) | |
| VALUES ( | |
| NEW.req_id, NEW."text", NEW."version", NEW.categories, | |
| NEW.created_at, CURRENT_TIMESTAMP | |
| ) | |
| ON CONFLICT (req_id, version) DO NOTHING; -- Avoid duplicates | |
| END IF; | |
| RETURN NEW; | |
| END; | |
| $function$ | |
| ; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
saved