Last active
September 25, 2019 10:37
-
-
Save joshdcuneo/af07d1c4eed6e7fe5f499152c2f3c7c3 to your computer and use it in GitHub Desktop.
SQL Snippetes
This file contains 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 TRIGGER clear_deleted_at BEFORE | |
UPDATE | |
ON todo FOR EACH ROW | |
WHEN ( | |
NOT NEW.deleted | |
AND OLD.deleted IS DISTINCT | |
FROM | |
NEW.deleted | |
) EXECUTE PROCEDURE trigger_clear_deleted_at(); |
This file contains 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 TRIGGER set_deleted_at BEFORE | |
UPDATE | |
ON todo FOR EACH ROW | |
WHEN ( | |
NOT NEW.deleted | |
AND OLD.deleted IS DISTINCT | |
FROM | |
NEW.deleted | |
) EXECUTE PROCEDURE trigger_set_deleted_at(); |
This file contains 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 | |
OR REPLACE FUNCTION trigger_clear_deleted_at() RETURNS TRIGGER AS $$ BEGIN NEW.deleted_at = NULL; | |
RETURN NEW; | |
END; | |
$$ LANGUAGE plpgsql; |
This file contains 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 | |
OR REPLACE FUNCTION trigger_set_deleted_at() RETURNS TRIGGER AS $$ BEGIN NEW.deleted_at = NOW(); | |
RETURN NEW; | |
END; | |
$$ LANGUAGE plpgsql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment