Skip to content

Instantly share code, notes, and snippets.

@flowxcode
Created October 29, 2025 18:22
Show Gist options
  • Select an option

  • Save flowxcode/7157af77d004ee4d0e0712c1e0855bef to your computer and use it in GitHub Desktop.

Select an option

Save flowxcode/7157af77d004ee4d0e0712c1e0855bef to your computer and use it in GitHub Desktop.
plpgsql triggers
-- 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$
;
@flowxcode
Copy link
Author

saved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment