Created
February 26, 2020 21:43
-
-
Save dominics/6f2a5bef647710925f5d7b4ea40261d8 to your computer and use it in GitHub Desktop.
trigger
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
$ CREATE FUNCTION sanitize_dates() RETURNS TRIGGER AS $$ | |
BEGIN | |
IF TG_OP = 'INSERT' THEN | |
NEW.created_on := now(); | |
NEW.last_modified := NULL; | |
ELSIF TG_OP = 'UPDATE' THEN | |
NEW.created_on := OLD.created_on; | |
NEW.last_modified := now(); | |
END IF; | |
RETURN NEW; | |
END; | |
$$ LANGUAGE plpgsql; | |
$ CREATE TRIGGER sanitize_dates_trg | |
BEFORE INSERT OR UPDATE | |
ON users | |
FOR EACH ROW | |
EXECUTE PROCEDURE sanitize_dates(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment