2 * Copyright (C) 2008 Equinox Software, Inc.
3 * Bill Erickson <erickson@esilibrary.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 CREATE OR REPLACE FUNCTION actor.usr_merge_rows( table_name TEXT, col_name TEXT, src_usr INT, dest_usr INT ) RETURNS VOID AS $$
24 sel := 'SELECT id::BIGINT FROM ' || table_name || ' WHERE ' || quote_ident(col_name) || ' = ' || quote_literal(src_usr);
25 upd := 'UPDATE ' || table_name || ' SET ' || quote_ident(col_name) || ' = ' || quote_literal(dest_usr) || ' WHERE id = ';
26 del := 'DELETE FROM ' || table_name || ' WHERE id = ';
27 FOR cur_row IN EXECUTE sel LOOP
29 --RAISE NOTICE 'Attempting to merge % %', table_name, cur_row.id;
30 EXECUTE upd || cur_row.id;
31 EXCEPTION WHEN unique_violation THEN
32 --RAISE NOTICE 'Deleting conflicting % %', table_name, cur_row.id;
33 EXECUTE del || cur_row.id;
39 COMMENT ON FUNCTION actor.usr_merge_rows(TEXT, TEXT, INT, INT) IS $$
41 * Attempts to move each row of the specified table from src_user to dest_user.
42 * Where conflicts exist, the conflicting "source" row is deleted.
47 CREATE OR REPLACE FUNCTION actor.usr_merge( src_usr INT, dest_usr INT, del_addrs BOOLEAN, del_cards BOOLEAN, deactivate_cards BOOLEAN ) RETURNS VOID AS $$
56 -- do some initial cleanup
57 UPDATE actor.usr SET card = NULL WHERE id = src_usr;
58 UPDATE actor.usr SET mailing_address = NULL WHERE id = src_usr;
59 UPDATE actor.usr SET billing_address = NULL WHERE id = src_usr;
63 DELETE FROM actor.card where usr = src_usr;
65 IF deactivate_cards THEN
66 UPDATE actor.card SET active = 'f' WHERE usr = src_usr;
68 UPDATE actor.card SET usr = dest_usr WHERE usr = src_usr;
73 DELETE FROM actor.usr_address WHERE usr = src_usr;
75 UPDATE actor.usr_address SET usr = dest_usr WHERE usr = src_usr;
78 UPDATE actor.usr_note SET usr = dest_usr WHERE usr = src_usr;
79 -- dupes are technically OK in actor.usr_standing_penalty, should manually delete them...
80 UPDATE actor.usr_standing_penalty SET usr = dest_usr WHERE usr = src_usr;
81 PERFORM actor.usr_merge_rows('actor.usr_org_unit_opt_in', 'usr', src_usr, dest_usr);
82 PERFORM actor.usr_merge_rows('actor.usr_setting', 'usr', src_usr, dest_usr);
85 PERFORM actor.usr_merge_rows('permission.usr_perm_map', 'usr', src_usr, dest_usr);
86 PERFORM actor.usr_merge_rows('permission.usr_object_perm_map', 'usr', src_usr, dest_usr);
87 PERFORM actor.usr_merge_rows('permission.usr_grp_map', 'usr', src_usr, dest_usr);
88 PERFORM actor.usr_merge_rows('permission.usr_work_ou_map', 'usr', src_usr, dest_usr);
93 -- For each *_bucket table: transfer every bucket belonging to src_usr
94 -- into the custody of dest_usr.
96 -- In order to avoid colliding with an existing bucket owned by
97 -- the destination user, append the source user's id (in parenthesese)
98 -- to the name. If you still get a collision, add successive
99 -- spaces to the name and keep trying until you succeed.
103 FROM container.biblio_record_entry_bucket
104 WHERE owner = src_usr
106 suffix := ' (' || src_usr || ')';
109 UPDATE container.biblio_record_entry_bucket
110 SET owner = dest_usr, name = name || suffix
111 WHERE id = bucket_row.id;
112 EXCEPTION WHEN unique_violation THEN
113 suffix := suffix || ' ';
122 FROM container.call_number_bucket
123 WHERE owner = src_usr
125 suffix := ' (' || src_usr || ')';
128 UPDATE container.call_number_bucket
129 SET owner = dest_usr, name = name || suffix
130 WHERE id = bucket_row.id;
131 EXCEPTION WHEN unique_violation THEN
132 suffix := suffix || ' ';
141 FROM container.copy_bucket
142 WHERE owner = src_usr
144 suffix := ' (' || src_usr || ')';
147 UPDATE container.copy_bucket
148 SET owner = dest_usr, name = name || suffix
149 WHERE id = bucket_row.id;
150 EXCEPTION WHEN unique_violation THEN
151 suffix := suffix || ' ';
160 FROM container.user_bucket
161 WHERE owner = src_usr
163 suffix := ' (' || src_usr || ')';
166 UPDATE container.user_bucket
167 SET owner = dest_usr, name = name || suffix
168 WHERE id = bucket_row.id;
169 EXCEPTION WHEN unique_violation THEN
170 suffix := suffix || ' ';
177 UPDATE container.user_bucket_item SET target_user = dest_usr WHERE target_user = src_usr;
180 -- transfer queues the same way we transfer buckets (see above)
184 WHERE owner = src_usr
186 suffix := ' (' || src_usr || ')';
189 UPDATE vandelay.queue
190 SET owner = dest_usr, name = name || suffix
191 WHERE id = queue_row.id;
192 EXCEPTION WHEN unique_violation THEN
193 suffix := suffix || ' ';
201 PERFORM actor.usr_merge_rows('money.collections_tracker', 'usr', src_usr, dest_usr);
202 PERFORM actor.usr_merge_rows('money.collections_tracker', 'collector', src_usr, dest_usr);
203 UPDATE money.billable_xact SET usr = dest_usr WHERE usr = src_usr;
204 UPDATE money.billing SET voider = dest_usr WHERE voider = src_usr;
205 UPDATE money.bnm_payment SET accepting_usr = dest_usr WHERE accepting_usr = src_usr;
208 UPDATE action.circulation SET usr = dest_usr WHERE usr = src_usr;
209 UPDATE action.circulation SET circ_staff = dest_usr WHERE circ_staff = src_usr;
210 UPDATE action.circulation SET checkin_staff = dest_usr WHERE checkin_staff = src_usr;
212 UPDATE action.hold_request SET usr = dest_usr WHERE usr = src_usr;
213 UPDATE action.hold_request SET fulfillment_staff = dest_usr WHERE fulfillment_staff = src_usr;
214 UPDATE action.hold_request SET requestor = dest_usr WHERE requestor = src_usr;
215 UPDATE action.hold_notification SET notify_staff = dest_usr WHERE notify_staff = src_usr;
217 UPDATE action.in_house_use SET staff = dest_usr WHERE staff = src_usr;
218 UPDATE action.non_cataloged_circulation SET staff = dest_usr WHERE staff = src_usr;
219 UPDATE action.non_cataloged_circulation SET patron = dest_usr WHERE patron = src_usr;
220 UPDATE action.non_cat_in_house_use SET staff = dest_usr WHERE staff = src_usr;
221 UPDATE action.survey_response SET usr = dest_usr WHERE usr = src_usr;
224 UPDATE acq.fund_allocation SET allocator = dest_usr WHERE allocator = src_usr;
225 UPDATE acq.fund_transfer SET transfer_user = dest_usr WHERE transfer_user = src_usr;
227 -- transfer picklists the same way we transfer buckets (see above)
231 WHERE owner = src_usr
233 suffix := ' (' || src_usr || ')';
237 SET owner = dest_usr, name = name || suffix
238 WHERE id = picklist_row.id;
239 EXCEPTION WHEN unique_violation THEN
240 suffix := suffix || ' ';
247 UPDATE acq.purchase_order SET owner = dest_usr WHERE owner = src_usr;
248 UPDATE acq.po_note SET creator = dest_usr WHERE creator = src_usr;
249 UPDATE acq.po_note SET editor = dest_usr WHERE editor = src_usr;
250 UPDATE acq.provider_note SET creator = dest_usr WHERE creator = src_usr;
251 UPDATE acq.provider_note SET editor = dest_usr WHERE editor = src_usr;
252 UPDATE acq.lineitem_note SET creator = dest_usr WHERE creator = src_usr;
253 UPDATE acq.lineitem_note SET editor = dest_usr WHERE editor = src_usr;
254 UPDATE acq.lineitem_usr_attr_definition SET usr = dest_usr WHERE usr = src_usr;
257 UPDATE asset.copy SET creator = dest_usr WHERE creator = src_usr;
258 UPDATE asset.copy SET editor = dest_usr WHERE editor = src_usr;
259 UPDATE asset.copy_note SET creator = dest_usr WHERE creator = src_usr;
260 UPDATE asset.call_number SET creator = dest_usr WHERE creator = src_usr;
261 UPDATE asset.call_number SET editor = dest_usr WHERE editor = src_usr;
262 UPDATE asset.call_number_note SET creator = dest_usr WHERE creator = src_usr;
265 UPDATE serial.record_entry SET creator = dest_usr WHERE creator = src_usr;
266 UPDATE serial.record_entry SET editor = dest_usr WHERE editor = src_usr;
269 -- It's not uncommon to define the reporter schema in a replica
270 -- DB only, so don't assume these tables exist in the write DB.
272 UPDATE reporter.template SET owner = dest_usr WHERE owner = src_usr;
273 EXCEPTION WHEN undefined_table THEN
277 UPDATE reporter.report SET owner = dest_usr WHERE owner = src_usr;
278 EXCEPTION WHEN undefined_table THEN
282 UPDATE reporter.schedule SET runner = dest_usr WHERE runner = src_usr;
283 EXCEPTION WHEN undefined_table THEN
287 -- transfer folders the same way we transfer buckets (see above)
290 FROM reporter.template_folder
291 WHERE owner = src_usr
293 suffix := ' (' || src_usr || ')';
296 UPDATE reporter.template_folder
297 SET owner = dest_usr, name = name || suffix
298 WHERE id = folder_row.id;
299 EXCEPTION WHEN unique_violation THEN
300 suffix := suffix || ' ';
306 EXCEPTION WHEN undefined_table THEN
310 -- transfer folders the same way we transfer buckets (see above)
313 FROM reporter.report_folder
314 WHERE owner = src_usr
316 suffix := ' (' || src_usr || ')';
319 UPDATE reporter.report_folder
320 SET owner = dest_usr, name = name || suffix
321 WHERE id = folder_row.id;
322 EXCEPTION WHEN unique_violation THEN
323 suffix := suffix || ' ';
329 EXCEPTION WHEN undefined_table THEN
333 -- transfer folders the same way we transfer buckets (see above)
336 FROM reporter.output_folder
337 WHERE owner = src_usr
339 suffix := ' (' || src_usr || ')';
342 UPDATE reporter.output_folder
343 SET owner = dest_usr, name = name || suffix
344 WHERE id = folder_row.id;
345 EXCEPTION WHEN unique_violation THEN
346 suffix := suffix || ' ';
352 EXCEPTION WHEN undefined_table THEN
356 -- Finally, delete the source user
357 DELETE FROM actor.usr WHERE id = src_usr;
362 COMMENT ON FUNCTION actor.usr_merge(INT, INT, BOOLEAN, BOOLEAN, BOOLEAN) IS $$
364 * Merges all user date from src_usr to dest_usr. When collisions occur,
365 * keep dest_usr's data and delete src_usr's data.
370 CREATE OR REPLACE FUNCTION actor.usr_purge_data(
376 renamable_row RECORD;
382 mailing_address = NULL,
383 billing_address = NULL
387 UPDATE acq.fund_allocation SET allocator = dest_usr WHERE allocator = src_usr;
388 UPDATE acq.lineitem SET creator = dest_usr WHERE creator = src_usr;
389 UPDATE acq.lineitem SET editor = dest_usr WHERE editor = src_usr;
390 UPDATE acq.lineitem SET selector = dest_usr WHERE selector = src_usr;
391 UPDATE acq.lineitem_note SET creator = dest_usr WHERE creator = src_usr;
392 UPDATE acq.lineitem_note SET editor = dest_usr WHERE editor = src_usr;
393 DELETE FROM acq.lineitem_usr_attr_definition WHERE usr = src_usr;
395 -- Update with a rename to avoid collisions
399 WHERE owner = src_usr
401 suffix := ' (' || src_usr || ')';
405 SET owner = dest_usr, name = name || suffix
406 WHERE id = renamable_row.id;
407 EXCEPTION WHEN unique_violation THEN
408 suffix := suffix || ' ';
415 UPDATE acq.picklist SET creator = dest_usr WHERE creator = src_usr;
416 UPDATE acq.picklist SET editor = dest_usr WHERE editor = src_usr;
417 UPDATE acq.po_note SET creator = dest_usr WHERE creator = src_usr;
418 UPDATE acq.po_note SET editor = dest_usr WHERE editor = src_usr;
419 UPDATE acq.purchase_order SET owner = dest_usr WHERE owner = src_usr;
420 UPDATE acq.purchase_order SET creator = dest_usr WHERE creator = src_usr;
421 UPDATE acq.purchase_order SET editor = dest_usr WHERE editor = src_usr;
422 UPDATE acq.claim_event SET creator = dest_usr WHERE creator = src_usr;
425 DELETE FROM action.circulation WHERE usr = src_usr;
426 UPDATE action.circulation SET circ_staff = dest_usr WHERE circ_staff = src_usr;
427 UPDATE action.circulation SET checkin_staff = dest_usr WHERE checkin_staff = src_usr;
428 UPDATE action.hold_notification SET notify_staff = dest_usr WHERE notify_staff = src_usr;
429 UPDATE action.hold_request SET fulfillment_staff = dest_usr WHERE fulfillment_staff = src_usr;
430 UPDATE action.hold_request SET requestor = dest_usr WHERE requestor = src_usr;
431 DELETE FROM action.hold_request WHERE usr = src_usr;
432 UPDATE action.in_house_use SET staff = dest_usr WHERE staff = src_usr;
433 UPDATE action.non_cat_in_house_use SET staff = dest_usr WHERE staff = src_usr;
434 DELETE FROM action.non_cataloged_circulation WHERE patron = src_usr;
435 UPDATE action.non_cataloged_circulation SET staff = dest_usr WHERE staff = src_usr;
436 DELETE FROM action.survey_response WHERE usr = src_usr;
437 UPDATE action.fieldset SET owner = dest_usr WHERE owner = src_usr;
440 DELETE FROM actor.card WHERE usr = src_usr;
441 DELETE FROM actor.stat_cat_entry_usr_map WHERE target_usr = src_usr;
443 -- The following update is intended to avoid transient violations of a foreign
444 -- key constraint, whereby actor.usr_address references itself. It may not be
445 -- necessary, but it does no harm.
446 UPDATE actor.usr_address SET replaces = NULL
447 WHERE usr = src_usr AND replaces IS NOT NULL;
448 DELETE FROM actor.usr_address WHERE usr = src_usr;
449 DELETE FROM actor.usr_note WHERE usr = src_usr;
450 UPDATE actor.usr_note SET creator = dest_usr WHERE creator = src_usr;
451 DELETE FROM actor.usr_org_unit_opt_in WHERE usr = src_usr;
452 UPDATE actor.usr_org_unit_opt_in SET staff = dest_usr WHERE staff = src_usr;
453 DELETE FROM actor.usr_setting WHERE usr = src_usr;
454 DELETE FROM actor.usr_standing_penalty WHERE usr = src_usr;
455 UPDATE actor.usr_standing_penalty SET staff = dest_usr WHERE staff = src_usr;
458 UPDATE asset.call_number SET creator = dest_usr WHERE creator = src_usr;
459 UPDATE asset.call_number SET editor = dest_usr WHERE editor = src_usr;
460 UPDATE asset.call_number_note SET creator = dest_usr WHERE creator = src_usr;
461 UPDATE asset.copy SET creator = dest_usr WHERE creator = src_usr;
462 UPDATE asset.copy SET editor = dest_usr WHERE editor = src_usr;
463 UPDATE asset.copy_note SET creator = dest_usr WHERE creator = src_usr;
466 DELETE FROM auditor.actor_usr_address_history WHERE id = src_usr;
467 DELETE FROM auditor.actor_usr_history WHERE id = src_usr;
468 UPDATE auditor.asset_call_number_history SET creator = dest_usr WHERE creator = src_usr;
469 UPDATE auditor.asset_call_number_history SET editor = dest_usr WHERE editor = src_usr;
470 UPDATE auditor.asset_copy_history SET creator = dest_usr WHERE creator = src_usr;
471 UPDATE auditor.asset_copy_history SET editor = dest_usr WHERE editor = src_usr;
472 UPDATE auditor.biblio_record_entry_history SET creator = dest_usr WHERE creator = src_usr;
473 UPDATE auditor.biblio_record_entry_history SET editor = dest_usr WHERE editor = src_usr;
476 UPDATE biblio.record_entry SET creator = dest_usr WHERE creator = src_usr;
477 UPDATE biblio.record_entry SET editor = dest_usr WHERE editor = src_usr;
478 UPDATE biblio.record_note SET creator = dest_usr WHERE creator = src_usr;
479 UPDATE biblio.record_note SET editor = dest_usr WHERE editor = src_usr;
482 -- Update buckets with a rename to avoid collisions
485 FROM container.biblio_record_entry_bucket
486 WHERE owner = src_usr
488 suffix := ' (' || src_usr || ')';
491 UPDATE container.biblio_record_entry_bucket
492 SET owner = dest_usr, name = name || suffix
493 WHERE id = renamable_row.id;
494 EXCEPTION WHEN unique_violation THEN
495 suffix := suffix || ' ';
504 FROM container.call_number_bucket
505 WHERE owner = src_usr
507 suffix := ' (' || src_usr || ')';
510 UPDATE container.call_number_bucket
511 SET owner = dest_usr, name = name || suffix
512 WHERE id = renamable_row.id;
513 EXCEPTION WHEN unique_violation THEN
514 suffix := suffix || ' ';
523 FROM container.copy_bucket
524 WHERE owner = src_usr
526 suffix := ' (' || src_usr || ')';
529 UPDATE container.copy_bucket
530 SET owner = dest_usr, name = name || suffix
531 WHERE id = renamable_row.id;
532 EXCEPTION WHEN unique_violation THEN
533 suffix := suffix || ' ';
542 FROM container.user_bucket
543 WHERE owner = src_usr
545 suffix := ' (' || src_usr || ')';
548 UPDATE container.user_bucket
549 SET owner = dest_usr, name = name || suffix
550 WHERE id = renamable_row.id;
551 EXCEPTION WHEN unique_violation THEN
552 suffix := suffix || ' ';
559 DELETE FROM container.user_bucket_item WHERE target_user = src_usr;
562 DELETE FROM money.billable_xact WHERE usr = src_usr;
563 DELETE FROM money.collections_tracker WHERE usr = src_usr;
564 UPDATE money.collections_tracker SET collector = dest_usr WHERE collector = src_usr;
567 DELETE FROM permission.usr_grp_map WHERE usr = src_usr;
568 DELETE FROM permission.usr_object_perm_map WHERE usr = src_usr;
569 DELETE FROM permission.usr_perm_map WHERE usr = src_usr;
570 DELETE FROM permission.usr_work_ou_map WHERE usr = src_usr;
573 -- Update with a rename to avoid collisions
577 FROM reporter.output_folder
578 WHERE owner = src_usr
580 suffix := ' (' || src_usr || ')';
583 UPDATE reporter.output_folder
584 SET owner = dest_usr, name = name || suffix
585 WHERE id = renamable_row.id;
586 EXCEPTION WHEN unique_violation THEN
587 suffix := suffix || ' ';
593 EXCEPTION WHEN undefined_table THEN
598 UPDATE reporter.report SET owner = dest_usr WHERE owner = src_usr;
599 EXCEPTION WHEN undefined_table THEN
603 -- Update with a rename to avoid collisions
607 FROM reporter.report_folder
608 WHERE owner = src_usr
610 suffix := ' (' || src_usr || ')';
613 UPDATE reporter.report_folder
614 SET owner = dest_usr, name = name || suffix
615 WHERE id = renamable_row.id;
616 EXCEPTION WHEN unique_violation THEN
617 suffix := suffix || ' ';
623 EXCEPTION WHEN undefined_table THEN
628 UPDATE reporter.schedule SET runner = dest_usr WHERE runner = src_usr;
629 EXCEPTION WHEN undefined_table THEN
634 UPDATE reporter.template SET owner = dest_usr WHERE owner = src_usr;
635 EXCEPTION WHEN undefined_table THEN
639 -- Update with a rename to avoid collisions
643 FROM reporter.template_folder
644 WHERE owner = src_usr
646 suffix := ' (' || src_usr || ')';
649 UPDATE reporter.template_folder
650 SET owner = dest_usr, name = name || suffix
651 WHERE id = renamable_row.id;
652 EXCEPTION WHEN unique_violation THEN
653 suffix := suffix || ' ';
659 EXCEPTION WHEN undefined_table THEN
664 -- Update with a rename to avoid collisions
668 WHERE owner = src_usr
670 suffix := ' (' || src_usr || ')';
673 UPDATE vandelay.queue
674 SET owner = dest_usr, name = name || suffix
675 WHERE id = renamable_row.id;
676 EXCEPTION WHEN unique_violation THEN
677 suffix := suffix || ' ';
687 COMMENT ON FUNCTION actor.usr_purge_data(INT, INT) IS $$
689 * Finds rows dependent on a given row in actor.usr and either deletes them
690 * or reassigns them to a different user.
696 CREATE OR REPLACE FUNCTION actor.usr_delete(
701 old_profile actor.usr.profile%type;
702 old_home_ou actor.usr.home_ou%type;
703 new_profile actor.usr.profile%type;
704 new_home_ou actor.usr.home_ou%type;
706 new_dob actor.usr.dob%type;
709 id || '-PURGED-' || now(),
723 -- Quit if no such user
725 IF old_profile IS NULL THEN
729 perform actor.usr_purge_data( src_usr, dest_usr );
731 -- Find the root grp_tree and the root org_unit. This would be simpler if we
732 -- could assume that there is only one root. Theoretically, someday, maybe,
733 -- there could be multiple roots, so we take extra trouble to get the right ones.
740 permission.grp_ancestors( old_profile )
749 actor.org_unit_ancestors( old_home_ou )
753 -- Truncate date of birth
755 IF new_dob IS NOT NULL THEN
756 new_dob := date_trunc( 'year', new_dob );
763 profile = new_profile,
766 passwd = random()::text,
771 FROM config.identification_type
776 net_access_level = DEFAULT,
779 first_given_name = new_name,
780 second_given_name = NULL,
781 family_name = new_name,
785 evening_phone = NULL,
787 mailing_address = NULL,
788 billing_address = NULL,
789 home_ou = new_home_ou,
792 master_account = DEFAULT,
793 super_user = DEFAULT,
798 claims_returned_count = DEFAULT,
799 credit_forward_balance = DEFAULT,
800 last_xact_id = DEFAULT,
801 alert_message = NULL,
809 COMMENT ON FUNCTION actor.usr_delete(INT, INT) IS $$
811 * Logically deletes a user. Removes personally identifiable information,
812 * and purges associated data in other tables.
818 CREATE OR REPLACE FUNCTION actor.approve_pending_address(pending_id INT) RETURNS BIGINT AS $$
822 SELECT INTO old_id replaces FROM actor.usr_address where id = pending_id;
823 IF old_id IS NULL THEN
824 UPDATE actor.usr_address SET pending = 'f' WHERE id = pending_id;
827 -- address replaces an existing address
828 DELETE FROM actor.usr_address WHERE id = -old_id;
829 UPDATE actor.usr_address SET id = -id WHERE id = old_id;
830 UPDATE actor.usr_address SET replaces = NULL, id = old_id, pending = 'f' WHERE id = pending_id;
835 COMMENT ON FUNCTION actor.approve_pending_address(INT) IS $$
837 * Replaces an address with a pending address. This is done by giving the pending
838 * address the ID of the old address. The replaced address is retained with -id.
842 CREATE OR REPLACE FUNCTION container.clear_expired_circ_history_items(
846 -- Delete old circulation bucket items for a specified user.
847 -- "Old" means older than the interval specified by a
848 -- user-level setting, if it is so specified.
851 threshold TIMESTAMP WITH TIME ZONE;
854 IF ac_usr IS NULL THEN
857 -- Determine the threshold date that defines "old". Subtract the
858 -- interval from the system date, then truncate to midnight.
862 now() - CAST( translate( value, '"', '' ) AS INTERVAL )
870 AND name = 'patron.max_reading_list_interval';
872 IF threshold is null THEN
873 -- No interval defined; don't delete anything
874 -- RAISE NOTICE 'No interval defined for user %', ac_usr;
878 -- RAISE NOTICE 'Date threshold: %', threshold;
880 -- Threshold found; do the delete
881 delete from container.copy_bucket_item
888 container.copy_bucket
891 and btype = 'circ_history'
893 and create_time < threshold;
899 COMMENT ON FUNCTION container.clear_expired_circ_history_items( INTEGER ) IS $$
901 * Delete old circulation bucket items for a specified user.
902 * "Old" means older than the interval specified by a
903 * user-level setting, if it is so specified.
907 CREATE OR REPLACE FUNCTION container.clear_all_expired_circ_history_items( )
910 -- Delete expired circulation bucket items for all users that have
911 -- a setting for patron.max_reading_list_interval.
914 today TIMESTAMP WITH TIME ZONE;
915 threshold TIMESTAMP WITH TIME ZONE;
918 SELECT date_trunc( 'day', now() ) INTO today;
927 name = 'patron.max_reading_list_interval'
930 -- Make sure the setting is a valid interval
933 threshold := today - CAST( translate( usr_setting.value, '"', '' ) AS INTERVAL );
936 RAISE NOTICE 'Invalid setting patron.max_reading_list_interval for user %: ''%''',
937 usr_setting.usr, usr_setting.value;
941 --RAISE NOTICE 'User % threshold %', usr_setting.usr, threshold;
943 DELETE FROM container.copy_bucket_item
950 container.copy_bucket
952 owner = usr_setting.usr
953 AND btype = 'circ_history'
955 AND create_time < threshold;
961 COMMENT ON FUNCTION container.clear_all_expired_circ_history_items( ) IS $$
963 * Delete expired circulation bucket items for all users that have
964 * a setting for patron.max_reading_list_interval.
968 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
970 moved_objects INT := 0;
971 source_cn asset.call_number%ROWTYPE;
972 target_cn asset.call_number%ROWTYPE;
973 metarec metabib.metarecord%ROWTYPE;
974 hold action.hold_request%ROWTYPE;
975 ser_rec serial.record_entry%ROWTYPE;
982 -- move any 856 entries on records that have at least one MARC-mapped URI entry
983 SELECT INTO uri_count COUNT(*)
984 FROM asset.uri_call_number_map m
985 JOIN asset.call_number cn ON (m.call_number = cn.id)
986 WHERE cn.record = source_record;
988 IF uri_count > 0 THEN
990 SELECT COUNT(*) INTO counter
991 FROM oils_xpath_table(
994 'biblio.record_entry',
996 'id=' || source_record
997 ) as t(i int,c text);
999 FOR i IN 1 .. counter LOOP
1000 SELECT '<datafield xmlns="http://www.loc.gov/MARC21/slim"' ||
1002 ' ind1="' || FIRST(ind1) || '"' ||
1003 ' ind2="' || FIRST(ind2) || '">' ||
1006 '<subfield code="' || subfield || '">' ||
1009 regexp_replace(data,'&','&','g'),
1015 ) || '</datafield>' INTO uri_datafield
1016 FROM oils_xpath_table(
1019 'biblio.record_entry',
1020 '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
1021 '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
1022 '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
1023 '//*[@tag="856"][position()=' || i || ']/*[@code]',
1024 'id=' || source_record
1025 ) as t(id int,ind1 text, ind2 text,subfield text,data text);
1027 uri_text := uri_text || uri_datafield;
1030 IF uri_text <> '' THEN
1031 UPDATE biblio.record_entry
1032 SET marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
1033 WHERE id = target_record;
1038 -- Find and move metarecords to the target record
1039 SELECT INTO metarec *
1040 FROM metabib.metarecord
1041 WHERE master_record = source_record;
1044 UPDATE metabib.metarecord
1045 SET master_record = target_record,
1047 WHERE id = metarec.id;
1049 moved_objects := moved_objects + 1;
1052 -- Find call numbers attached to the source ...
1053 FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
1055 SELECT INTO target_cn *
1056 FROM asset.call_number
1057 WHERE label = source_cn.label
1058 AND owning_lib = source_cn.owning_lib
1059 AND record = target_record;
1061 -- ... and if there's a conflicting one on the target ...
1064 -- ... move the copies to that, and ...
1066 SET call_number = target_cn.id
1067 WHERE call_number = source_cn.id;
1069 -- ... move V holds to the move-target call number
1070 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
1072 UPDATE action.hold_request
1073 SET target = target_cn.id
1076 moved_objects := moved_objects + 1;
1081 -- ... just move the call number to the target record
1082 UPDATE asset.call_number
1083 SET record = target_record
1084 WHERE id = source_cn.id;
1087 moved_objects := moved_objects + 1;
1090 -- Find T holds targeting the source record ...
1091 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
1093 -- ... and move them to the target record
1094 UPDATE action.hold_request
1095 SET target = target_record
1098 moved_objects := moved_objects + 1;
1101 -- Find serial records targeting the source record ...
1102 FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
1103 -- ... and move them to the target record
1104 UPDATE serial.record_entry
1105 SET record = target_record
1106 WHERE id = ser_rec.id;
1108 moved_objects := moved_objects + 1;
1111 -- Finally, "delete" the source record
1112 DELETE FROM biblio.record_entry WHERE id = source_record;
1114 -- That's all, folks!
1115 RETURN moved_objects;
1117 $func$ LANGUAGE plpgsql;
1119 -- copy OPAC visibility materialized view
1120 CREATE OR REPLACE FUNCTION asset.refresh_opac_visible_copies_mat_view () RETURNS VOID AS $$
1122 TRUNCATE TABLE asset.opac_visible_copies;
1124 INSERT INTO asset.opac_visible_copies (id, circ_lib, record)
1125 SELECT cp.id, cp.circ_lib, cn.record
1127 JOIN asset.call_number cn ON (cn.id = cp.call_number)
1128 JOIN actor.org_unit a ON (cp.circ_lib = a.id)
1129 JOIN asset.copy_location cl ON (cp.location = cl.id)
1130 JOIN config.copy_status cs ON (cp.status = cs.id)
1131 JOIN biblio.record_entry b ON (cn.record = b.id)
1132 WHERE NOT cp.deleted
1141 COMMENT ON FUNCTION asset.refresh_opac_visible_copies_mat_view() IS $$
1142 Rebuild the copy OPAC visibility cache. Useful during migrations.
1145 CREATE OR REPLACE FUNCTION asset.cache_copy_visibility () RETURNS TRIGGER as $func$
1149 do_add BOOLEAN := false;
1150 do_remove BOOLEAN := false;
1153 INSERT INTO asset.opac_visible_copies (id, circ_lib, record)
1154 SELECT cp.id, cp.circ_lib, cn.record
1156 JOIN asset.call_number cn ON (cn.id = cp.call_number)
1157 JOIN actor.org_unit a ON (cp.circ_lib = a.id)
1158 JOIN asset.copy_location cl ON (cp.location = cl.id)
1159 JOIN config.copy_status cs ON (cp.status = cs.id)
1160 JOIN biblio.record_entry b ON (cn.record = b.id)
1161 WHERE NOT cp.deleted
1170 remove_query := $$ DELETE FROM asset.opac_visible_copies WHERE id IN ( SELECT id FROM asset.copy WHERE $$;
1172 IF TG_OP = 'INSERT' THEN
1174 IF TG_TABLE_NAME IN ('copy', 'unit') THEN
1175 add_query := add_query || 'AND cp.id = ' || NEW.id || ';';
1183 -- handle items first, since with circulation activity
1184 -- their statuses change frequently
1185 IF TG_TABLE_NAME IN ('copy', 'unit') THEN
1187 IF OLD.location <> NEW.location OR
1188 OLD.call_number <> NEW.call_number OR
1189 OLD.status <> NEW.status OR
1190 OLD.circ_lib <> NEW.circ_lib THEN
1191 -- any of these could change visibility, but
1192 -- we'll save some queries and not try to calculate
1193 -- the change directly
1198 IF OLD.deleted <> NEW.deleted THEN
1206 IF OLD.opac_visible <> NEW.opac_visible THEN
1207 IF OLD.opac_visible THEN
1209 ELSIF NOT do_remove THEN -- handle edge case where deleted item
1210 -- is also marked opac_visible
1218 DELETE FROM asset.opac_visible_copies WHERE id = NEW.id;
1221 add_query := add_query || 'AND cp.id = ' || NEW.id || ';';
1229 IF TG_TABLE_NAME IN ('call_number', 'record_entry') THEN -- these have a 'deleted' column
1231 IF OLD.deleted AND NEW.deleted THEN -- do nothing
1235 ELSIF NEW.deleted THEN -- remove rows
1237 IF TG_TABLE_NAME = 'call_number' THEN
1238 DELETE FROM asset.opac_visible_copies WHERE id IN (SELECT id FROM asset.copy WHERE call_number = NEW.id);
1239 ELSIF TG_TABLE_NAME = 'record_entry' THEN
1240 DELETE FROM asset.opac_visible_copies WHERE record = NEW.id;
1245 ELSIF OLD.deleted THEN -- add rows
1247 IF TG_TABLE_NAME IN ('copy','unit') THEN
1248 add_query := add_query || 'AND cp.id = ' || NEW.id || ';';
1249 ELSIF TG_TABLE_NAME = 'call_number' THEN
1250 add_query := add_query || 'AND cp.call_number = ' || NEW.id || ';';
1251 ELSIF TG_TABLE_NAME = 'record_entry' THEN
1252 add_query := add_query || 'AND cn.record = ' || NEW.id || ';';
1262 IF TG_TABLE_NAME = 'call_number' THEN
1264 IF OLD.record <> NEW.record THEN
1265 -- call number is linked to different bib
1266 remove_query := remove_query || 'call_number = ' || NEW.id || ');';
1267 EXECUTE remove_query;
1268 add_query := add_query || 'AND cp.call_number = ' || NEW.id || ';';
1276 IF TG_TABLE_NAME IN ('record_entry') THEN
1277 RETURN NEW; -- don't have 'opac_visible'
1280 -- actor.org_unit, asset.copy_location, asset.copy_status
1281 IF NEW.opac_visible = OLD.opac_visible THEN -- do nothing
1285 ELSIF NEW.opac_visible THEN -- add rows
1287 IF TG_TABLE_NAME = 'org_unit' THEN
1288 add_query := add_query || 'AND cp.circ_lib = ' || NEW.id || ';';
1289 ELSIF TG_TABLE_NAME = 'copy_location' THEN
1290 add_query := add_query || 'AND cp.location = ' || NEW.id || ';';
1291 ELSIF TG_TABLE_NAME = 'copy_status' THEN
1292 add_query := add_query || 'AND cp.status = ' || NEW.id || ';';
1299 IF TG_TABLE_NAME = 'org_unit' THEN
1300 remove_query := 'DELETE FROM asset.opac_visible_copies WHERE circ_lib = ' || NEW.id || ';';
1301 ELSIF TG_TABLE_NAME = 'copy_location' THEN
1302 remove_query := remove_query || 'location = ' || NEW.id || ');';
1303 ELSIF TG_TABLE_NAME = 'copy_status' THEN
1304 remove_query := remove_query || 'status = ' || NEW.id || ');';
1307 EXECUTE remove_query;
1313 $func$ LANGUAGE PLPGSQL;
1314 COMMENT ON FUNCTION asset.cache_copy_visibility() IS $$
1315 Trigger function to update the copy OPAC visiblity cache.
1317 CREATE TRIGGER a_opac_vis_mat_view_tgr AFTER INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE asset.cache_copy_visibility();
1318 CREATE TRIGGER a_opac_vis_mat_view_tgr AFTER INSERT OR UPDATE ON asset.copy FOR EACH ROW EXECUTE PROCEDURE asset.cache_copy_visibility();
1319 CREATE TRIGGER a_opac_vis_mat_view_tgr AFTER INSERT OR UPDATE ON asset.call_number FOR EACH ROW EXECUTE PROCEDURE asset.cache_copy_visibility();
1320 CREATE TRIGGER a_opac_vis_mat_view_tgr AFTER INSERT OR UPDATE ON asset.copy_location FOR EACH ROW EXECUTE PROCEDURE asset.cache_copy_visibility();
1321 CREATE TRIGGER a_opac_vis_mat_view_tgr AFTER INSERT OR UPDATE ON serial.unit FOR EACH ROW EXECUTE PROCEDURE asset.cache_copy_visibility();
1322 CREATE TRIGGER a_opac_vis_mat_view_tgr AFTER INSERT OR UPDATE ON config.copy_status FOR EACH ROW EXECUTE PROCEDURE asset.cache_copy_visibility();
1323 CREATE TRIGGER a_opac_vis_mat_view_tgr AFTER INSERT OR UPDATE ON actor.org_unit FOR EACH ROW EXECUTE PROCEDURE asset.cache_copy_visibility();
1325 -- Authority ingest routines
1326 CREATE OR REPLACE FUNCTION authority.propagate_changes (aid BIGINT, bid BIGINT) RETURNS BIGINT AS $func$
1327 UPDATE biblio.record_entry
1328 SET marc = vandelay.merge_record_xml( marc, authority.generate_overlay_template( $1 ) )
1331 $func$ LANGUAGE SQL;
1333 CREATE OR REPLACE FUNCTION authority.propagate_changes (aid BIGINT) RETURNS SETOF BIGINT AS $func$
1334 SELECT authority.propagate_changes( authority, bib ) FROM authority.bib_linking WHERE authority = $1;
1335 $func$ LANGUAGE SQL;
1337 CREATE OR REPLACE FUNCTION authority.flatten_marc ( TEXT ) RETURNS SETOF authority.full_rec AS $func$
1340 use MARC::File::XML (BinaryEncoding => 'UTF-8');
1343 my $r = MARC::Record->new_from_xml( $xml );
1345 return_next( { tag => 'LDR', value => $r->leader } );
1347 for my $f ( $r->fields ) {
1348 if ($f->is_control_field) {
1349 return_next({ tag => $f->tag, value => $f->data });
1351 for my $s ($f->subfields) {
1354 ind1 => $f->indicator(1),
1355 ind2 => $f->indicator(2),
1356 subfield => $s->[0],
1366 $func$ LANGUAGE PLPERLU;
1368 CREATE OR REPLACE FUNCTION authority.flatten_marc ( rid BIGINT ) RETURNS SETOF authority.full_rec AS $func$
1370 auth authority.record_entry%ROWTYPE;
1371 output authority.full_rec%ROWTYPE;
1374 SELECT INTO auth * FROM authority.record_entry WHERE id = rid;
1376 FOR field IN SELECT * FROM authority.flatten_marc( auth.marc ) LOOP
1377 output.record := rid;
1378 output.ind1 := field.ind1;
1379 output.ind2 := field.ind2;
1380 output.tag := field.tag;
1381 output.subfield := field.subfield;
1382 IF field.subfield IS NOT NULL THEN
1383 output.value := naco_normalize(field.value, field.subfield);
1385 output.value := field.value;
1388 CONTINUE WHEN output.value IS NULL;
1393 $func$ LANGUAGE PLPGSQL;
1395 -- authority.rec_descriptor appears to be unused currently
1396 CREATE OR REPLACE FUNCTION authority.reingest_authority_rec_descriptor( auth_id BIGINT ) RETURNS VOID AS $func$
1398 DELETE FROM authority.rec_descriptor WHERE record = auth_id;
1399 -- INSERT INTO authority.rec_descriptor (record, record_status, char_encoding)
1400 -- SELECT auth_id, ;
1404 $func$ LANGUAGE PLPGSQL;
1406 CREATE OR REPLACE FUNCTION authority.reingest_authority_full_rec( auth_id BIGINT ) RETURNS VOID AS $func$
1408 DELETE FROM authority.full_rec WHERE record = auth_id;
1409 INSERT INTO authority.full_rec (record, tag, ind1, ind2, subfield, value)
1410 SELECT record, tag, ind1, ind2, subfield, value FROM authority.flatten_marc( auth_id );
1414 $func$ LANGUAGE PLPGSQL;
1416 -- AFTER UPDATE OR INSERT trigger for authority.record_entry
1417 CREATE OR REPLACE FUNCTION authority.indexing_ingest_or_delete () RETURNS TRIGGER AS $func$
1420 IF NEW.deleted IS TRUE THEN -- If this authority is deleted
1421 DELETE FROM authority.bib_linking WHERE authority = NEW.id; -- Avoid updating fields in bibs that are no longer visible
1422 DELETE FROM authority.full_rec WHERE record = NEW.id; -- Avoid validating fields against deleted authority records
1423 -- Should remove matching $0 from controlled fields at the same time?
1424 RETURN NEW; -- and we're done
1427 IF TG_OP = 'UPDATE' THEN -- re-ingest?
1428 PERFORM * FROM config.internal_flag WHERE name = 'ingest.reingest.force_on_same_marc' AND enabled;
1430 IF NOT FOUND AND OLD.marc = NEW.marc THEN -- don't do anything if the MARC didn't change
1433 -- Propagate these updates to any linked bib records
1434 PERFORM authority.propagate_changes(NEW.id) FROM authority.record_entry WHERE id = NEW.id;
1437 -- Flatten and insert the afr data
1438 PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_full_rec' AND enabled;
1440 PERFORM authority.reingest_authority_full_rec(NEW.id);
1441 -- authority.rec_descriptor is not currently used
1442 -- PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_rec_descriptor' AND enabled;
1443 -- IF NOT FOUND THEN
1444 -- PERFORM authority.reingest_authority_rec_descriptor(NEW.id);
1450 $func$ LANGUAGE PLPGSQL;
1453 CREATE TRIGGER fingerprint_tgr BEFORE INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE biblio.fingerprint_trigger ('eng','BKS');
1454 CREATE TRIGGER aaa_indexing_ingest_or_delete AFTER INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE biblio.indexing_ingest_or_delete ();
1455 CREATE TRIGGER bbb_simple_rec_trigger AFTER INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE reporter.simple_rec_trigger ();
1457 CREATE TRIGGER aaa_auth_ingest_or_delete AFTER INSERT OR UPDATE ON authority.record_entry FOR EACH ROW EXECUTE PROCEDURE authority.indexing_ingest_or_delete ();
1459 -- Utility routines, callable via cstore
1461 CREATE OR REPLACE FUNCTION config.interval_to_seconds( interval_val INTERVAL )
1462 RETURNS INTEGER AS $$
1464 RETURN EXTRACT( EPOCH FROM interval_val );
1466 $$ LANGUAGE plpgsql;
1468 CREATE OR REPLACE FUNCTION config.interval_to_seconds( interval_string TEXT )
1469 RETURNS INTEGER AS $$
1471 RETURN config.interval_to_seconds( interval_string::INTERVAL );
1473 $$ LANGUAGE plpgsql;