Created
May 30, 2013 20:24
-
-
Save jamiehodge/5680873 to your computer and use it in GitHub Desktop.
Notify function and 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(:notify, <<-SQL, language: :plpgsql, returns: :trigger, replace: true) | |
BEGIN | |
IF (TG_OP = 'DELETE') THEN | |
PERFORM pg_notify(TG_TABLE_NAME, '{ \"id\": \"' || OLD.id || '\", \"event\": \"' || TG_OP || '\" }'); | |
RETURN OLD; | |
ELSE | |
PERFORM pg_notify(TG_TABLE_NAME, '{ \"id\": \"' || NEW.id || '\", \"event\": \"' || TG_OP || '\" }'); | |
RETURN NEW; | |
END IF; | |
END; | |
SQL | |
end | |
create_trigger :table, :notify, :notify, each_row: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment