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;
439 DELETE FROM actor.card WHERE usr = src_usr;
440 DELETE FROM actor.stat_cat_entry_usr_map WHERE target_usr = src_usr;
442 -- The following update is intended to avoid transient violations of a foreign
443 -- key constraint, whereby actor.usr_address references itself. It may not be
444 -- necessary, but it does no harm.
445 UPDATE actor.usr_address SET replaces = NULL
446 WHERE usr = src_usr AND replaces IS NOT NULL;
447 DELETE FROM actor.usr_address WHERE usr = src_usr;
448 DELETE FROM actor.usr_note WHERE usr = src_usr;
449 UPDATE actor.usr_note SET creator = dest_usr WHERE creator = src_usr;
450 DELETE FROM actor.usr_org_unit_opt_in WHERE usr = src_usr;
451 UPDATE actor.usr_org_unit_opt_in SET staff = dest_usr WHERE staff = src_usr;
452 DELETE FROM actor.usr_setting WHERE usr = src_usr;
453 DELETE FROM actor.usr_standing_penalty WHERE usr = src_usr;
454 UPDATE actor.usr_standing_penalty SET staff = dest_usr WHERE staff = src_usr;
457 UPDATE asset.call_number SET creator = dest_usr WHERE creator = src_usr;
458 UPDATE asset.call_number SET editor = dest_usr WHERE editor = src_usr;
459 UPDATE asset.call_number_note SET creator = dest_usr WHERE creator = src_usr;
460 UPDATE asset.copy SET creator = dest_usr WHERE creator = src_usr;
461 UPDATE asset.copy SET editor = dest_usr WHERE editor = src_usr;
462 UPDATE asset.copy_note SET creator = dest_usr WHERE creator = src_usr;
465 DELETE FROM auditor.actor_usr_address_history WHERE id = src_usr;
466 DELETE FROM auditor.actor_usr_history WHERE id = src_usr;
467 UPDATE auditor.asset_call_number_history SET creator = dest_usr WHERE creator = src_usr;
468 UPDATE auditor.asset_call_number_history SET editor = dest_usr WHERE editor = src_usr;
469 UPDATE auditor.asset_copy_history SET creator = dest_usr WHERE creator = src_usr;
470 UPDATE auditor.asset_copy_history SET editor = dest_usr WHERE editor = src_usr;
471 UPDATE auditor.biblio_record_entry_history SET creator = dest_usr WHERE creator = src_usr;
472 UPDATE auditor.biblio_record_entry_history SET editor = dest_usr WHERE editor = src_usr;
475 UPDATE biblio.record_entry SET creator = dest_usr WHERE creator = src_usr;
476 UPDATE biblio.record_entry SET editor = dest_usr WHERE editor = src_usr;
477 UPDATE biblio.record_note SET creator = dest_usr WHERE creator = src_usr;
478 UPDATE biblio.record_note SET editor = dest_usr WHERE editor = src_usr;
481 -- Update buckets with a rename to avoid collisions
484 FROM container.biblio_record_entry_bucket
485 WHERE owner = src_usr
487 suffix := ' (' || src_usr || ')';
490 UPDATE container.biblio_record_entry_bucket
491 SET owner = dest_usr, name = name || suffix
492 WHERE id = renamable_row.id;
493 EXCEPTION WHEN unique_violation THEN
494 suffix := suffix || ' ';
503 FROM container.call_number_bucket
504 WHERE owner = src_usr
506 suffix := ' (' || src_usr || ')';
509 UPDATE container.call_number_bucket
510 SET owner = dest_usr, name = name || suffix
511 WHERE id = renamable_row.id;
512 EXCEPTION WHEN unique_violation THEN
513 suffix := suffix || ' ';
522 FROM container.copy_bucket
523 WHERE owner = src_usr
525 suffix := ' (' || src_usr || ')';
528 UPDATE container.copy_bucket
529 SET owner = dest_usr, name = name || suffix
530 WHERE id = renamable_row.id;
531 EXCEPTION WHEN unique_violation THEN
532 suffix := suffix || ' ';
541 FROM container.user_bucket
542 WHERE owner = src_usr
544 suffix := ' (' || src_usr || ')';
547 UPDATE container.user_bucket
548 SET owner = dest_usr, name = name || suffix
549 WHERE id = renamable_row.id;
550 EXCEPTION WHEN unique_violation THEN
551 suffix := suffix || ' ';
558 DELETE FROM container.user_bucket_item WHERE target_user = src_usr;
561 DELETE FROM money.billable_xact WHERE usr = src_usr;
562 DELETE FROM money.collections_tracker WHERE usr = src_usr;
563 UPDATE money.collections_tracker SET collector = dest_usr WHERE collector = src_usr;
566 DELETE FROM permission.usr_grp_map WHERE usr = src_usr;
567 DELETE FROM permission.usr_object_perm_map WHERE usr = src_usr;
568 DELETE FROM permission.usr_perm_map WHERE usr = src_usr;
569 DELETE FROM permission.usr_work_ou_map WHERE usr = src_usr;
572 -- Update with a rename to avoid collisions
576 FROM reporter.output_folder
577 WHERE owner = src_usr
579 suffix := ' (' || src_usr || ')';
582 UPDATE reporter.output_folder
583 SET owner = dest_usr, name = name || suffix
584 WHERE id = renamable_row.id;
585 EXCEPTION WHEN unique_violation THEN
586 suffix := suffix || ' ';
592 EXCEPTION WHEN undefined_table THEN
597 UPDATE reporter.report SET owner = dest_usr WHERE owner = src_usr;
598 EXCEPTION WHEN undefined_table THEN
602 -- Update with a rename to avoid collisions
606 FROM reporter.report_folder
607 WHERE owner = src_usr
609 suffix := ' (' || src_usr || ')';
612 UPDATE reporter.report_folder
613 SET owner = dest_usr, name = name || suffix
614 WHERE id = renamable_row.id;
615 EXCEPTION WHEN unique_violation THEN
616 suffix := suffix || ' ';
622 EXCEPTION WHEN undefined_table THEN
627 UPDATE reporter.schedule SET runner = dest_usr WHERE runner = src_usr;
628 EXCEPTION WHEN undefined_table THEN
633 UPDATE reporter.template SET owner = dest_usr WHERE owner = src_usr;
634 EXCEPTION WHEN undefined_table THEN
638 -- Update with a rename to avoid collisions
642 FROM reporter.template_folder
643 WHERE owner = src_usr
645 suffix := ' (' || src_usr || ')';
648 UPDATE reporter.template_folder
649 SET owner = dest_usr, name = name || suffix
650 WHERE id = renamable_row.id;
651 EXCEPTION WHEN unique_violation THEN
652 suffix := suffix || ' ';
658 EXCEPTION WHEN undefined_table THEN
663 -- Update with a rename to avoid collisions
667 WHERE owner = src_usr
669 suffix := ' (' || src_usr || ')';
672 UPDATE vandelay.queue
673 SET owner = dest_usr, name = name || suffix
674 WHERE id = renamable_row.id;
675 EXCEPTION WHEN unique_violation THEN
676 suffix := suffix || ' ';
686 COMMENT ON FUNCTION actor.usr_purge_data(INT, INT) IS $$
688 * Finds rows dependent on a given row in actor.usr and either deletes them
689 * or reassigns them to a different user.
695 CREATE OR REPLACE FUNCTION actor.usr_delete(
700 old_profile actor.usr.profile%type;
701 old_home_ou actor.usr.home_ou%type;
702 new_profile actor.usr.profile%type;
703 new_home_ou actor.usr.home_ou%type;
705 new_dob actor.usr.dob%type;
708 id || '-PURGED-' || now(),
722 -- Quit if no such user
724 IF old_profile IS NULL THEN
728 perform actor.usr_purge_data( src_usr, dest_usr );
730 -- Find the root grp_tree and the root org_unit. This would be simpler if we
731 -- could assume that there is only one root. Theoretically, someday, maybe,
732 -- there could be multiple roots, so we take extra trouble to get the right ones.
739 permission.grp_ancestors( old_profile )
748 actor.org_unit_ancestors( old_home_ou )
752 -- Truncate date of birth
754 IF new_dob IS NOT NULL THEN
755 new_dob := date_trunc( 'year', new_dob );
762 profile = new_profile,
765 passwd = random()::text,
770 FROM config.identification_type
775 net_access_level = DEFAULT,
778 first_given_name = new_name,
779 second_given_name = NULL,
780 family_name = new_name,
784 evening_phone = NULL,
786 mailing_address = NULL,
787 billing_address = NULL,
788 home_ou = new_home_ou,
791 master_account = DEFAULT,
792 super_user = DEFAULT,
797 claims_returned_count = DEFAULT,
798 credit_forward_balance = DEFAULT,
799 last_xact_id = DEFAULT,
800 alert_message = NULL,
808 COMMENT ON FUNCTION actor.usr_delete(INT, INT) IS $$
810 * Logically deletes a user. Removes personally identifiable information,
811 * and purges associated data in other tables.
817 CREATE OR REPLACE FUNCTION actor.approve_pending_address(pending_id INT) RETURNS BIGINT AS $$
821 SELECT INTO old_id replaces FROM actor.usr_address where id = pending_id;
822 IF old_id IS NULL THEN
823 UPDATE actor.usr_address SET pending = 'f' WHERE id = pending_id;
826 -- address replaces an existing address
827 DELETE FROM actor.usr_address WHERE id = -old_id;
828 UPDATE actor.usr_address SET id = -id WHERE id = old_id;
829 UPDATE actor.usr_address SET replaces = NULL, id = old_id, pending = 'f' WHERE id = pending_id;
834 COMMENT ON FUNCTION actor.approve_pending_address(INT) IS $$
836 * Replaces an address with a pending address. This is done by giving the pending
837 * address the ID of the old address. The replaced address is retained with -id.
841 CREATE OR REPLACE FUNCTION container.clear_expired_circ_history_items(
845 -- Delete old circulation bucket items for a specified user.
846 -- "Old" means older than the interval specified by a
847 -- user-level setting, if it is so specified.
850 threshold TIMESTAMP WITH TIME ZONE;
853 IF ac_usr IS NULL THEN
856 -- Determine the threshold date that defines "old". Subtract the
857 -- interval from the system date, then truncate to midnight.
861 now() - CAST( translate( value, '"', '' ) AS INTERVAL )
869 AND name = 'patron.max_reading_list_interval';
871 IF threshold is null THEN
872 -- No interval defined; don't delete anything
873 -- RAISE NOTICE 'No interval defined for user %', ac_usr;
877 -- RAISE NOTICE 'Date threshold: %', threshold;
879 -- Threshold found; do the delete
880 delete from container.copy_bucket_item
887 container.copy_bucket
890 and btype = 'circ_history'
892 and create_time < threshold;
898 COMMENT ON FUNCTION container.clear_expired_circ_history_items( INTEGER ) IS $$
900 * Delete old circulation bucket items for a specified user.
901 * "Old" means older than the interval specified by a
902 * user-level setting, if it is so specified.
906 CREATE OR REPLACE FUNCTION container.clear_all_expired_circ_history_items( )
909 -- Delete expired circulation bucket items for all users that have
910 -- a setting for patron.max_reading_list_interval.
913 today TIMESTAMP WITH TIME ZONE;
914 threshold TIMESTAMP WITH TIME ZONE;
917 SELECT date_trunc( 'day', now() ) INTO today;
926 name = 'patron.max_reading_list_interval'
929 -- Make sure the setting is a valid interval
932 threshold := today - CAST( translate( usr_setting.value, '"', '' ) AS INTERVAL );
935 RAISE NOTICE 'Invalid setting patron.max_reading_list_interval for user %: ''%''',
936 usr_setting.usr, usr_setting.value;
940 --RAISE NOTICE 'User % threshold %', usr_setting.usr, threshold;
942 DELETE FROM container.copy_bucket_item
949 container.copy_bucket
951 owner = usr_setting.usr
952 AND btype = 'circ_history'
954 AND create_time < threshold;
960 COMMENT ON FUNCTION container.clear_all_expired_circ_history_items( ) IS $$
962 * Delete expired circulation bucket items for all users that have
963 * a setting for patron.max_reading_list_interval.
967 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
969 moved_objects INT := 0;
970 source_cn asset.call_number%ROWTYPE;
971 target_cn asset.call_number%ROWTYPE;
972 metarec metabib.metarecord%ROWTYPE;
973 hold action.hold_request%ROWTYPE;
974 ser_rec serial.record_entry%ROWTYPE;
981 -- move any 856 entries on records that have at least one MARC-mapped URI entry
982 SELECT INTO uri_count COUNT(*)
983 FROM asset.uri_call_number_map m
984 JOIN asset.call_number cn ON (m.call_number = cn.id)
985 WHERE cn.record = source_record;
987 IF uri_count > 0 THEN
989 SELECT COUNT(*) INTO counter
990 FROM oils_xpath_table(
993 'biblio.record_entry',
996 ) as t(i int,c text);
998 FOR i IN 1 .. counter LOOP
999 SELECT '<datafield xmlns="http://www.loc.gov/MARC21/slim" tag="856">' ||
1002 '<subfield code="' || subfield || '">' ||
1005 regexp_replace(data,'&','&','g'),
1011 ) || '</datafield>' INTO uri_datafield
1012 FROM oils_xpath_table(
1015 'biblio.record_entry',
1016 '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
1017 '//*[@tag="856"][position()=' || i || ']/*[@code]',
1018 'id=' || source_record
1019 ) as t(id int,subfield text,data text);
1021 uri_text := uri_text || uri_datafield;
1024 IF uri_text <> '' THEN
1025 UPDATE biblio.record_entry
1026 SET marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
1027 WHERE id = target_record;
1032 -- Find and move metarecords to the target record
1033 SELECT INTO metarec *
1034 FROM metabib.metarecord
1035 WHERE master_record = source_record;
1038 UPDATE metabib.metarecord
1039 SET master_record = target_record,
1041 WHERE id = metarec.id;
1043 moved_objects := moved_objects + 1;
1046 -- Find call numbers attached to the source ...
1047 FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
1049 SELECT INTO target_cn *
1050 FROM asset.call_number
1051 WHERE label = source_cn.label
1052 AND owning_lib = source_cn.owning_lib
1053 AND record = target_record;
1055 -- ... and if there's a conflicting one on the target ...
1058 -- ... move the copies to that, and ...
1060 SET call_number = target_cn.id
1061 WHERE call_number = source_cn.id;
1063 -- ... move V holds to the move-target call number
1064 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
1066 UPDATE action.hold_request
1067 SET target = target_cn.id
1070 moved_objects := moved_objects + 1;
1075 -- ... just move the call number to the target record
1076 UPDATE asset.call_number
1077 SET record = target_record
1078 WHERE id = source_cn.id;
1081 moved_objects := moved_objects + 1;
1084 -- Find T holds targeting the source record ...
1085 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
1087 -- ... and move them to the target record
1088 UPDATE action.hold_request
1089 SET target = target_record
1092 moved_objects := moved_objects + 1;
1095 -- Find serial records targeting the source record ...
1096 FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
1097 -- ... and move them to the target record
1098 UPDATE serial.record_entry
1099 SET record = target_record
1100 WHERE id = ser_rec.id;
1102 moved_objects := moved_objects + 1;
1105 -- Finally, "delete" the source record
1106 DELETE FROM biblio.record_entry WHERE id = source_record;
1108 -- That's all, folks!
1109 RETURN moved_objects;
1111 $func$ LANGUAGE plpgsql;
1113 -- copy OPAC visibility materialized view
1114 CREATE OR REPLACE FUNCTION asset.refresh_opac_visible_copies_mat_view () RETURNS VOID AS $$
1116 TRUNCATE TABLE asset.opac_visible_copies;
1118 INSERT INTO asset.opac_visible_copies (id, circ_lib, record)
1119 SELECT cp.id, cp.circ_lib, cn.record
1121 JOIN asset.call_number cn ON (cn.id = cp.call_number)
1122 JOIN actor.org_unit a ON (cp.circ_lib = a.id)
1123 JOIN asset.copy_location cl ON (cp.location = cl.id)
1124 JOIN config.copy_status cs ON (cp.status = cs.id)
1125 JOIN biblio.record_entry b ON (cn.record = b.id)
1126 WHERE NOT cp.deleted
1135 COMMENT ON FUNCTION asset.refresh_opac_visible_copies_mat_view() IS $$
1136 Rebuild the copy OPAC visibility cache. Useful during migrations.
1139 CREATE OR REPLACE FUNCTION asset.cache_copy_visibility () RETURNS TRIGGER as $func$
1143 do_add BOOLEAN := false;
1144 do_remove BOOLEAN := false;
1147 INSERT INTO asset.opac_visible_copies (id, circ_lib, record)
1148 SELECT cp.id, cp.circ_lib, cn.record
1150 JOIN asset.call_number cn ON (cn.id = cp.call_number)
1151 JOIN actor.org_unit a ON (cp.circ_lib = a.id)
1152 JOIN asset.copy_location cl ON (cp.location = cl.id)
1153 JOIN config.copy_status cs ON (cp.status = cs.id)
1154 JOIN biblio.record_entry b ON (cn.record = b.id)
1155 WHERE NOT cp.deleted
1164 remove_query := $$ DELETE FROM asset.opac_visible_copies WHERE id IN ( SELECT id FROM asset.copy WHERE $$;
1166 IF TG_OP = 'INSERT' THEN
1168 IF TG_TABLE_NAME IN ('copy', 'unit') THEN
1169 add_query := add_query || 'AND cp.id = ' || NEW.id || ';';
1177 -- handle items first, since with circulation activity
1178 -- their statuses change frequently
1179 IF TG_TABLE_NAME IN ('copy', 'unit') THEN
1181 IF OLD.location <> NEW.location OR
1182 OLD.call_number <> NEW.call_number OR
1183 OLD.status <> NEW.status OR
1184 OLD.circ_lib <> NEW.circ_lib THEN
1185 -- any of these could change visibility, but
1186 -- we'll save some queries and not try to calculate
1187 -- the change directly
1192 IF OLD.deleted <> NEW.deleted THEN
1200 IF OLD.opac_visible <> NEW.opac_visible THEN
1201 IF OLD.opac_visible THEN
1203 ELSIF NOT do_remove THEN -- handle edge case where deleted item
1204 -- is also marked opac_visible
1212 DELETE FROM asset.opac_visible_copies WHERE id = NEW.id;
1215 add_query := add_query || 'AND cp.id = ' || NEW.id || ';';
1223 IF TG_TABLE_NAME IN ('call_number', 'record_entry') THEN -- these have a 'deleted' column
1225 IF OLD.deleted AND NEW.deleted THEN -- do nothing
1229 ELSIF NEW.deleted THEN -- remove rows
1231 IF TG_TABLE_NAME = 'call_number' THEN
1232 DELETE FROM asset.opac_visible_copies WHERE id IN (SELECT id FROM asset.copy WHERE call_number = NEW.id);
1233 ELSIF TG_TABLE_NAME = 'record_entry' THEN
1234 DELETE FROM asset.opac_visible_copies WHERE record = NEW.id;
1239 ELSIF OLD.deleted THEN -- add rows
1241 IF TG_TABLE_NAME IN ('copy','unit') THEN
1242 add_query := add_query || 'AND cp.id = ' || NEW.id || ';';
1243 ELSIF TG_TABLE_NAME = 'call_number' THEN
1244 add_query := add_query || 'AND cp.call_number = ' || NEW.id || ';';
1245 ELSIF TG_TABLE_NAME = 'record_entry' THEN
1246 add_query := add_query || 'AND cn.record = ' || NEW.id || ';';
1256 IF TG_TABLE_NAME = 'call_number' THEN
1258 IF OLD.record <> NEW.record THEN
1259 -- call number is linked to different bib
1260 remove_query := remove_query || 'call_number = ' || NEW.id || ');';
1261 EXECUTE remove_query;
1262 add_query := add_query || 'AND cp.call_number = ' || NEW.id || ';';
1270 IF TG_TABLE_NAME IN ('record_entry') THEN
1271 RETURN NEW; -- don't have 'opac_visible'
1274 -- actor.org_unit, asset.copy_location, asset.copy_status
1275 IF NEW.opac_visible = OLD.opac_visible THEN -- do nothing
1279 ELSIF NEW.opac_visible THEN -- add rows
1281 IF TG_TABLE_NAME = 'org_unit' THEN
1282 add_query := add_query || 'AND cp.circ_lib = ' || NEW.id || ';';
1283 ELSIF TG_TABLE_NAME = 'copy_location' THEN
1284 add_query := add_query || 'AND cp.location = ' || NEW.id || ';';
1285 ELSIF TG_TABLE_NAME = 'copy_status' THEN
1286 add_query := add_query || 'AND cp.status = ' || NEW.id || ';';
1293 IF TG_TABLE_NAME = 'org_unit' THEN
1294 remove_query := 'DELETE FROM asset.opac_visible_copies WHERE circ_lib = ' || NEW.id || ';';
1295 ELSIF TG_TABLE_NAME = 'copy_location' THEN
1296 remove_query := remove_query || 'location = ' || NEW.id || ');';
1297 ELSIF TG_TABLE_NAME = 'copy_status' THEN
1298 remove_query := remove_query || 'status = ' || NEW.id || ');';
1301 EXECUTE remove_query;
1307 $func$ LANGUAGE PLPGSQL;
1308 COMMENT ON FUNCTION asset.cache_copy_visibility() IS $$
1309 Trigger function to update the copy OPAC visiblity cache.
1311 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();
1312 CREATE TRIGGER a_opac_vis_mat_view_tgr AFTER INSERT OR UPDATE ON asset.copy FOR EACH ROW EXECUTE PROCEDURE asset.cache_copy_visibility();
1313 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();
1314 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();
1315 CREATE TRIGGER a_opac_vis_mat_view_tgr AFTER INSERT OR UPDATE ON serial.unit FOR EACH ROW EXECUTE PROCEDURE asset.cache_copy_visibility();
1316 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();
1317 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();
1319 -- Authority ingest routines
1320 CREATE OR REPLACE FUNCTION authority.propagate_changes (aid BIGINT, bid BIGINT) RETURNS BIGINT AS $func$
1321 UPDATE biblio.record_entry
1322 SET marc = vandelay.merge_record_xml( marc, authority.generate_overlay_template( $1 ) )
1325 $func$ LANGUAGE SQL;
1327 CREATE OR REPLACE FUNCTION authority.propagate_changes (aid BIGINT) RETURNS SETOF BIGINT AS $func$
1328 SELECT authority.propagate_changes( authority, bib ) FROM authority.bib_linking WHERE authority = $1;
1329 $func$ LANGUAGE SQL;
1331 CREATE OR REPLACE FUNCTION authority.flatten_marc ( TEXT ) RETURNS SETOF authority.full_rec AS $func$
1334 use MARC::File::XML (BinaryEncoding => 'UTF-8');
1337 my $r = MARC::Record->new_from_xml( $xml );
1339 return_next( { tag => 'LDR', value => $r->leader } );
1341 for my $f ( $r->fields ) {
1342 if ($f->is_control_field) {
1343 return_next({ tag => $f->tag, value => $f->data });
1345 for my $s ($f->subfields) {
1348 ind1 => $f->indicator(1),
1349 ind2 => $f->indicator(2),
1350 subfield => $s->[0],
1360 $func$ LANGUAGE PLPERLU;
1362 CREATE OR REPLACE FUNCTION authority.flatten_marc ( rid BIGINT ) RETURNS SETOF authority.full_rec AS $func$
1364 auth authority.record_entry%ROWTYPE;
1365 output authority.full_rec%ROWTYPE;
1368 SELECT INTO auth * FROM authority.record_entry WHERE id = rid;
1370 FOR field IN SELECT * FROM authority.flatten_marc( auth.marc ) LOOP
1371 output.record := rid;
1372 output.ind1 := field.ind1;
1373 output.ind2 := field.ind2;
1374 output.tag := field.tag;
1375 output.subfield := field.subfield;
1376 IF field.subfield IS NOT NULL THEN
1377 output.value := naco_normalize(field.value, field.subfield);
1379 output.value := field.value;
1382 CONTINUE WHEN output.value IS NULL;
1387 $func$ LANGUAGE PLPGSQL;
1389 -- authority.rec_descriptor appears to be unused currently
1390 CREATE OR REPLACE FUNCTION authority.reingest_authority_rec_descriptor( auth_id BIGINT ) RETURNS VOID AS $func$
1392 DELETE FROM authority.rec_descriptor WHERE record = auth_id;
1393 -- INSERT INTO authority.rec_descriptor (record, record_status, char_encoding)
1394 -- SELECT auth_id, ;
1398 $func$ LANGUAGE PLPGSQL;
1400 CREATE OR REPLACE FUNCTION authority.reingest_authority_full_rec( auth_id BIGINT ) RETURNS VOID AS $func$
1402 DELETE FROM authority.full_rec WHERE record = auth_id;
1403 INSERT INTO authority.full_rec (record, tag, ind1, ind2, subfield, value)
1404 SELECT record, tag, ind1, ind2, subfield, value FROM authority.flatten_marc( auth_id );
1408 $func$ LANGUAGE PLPGSQL;
1410 -- AFTER UPDATE OR INSERT trigger for authority.record_entry
1411 CREATE OR REPLACE FUNCTION authority.indexing_ingest_or_delete () RETURNS TRIGGER AS $func$
1414 IF NEW.deleted IS TRUE THEN -- If this authority is deleted
1415 DELETE FROM authority.bib_linking WHERE authority = NEW.id; -- Avoid updating fields in bibs that are no longer visible
1416 -- Should remove matching $0 from controlled fields at the same time?
1417 RETURN NEW; -- and we're done
1420 IF TG_OP = 'UPDATE' THEN -- re-ingest?
1421 PERFORM * FROM config.internal_flag WHERE name = 'ingest.reingest.force_on_same_marc' AND enabled;
1423 IF NOT FOUND AND OLD.marc = NEW.marc THEN -- don't do anything if the MARC didn't change
1428 -- Flatten and insert the afr data
1429 PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_full_rec' AND enabled;
1431 PERFORM authority.reingest_authority_full_rec(NEW.id);
1432 -- authority.rec_descriptor is not currently used
1433 -- PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_rec_descriptor' AND enabled;
1434 -- IF NOT FOUND THEN
1435 -- PERFORM authority.reingest_authority_rec_descriptor(NEW.id);
1441 $func$ LANGUAGE PLPGSQL;
1444 CREATE TRIGGER fingerprint_tgr BEFORE INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE biblio.fingerprint_trigger ('eng','BKS');
1445 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 ();
1446 CREATE TRIGGER bbb_simple_rec_trigger AFTER INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE reporter.simple_rec_trigger ();
1448 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 ();
1450 -- Utility routines, callable via cstore
1452 CREATE OR REPLACE FUNCTION config.interval_to_seconds( interval_val INTERVAL )
1453 RETURNS INTEGER AS $$
1455 RETURN EXTRACT( EPOCH FROM interval_val );
1457 $$ LANGUAGE plpgsql;
1459 CREATE OR REPLACE FUNCTION config.interval_to_seconds( interval_string TEXT )
1460 RETURNS INTEGER AS $$
1462 RETURN config.interval_to_seconds( interval_string::INTERVAL );
1464 $$ LANGUAGE plpgsql;