Last active
June 25, 2020 14:03
-
-
Save ecthiender/f472f606428847a56c305cc3edcc0779 to your computer and use it in GitHub Desktop.
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
-- 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