diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/audit.sql b/audit.sql index 601d749..d41cad8 100644 --- a/audit.sql +++ b/audit.sql @@ -180,7 +180,7 @@ $body$; -CREATE OR REPLACE FUNCTION audit.audit_table(target_table regclass, audit_rows boolean, audit_query_text boolean, ignored_cols text[]) RETURNS void AS $body$ +CREATE OR REPLACE FUNCTION audit.audit_table(target_table regclass, audit_rows boolean, audit_query_text boolean, audit_inserts boolean, ignored_cols text[]) RETURNS void AS $body$ DECLARE stm_targets text = 'INSERT OR UPDATE OR DELETE OR TRUNCATE'; _q_txt text; @@ -193,7 +193,9 @@ BEGIN IF array_length(ignored_cols,1) > 0 THEN _ignored_cols_snip = ', ' || quote_literal(ignored_cols); END IF; - _q_txt = 'CREATE TRIGGER audit_trigger_row AFTER INSERT OR UPDATE OR DELETE ON ' || + _q_txt = 'CREATE TRIGGER audit_trigger_row AFTER ' || + CASE WHEN audit_inserts THEN 'INSERT OR ' ELSE '' END || + 'UPDATE OR DELETE ON ' || target_table || ' FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func(' || quote_literal(audit_query_text) || _ignored_cols_snip || ');'; @@ -214,26 +216,37 @@ END; $body$ language 'plpgsql'; -COMMENT ON FUNCTION audit.audit_table(regclass, boolean, boolean, text[]) IS $body$ +COMMENT ON FUNCTION audit.audit_table(regclass, boolean, boolean, boolean, text[]) IS $body$ Add auditing support to a table. Arguments: target_table: Table name, schema qualified if not on search_path audit_rows: Record each row change, or only audit at a statement level audit_query_text: Record the text of the client query that triggered the audit event? + audit_inserts: Audit insert statements or only updates/deletes/truncates? ignored_cols: Columns to exclude from update diffs, ignore updates that change only ignored cols. $body$; +-- Adaptor to older variant without the audit_inserts parameter for backwards compatibility +CREATE OR REPLACE FUNCTION audit.audit_table(target_table regclass, audit_rows boolean, audit_query_text boolean, ignored_cols text[]) RETURNS void AS $body$ +SELECT audit.audit_table($1, $2, $3, BOOLEAN 't', ignored_cols); +$body$ LANGUAGE SQL; + -- Pg doesn't allow variadic calls with 0 params, so provide a wrapper +CREATE OR REPLACE FUNCTION audit.audit_table(target_table regclass, audit_rows boolean, audit_query_text boolean, audit_inserts boolean) RETURNS void AS $body$ +SELECT audit.audit_table($1, $2, $3, $4, ARRAY[]::text[]); +$body$ LANGUAGE SQL; + +-- Older wrapper for backwards compatibility CREATE OR REPLACE FUNCTION audit.audit_table(target_table regclass, audit_rows boolean, audit_query_text boolean) RETURNS void AS $body$ -SELECT audit.audit_table($1, $2, $3, ARRAY[]::text[]); +SELECT audit.audit_table($1, $2, $3, BOOLEAN 't', ARRAY[]::text[]); $body$ LANGUAGE SQL; -- And provide a convenience call wrapper for the simplest case -- of row-level logging with no excluded cols and query logging enabled. -- CREATE OR REPLACE FUNCTION audit.audit_table(target_table regclass) RETURNS void AS $body$ -SELECT audit.audit_table($1, BOOLEAN 't', BOOLEAN 't'); +SELECT audit.audit_table($1, BOOLEAN 't', BOOLEAN 't', BOOLEAN 't'); $body$ LANGUAGE 'sql'; COMMENT ON FUNCTION audit.audit_table(regclass) IS $body$