1 -- Evergreen DB patch XXXX.schema.asset_merge_record_assets.sql
7 -- check whether patch can be applied
8 SELECT evergreen.upgrade_deps_block_check('0639', :eg_version);
10 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
12 moved_objects INT := 0;
13 source_cn asset.call_number%ROWTYPE;
14 target_cn asset.call_number%ROWTYPE;
15 metarec metabib.metarecord%ROWTYPE;
16 hold action.hold_request%ROWTYPE;
17 ser_rec serial.record_entry%ROWTYPE;
24 -- move any 856 entries on records that have at least one MARC-mapped URI entry
25 SELECT INTO uri_count COUNT(*)
26 FROM asset.uri_call_number_map m
27 JOIN asset.call_number cn ON (m.call_number = cn.id)
28 WHERE cn.record = source_record;
32 -- This returns more nodes than you might expect:
33 -- 7 instead of 1 for an 856 with $u $y $9
34 SELECT COUNT(*) INTO counter
35 FROM oils_xpath_table(
38 'biblio.record_entry',
40 'id=' || source_record
43 FOR i IN 1 .. counter LOOP
44 SELECT '<datafield xmlns="http://www.loc.gov/MARC21/slim"' ||
46 ' ind1="' || FIRST(ind1) || '"' ||
47 ' ind2="' || FIRST(ind2) || '">' ||
50 '<subfield code="' || subfield || '">' ||
53 regexp_replace(data,'&','&','g'),
59 ) || '</datafield>' INTO uri_datafield
60 FROM oils_xpath_table(
63 'biblio.record_entry',
64 '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
65 '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
66 '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
67 '//*[@tag="856"][position()=' || i || ']/*[@code]',
68 'id=' || source_record
69 ) as t(id int,ind1 text, ind2 text,subfield text,data text);
71 -- As most of the results will be NULL, protect against NULLifying
72 -- the valid content that we do generate
73 uri_text := uri_text || COALESCE(uri_datafield, '');
76 IF uri_text <> '' THEN
77 UPDATE biblio.record_entry
78 SET marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
79 WHERE id = target_record;
84 -- Find and move metarecords to the target record
86 FROM metabib.metarecord
87 WHERE master_record = source_record;
90 UPDATE metabib.metarecord
91 SET master_record = target_record,
93 WHERE id = metarec.id;
95 moved_objects := moved_objects + 1;
98 -- Find call numbers attached to the source ...
99 FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
101 SELECT INTO target_cn *
102 FROM asset.call_number
103 WHERE label = source_cn.label
104 AND owning_lib = source_cn.owning_lib
105 AND record = target_record;
107 -- ... and if there's a conflicting one on the target ...
110 -- ... move the copies to that, and ...
112 SET call_number = target_cn.id
113 WHERE call_number = source_cn.id;
115 -- ... move V holds to the move-target call number
116 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
118 UPDATE action.hold_request
119 SET target = target_cn.id
122 moved_objects := moved_objects + 1;
127 -- ... just move the call number to the target record
128 UPDATE asset.call_number
129 SET record = target_record
130 WHERE id = source_cn.id;
133 moved_objects := moved_objects + 1;
136 -- Find T holds targeting the source record ...
137 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
139 -- ... and move them to the target record
140 UPDATE action.hold_request
141 SET target = target_record
144 moved_objects := moved_objects + 1;
147 -- Find serial records targeting the source record ...
148 FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
149 -- ... and move them to the target record
150 UPDATE serial.record_entry
151 SET record = target_record
152 WHERE id = ser_rec.id;
154 moved_objects := moved_objects + 1;
157 -- Finally, "delete" the source record
158 DELETE FROM biblio.record_entry WHERE id = source_record;
160 -- That's all, folks!
161 RETURN moved_objects;
163 $func$ LANGUAGE plpgsql;