Skip to content

Instantly share code, notes, and snippets.

@jamiehodge
Created May 30, 2013 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiehodge/5680873 to your computer and use it in GitHub Desktop.
Save jamiehodge/5680873 to your computer and use it in GitHub Desktop.
Notify function and trigger
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