Last active
August 29, 2015 14:01
-
-
Save ianthrive/591dbf3836b4d7b36c24 to your computer and use it in GitHub Desktop.
An audit changes on a PostgreSQL table.
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 TABLE customers_audit AS ( | |
id SERIAL NOT NULL UNIQUE PRIMARY KEY, | |
created TIMESTAMPTZ NOT NULL DEFAULT NOW(), | |
before HSTORE, | |
after HSTORE | |
); | |
CREATE FUNCTION trigger_customers_audit RETURNS TRIGGER LANUAGE plpgsql AS $$ | |
INSERT INTO customers_audit(before, after) | |
SELECT hstore(OLD), hstore(NEW); | |
RETURN NEW; | |
END; $$; | |
CREATE TRIGGER customers_audit | |
AFTER UPDATE ON customers FOR EACH ROW | |
EXECUTE PROCEDURE trigger_customers_audit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment