Skip to content

Instantly share code, notes, and snippets.

@ecthiender
Last active June 25, 2020 14:03
Show Gist options
  • Save ecthiender/f472f606428847a56c305cc3edcc0779 to your computer and use it in GitHub Desktop.
Save ecthiender/f472f606428847a56c305cc3edcc0779 to your computer and use it in GitHub Desktop.
-- the function to call when the trigger is invoked
CREATE FUNCTION trigger_on_note_revision()
RETURNS TRIGGER
LANGUAGE PLPGSQL AS $BODY$
BEGIN
-- Create revision only if node's subject or body columns have changed
IF OLD.title <> NEW.title OR OLD."data" <> NEW."data" THEN
INSERT INTO note_revision (note_id, created_at, title, "data")
VALUES (OLD.id, OLD.updated_at, OLD.title, OLD."data");
NEW.updated_at = now();
END IF;
-- Return the NEW record so that update can carry on as usual
RETURN NEW;
END; $BODY$;
-- create the trigger
CREATE TRIGGER trigger_note_revision
BEFORE UPDATE
ON note
FOR EACH ROW
EXECUTE PROCEDURE trigger_on_note_revision();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment