3 INSERT INTO config.upgrade_log (version) VALUES ('0339'); -- dbs
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);
7 CREATE OR REPLACE FUNCTION authority.merge_records ( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
9 moved_objects INT := 0;
11 bib_rec biblio.record_entry%ROWTYPE;
12 auth_link authority.bib_linking%ROWTYPE;
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
19 FROM authority.record_entry
20 WHERE id = target_record
22 WHERE id = source_record;
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
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;
35 moved_objects := moved_objects + 1;
38 -- 3. "Delete" source_record
39 DELETE FROM authority.record_entry
40 WHERE id = source_record;
44 $func$ LANGUAGE plpgsql;