3 -- check whether patch can be applied
4 -- SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
6 ALTER TABLE authority.control_set_authority_field
7 ADD COLUMN linking_subfield CHAR(1);
9 UPDATE authority.control_set_authority_field
10 SET linking_subfield = '0' WHERE main_entry IS NOT NULL;
12 CREATE TABLE authority.authority_linking (
13 id BIGSERIAL PRIMARY KEY,
14 source BIGINT REFERENCES authority.record_entry (id) NOT NULL,
15 target BIGINT REFERENCES authority.record_entry (id) NOT NULL,
16 field INT REFERENCES authority.control_set_authority_field (id) NOT NULL
19 -- Given an authority record's ID, control set ID (if known), and marc::XML,
20 -- return all links to other authority records in the form of rows that
21 -- can be inserted into authority.authority_linking.
22 CREATE OR REPLACE FUNCTION authority.calculate_authority_linking(
23 rec_id BIGINT, rec_control_set INT, rec_marc_xml XML
24 ) RETURNS SETOF authority.authority_linking AS $func$
26 acsaf authority.control_set_authority_field%ROWTYPE;
28 aal authority.authority_linking%ROWTYPE;
30 IF rec_control_set IS NULL THEN
31 -- No control_set on record? Guess at one
32 SELECT control_set INTO rec_control_set
33 FROM authority.control_set_authority_field
36 XPATH('//*[starts-with(@tag,"1")]/@tag',rec_marc_xml::XML)::TEXT[]
41 RAISE WARNING 'Could not even guess at control set for authority record %', rec_id;
49 SELECT * FROM authority.control_set_authority_field
50 WHERE control_set = rec_control_set
51 AND linking_subfield IS NOT NULL
52 AND main_entry IS NOT NULL
55 (XPATH('//*[@tag="' || acsaf.tag || '"]/*[@code="' ||
56 acsaf.linking_subfield || '"]/text()', rec_marc_xml))[1]::TEXT,
60 -- Ignore links that are null, malformed, circular, or point to
61 -- non-existent authority records.
62 IF link IS NOT NULL AND link::BIGINT <> rec_id THEN
63 PERFORM * FROM authority.record_entry WHERE id = link::BIGINT;
65 aal.target := link::BIGINT;
66 aal.field := acsaf.id;
72 $func$ LANGUAGE PLPGSQL;
75 -- AFTER UPDATE OR INSERT trigger for authority.record_entry
76 CREATE OR REPLACE FUNCTION authority.indexing_ingest_or_delete () RETURNS TRIGGER AS $func$
79 IF NEW.deleted IS TRUE THEN -- If this authority is deleted
80 DELETE FROM authority.bib_linking WHERE authority = NEW.id; -- Avoid updating fields in bibs that are no longer visible
81 DELETE FROM authority.full_rec WHERE record = NEW.id; -- Avoid validating fields against deleted authority records
82 DELETE FROM authority.simple_heading WHERE record = NEW.id;
83 -- Should remove matching $0 from controlled fields at the same time?
85 -- XXX What do we about the actual linking subfields present in
86 -- authority records that target this one when this happens?
87 DELETE FROM authority.authority_linking
88 WHERE source = NEW.id OR target = NEW.id;
90 RETURN NEW; -- and we're done
93 IF TG_OP = 'UPDATE' THEN -- re-ingest?
94 PERFORM * FROM config.internal_flag WHERE name = 'ingest.reingest.force_on_same_marc' AND enabled;
96 IF NOT FOUND AND OLD.marc = NEW.marc THEN -- don't do anything if the MARC didn't change
100 -- Propagate these updates to any linked bib records
101 PERFORM authority.propagate_changes(NEW.id) FROM authority.record_entry WHERE id = NEW.id;
103 DELETE FROM authority.simple_heading WHERE record = NEW.id;
104 DELETE FROM authority.authority_linking WHERE source = NEW.id;
107 INSERT INTO authority.authority_linking (source, target, field)
108 SELECT source, target, field FROM authority.calculate_authority_linking(
109 NEW.id, NEW.control_set, NEW.marc::XML
112 INSERT INTO authority.simple_heading (record,atag,value,sort_value)
113 SELECT record, atag, value, sort_value FROM authority.simple_heading_set(NEW.marc);
115 -- Flatten and insert the afr data
116 PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_full_rec' AND enabled;
118 PERFORM authority.reingest_authority_full_rec(NEW.id);
119 PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_rec_descriptor' AND enabled;
121 PERFORM authority.reingest_authority_rec_descriptor(NEW.id);
127 $func$ LANGUAGE PLPGSQL;