]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0339.schema.authority_records.sql
LP1779158 Angular7 and ng-lint updates
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0339.schema.authority_records.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0339'); -- dbs
4
5 CREATE RULE protect_authority_rec_delete AS ON DELETE TO authority.record_entry DO INSTEAD (UPDATE authority.record_entry SET deleted = TRUE WHERE OLD.id = authority.record_entry.id);
6
7 CREATE OR REPLACE FUNCTION authority.merge_records ( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
8 DECLARE
9     moved_objects INT := 0;
10     bib_id        INT := 0;
11     bib_rec       biblio.record_entry%ROWTYPE;
12     auth_link     authority.bib_linking%ROWTYPE;
13 BEGIN
14
15     -- 1. Make source_record MARC a copy of the target_record to get auto-sync in linked bib records
16     UPDATE authority.record_entry
17       SET marc = (
18         SELECT marc
19           FROM authority.record_entry
20           WHERE id = target_record
21       )
22       WHERE id = source_record;
23
24     -- 2. Update all bib records with the ID from target_record in their $0
25     FOR bib_rec IN SELECT bre.* FROM biblio.record_entry bre 
26       INNER JOIN authority.bib_linking abl ON abl.bib = bre.id
27       WHERE abl.authority = target_record LOOP
28
29         UPDATE biblio.record_entry
30           SET marc = REGEXP_REPLACE(marc, 
31             E'(<subfield\\s+code="0"\\s*>[^<]*?\\))' || source_record || '<',
32             E'\\1' || target_record || '<', 'g')
33           WHERE id = bib_rec.id;
34
35           moved_objects := moved_objects + 1;
36     END LOOP;
37
38     -- 3. "Delete" source_record
39     DELETE FROM authority.record_entry
40       WHERE id = source_record;
41
42     RETURN moved_objects;
43 END;
44 $func$ LANGUAGE plpgsql;
45
46 COMMIT;