Testing the Audit Trigger
Learn more about how audit triggers work.
We'll cover the following...
Viewing the updates in the artists
table
With that in place, let’s try it out:
Press + to interact
begin;create temp table batch(like moma.artistincluding all)on commit drop;\copy batch from '/usercode/artists/artists.2017-07-01.csv' with csv header delimiter ','with upd as(update moma.artistset (name, bio, nationality, gender, begin, "end", wiki_qid, ulan)= (batch.name, batch.bio, batch.nationality,batch.gender, batch.begin, batch."end",batch.wiki_qid, batch.ulan)from batchwhere batch.constituentid = artist.constituentidand (artist.name, artist.bio, artist.nationality,artist.gender, artist.begin, artist."end",artist.wiki_qid, artist.ulan)<> (batch.name, batch.bio, batch.nationality,batch.gender, batch.begin, batch."end",batch.wiki_qid, batch.ulan)returning artist.constituentid),ins as(insert into moma.artistselect constituentid, name, bio, nationality,gender, begin, "end", wiki_qid, ulanfrom batchwhere not exists(select 1from moma.artistwhere artist.constituentid = batch.constituentid)on conflict (constituentid) do nothingreturning artist.constituentid)select (select count(*) from upd) as updates,(select count(*) from ins) as inserts;commit;
This SQL statement outputs the following information:
BEGIN
CREATE TABLE
COPY 15226
updates │ inserts
═════════╪═════════
52 │ 61
(1 row)
COMMIT
And thanks to our audit trigger, we can have a look at what’s changed:
Press + to interact
select (before -> 'constituentid')::integer as id,after - before as difffrom moma.auditlimit 15;
Here are the first 15 changes out of the 52 updates we made:
id │
...