Advertisement
roganhamby

Authority Tracking Triggers

May 26th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. create table auditor.audit_authority (
  2.         id              SERIAL,
  3.         audit_id        BIGINT      NOT NULL,
  4.         record          BIGINT          NOT NULL,
  5.         tag             CHAR(3)         NOT NULL,
  6.         ind1            TEXT,
  7.         ind2            TEXT,
  8.         subfield        TEXT,
  9.         value           TEXT            NOT NULL,
  10.         index_vector    tsvector        NOT NULL
  11. );
  12.  
  13.  
  14. -- create table, check
  15. -- #####################################################################
  16.  
  17. CREATE OR REPLACE FUNCTION auditor.audit_authority_backup() RETURNS trigger as $body$
  18. BEGIN
  19.  
  20. INSERT INTO auditor.audit_authority (audit_id, record, tag, ind1, ind2, subfield, value, index_vector)
  21.  values (old.id, old.record, old.tag, old.ind1, old.ind2, old.subfield, old.value, old.index_vector);
  22. RETURN NULL;
  23.  
  24. END;
  25. $body$ LANGUAGE plpgsql;
  26.  
  27. -- create function check
  28. -- #####################################################################
  29.  
  30.  
  31. create trigger audit_authority
  32. before delete on metabib.real_full_rec
  33. for each ROW
  34.     when ( old.tag similar to '(1|4|6|7)%' )
  35. execute procedure auditor.audit_authority_backup();
  36.  
  37. /* this similar to evaluation is very slap dash but it works as a proof of concept */
  38.  
  39. -- create trigger check
  40. -- #####################################################################
  41.  
  42.  
  43. /*
  44. if we have a tag in metabib that doesn't match anything in auditor then it means
  45. it's a new or edited field and it needs to go for evaluation
  46. need distinct record though
  47. */
  48.  
  49. select distinct rfr.record
  50. from auditor.audit_authority aaa
  51. join metabib.real_full_rec rfr on rfr.record = aaa.record
  52. where aaa.value != rfr.value and aaa.tag != rfr.tag and aaa.subfield != rfr.subfield
  53. ;
  54.  
  55.  
  56. -- create sql check
  57. -- #####################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement