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.
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',
997 ) as t(i int,c text);
999 FOR i IN 1 .. counter LOOP
1000 SELECT '<datafield xmlns="http://www.loc.gov/MARC21/slim" tag="856">' ||
1003 '<subfield code="' || subfield || '">' ||
1006 regexp_replace(data,'&','&','g'),
1012 ) || '</datafield>' INTO uri_datafield
1013 FROM oils_xpath_table(
1016 'biblio.record_entry',
1017 '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
1018 '//*[@tag="856"][position()=' || i || ']/*[@code]',
1019 'id=' || source_record
1020 ) as t(id int,subfield text,data text);
1022 uri_text := uri_text || uri_datafield;
1025 IF uri_text <> '' THEN
1026 UPDATE biblio.record_entry
1027 SET marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
1028 WHERE id = target_record;
1033 -- Find and move metarecords to the target record
1034 SELECT INTO metarec *
1035 FROM metabib.metarecord
1036 WHERE master_record = source_record;
1039 UPDATE metabib.metarecord
1040 SET master_record = target_record,
1042 WHERE id = metarec.id;
1044 moved_objects := moved_objects + 1;
1047 -- Find call numbers attached to the source ...
1048 FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
1050 SELECT INTO target_cn *
1051 FROM asset.call_number
1052 WHERE label = source_cn.label
1053 AND owning_lib = source_cn.owning_lib
1054 AND record = target_record;
1056 -- ... and if there's a conflicting one on the target ...
1059 -- ... move the copies to that, and ...
1061 SET call_number = target_cn.id
1062 WHERE call_number = source_cn.id;
1064 -- ... move V holds to the move-target call number
1065 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
1067 UPDATE action.hold_request
1068 SET target = target_cn.id
1071 moved_objects := moved_objects + 1;
1076 -- ... just move the call number to the target record
1077 UPDATE asset.call_number
1078 SET record = target_record
1079 WHERE id = source_cn.id;
1082 moved_objects := moved_objects + 1;
1085 -- Find T holds targeting the source record ...
1086 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
1088 -- ... and move them to the target record
1089 UPDATE action.hold_request
1090 SET target = target_record
1093 moved_objects := moved_objects + 1;
1096 -- Find serial records targeting the source record ...
1097 FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
1098 -- ... and move them to the target record
1099 UPDATE serial.record_entry
1100 SET record = target_record
1101 WHERE id = ser_rec.id;
1103 moved_objects := moved_objects + 1;
1106 -- Find and move authority->bib links to the target record
1107 FOR auth_link IN SELECT * FROM authority.bib_linking WHERE bib = source_record LOOP
1108 UPDATE authority.bib_linking
1109 SET bib = target_record
1110 WHERE id = auth_link.id;
1112 moved_objects := moved_objects + 1;
1115 -- Finally, "delete" the source record
1116 DELETE FROM biblio.record_entry WHERE id = source_record;
1118 -- That's all, folks!
1119 RETURN moved_objects;
1121 $func$ LANGUAGE plpgsql;
1123 -- copy OPAC visibility materialized view
1124 CREATE OR REPLACE FUNCTION asset.refresh_opac_visible_copies_mat_view () RETURNS VOID AS $$
1126 TRUNCATE TABLE asset.opac_visible_copies;
1128 INSERT INTO asset.opac_visible_copies (id, circ_lib, record)
1129 SELECT cp.id, cp.circ_lib, cn.record
1131 JOIN asset.call_number cn ON (cn.id = cp.call_number)
1132 JOIN actor.org_unit a ON (cp.circ_lib = a.id)
1133 JOIN asset.copy_location cl ON (cp.location = cl.id)
1134 JOIN config.copy_status cs ON (cp.status = cs.id)
1135 JOIN biblio.record_entry b ON (cn.record = b.id)
1136 WHERE NOT cp.deleted
1145 COMMENT ON FUNCTION asset.refresh_opac_visible_copies_mat_view() IS $$
1146 Rebuild the copy OPAC visibility cache. Useful during migrations.
1149 CREATE OR REPLACE FUNCTION asset.cache_copy_visibility () RETURNS TRIGGER as $func$
1153 do_add BOOLEAN := false;
1154 do_remove BOOLEAN := false;
1157 INSERT INTO asset.opac_visible_copies (id, circ_lib, record)
1158 SELECT cp.id, cp.circ_lib, cn.record
1160 JOIN asset.call_number cn ON (cn.id = cp.call_number)
1161 JOIN actor.org_unit a ON (cp.circ_lib = a.id)
1162 JOIN asset.copy_location cl ON (cp.location = cl.id)
1163 JOIN config.copy_status cs ON (cp.status = cs.id)
1164 JOIN biblio.record_entry b ON (cn.record = b.id)
1165 WHERE NOT cp.deleted
1174 remove_query := $$ DELETE FROM asset.opac_visible_copies WHERE id IN ( SELECT id FROM asset.copy WHERE $$;
1176 IF TG_OP = 'INSERT' THEN
1178 IF TG_TABLE_NAME IN ('copy', 'unit') THEN
1179 add_query := add_query || 'AND cp.id = ' || NEW.id || ';';
1187 -- handle items first, since with circulation activity
1188 -- their statuses change frequently
1189 IF TG_TABLE_NAME IN ('copy', 'unit') THEN
1191 IF OLD.location <> NEW.location OR
1192 OLD.call_number <> NEW.call_number OR
1193 OLD.status <> NEW.status OR
1194 OLD.circ_lib <> NEW.circ_lib THEN
1195 -- any of these could change visibility, but
1196 -- we'll save some queries and not try to calculate
1197 -- the change directly
1202 IF OLD.deleted <> NEW.deleted THEN
1210 IF OLD.opac_visible <> NEW.opac_visible THEN
1211 IF OLD.opac_visible THEN
1213 ELSIF NOT do_remove THEN -- handle edge case where deleted item
1214 -- is also marked opac_visible
1222 DELETE FROM asset.opac_visible_copies WHERE id = NEW.id;
1225 add_query := add_query || 'AND cp.id = ' || NEW.id || ';';
1233 IF TG_TABLE_NAME IN ('call_number', 'record_entry') THEN -- these have a 'deleted' column
1235 IF OLD.deleted AND NEW.deleted THEN -- do nothing
1239 ELSIF NEW.deleted THEN -- remove rows
1241 IF TG_TABLE_NAME = 'call_number' THEN
1242 DELETE FROM asset.opac_visible_copies WHERE id IN (SELECT id FROM asset.copy WHERE call_number = NEW.id);
1243 ELSIF TG_TABLE_NAME = 'record_entry' THEN
1244 DELETE FROM asset.opac_visible_copies WHERE record = NEW.id;
1249 ELSIF OLD.deleted THEN -- add rows
1251 IF TG_TABLE_NAME IN ('copy','unit') THEN
1252 add_query := add_query || 'AND cp.id = ' || NEW.id || ';';
1253 ELSIF TG_TABLE_NAME = 'call_number' THEN
1254 add_query := add_query || 'AND cp.call_number = ' || NEW.id || ';';
1255 ELSIF TG_TABLE_NAME = 'record_entry' THEN
1256 add_query := add_query || 'AND cn.record = ' || NEW.id || ';';
1266 IF TG_TABLE_NAME = 'call_number' THEN
1268 IF OLD.record <> NEW.record THEN
1269 -- call number is linked to different bib
1270 remove_query := remove_query || 'call_number = ' || NEW.id || ');';
1271 EXECUTE remove_query;
1272 add_query := add_query || 'AND cp.call_number = ' || NEW.id || ';';
1280 IF TG_TABLE_NAME IN ('record_entry') THEN
1281 RETURN NEW; -- don't have 'opac_visible'
1284 -- actor.org_unit, asset.copy_location, asset.copy_status
1285 IF NEW.opac_visible = OLD.opac_visible THEN -- do nothing
1289 ELSIF NEW.opac_visible THEN -- add rows
1291 IF TG_TABLE_NAME = 'org_unit' THEN
1292 add_query := add_query || 'AND cp.circ_lib = ' || NEW.id || ';';
1293 ELSIF TG_TABLE_NAME = 'copy_location' THEN
1294 add_query := add_query || 'AND cp.location = ' || NEW.id || ';';
1295 ELSIF TG_TABLE_NAME = 'copy_status' THEN
1296 add_query := add_query || 'AND cp.status = ' || NEW.id || ';';
1303 IF TG_TABLE_NAME = 'org_unit' THEN
1304 remove_query := 'DELETE FROM asset.opac_visible_copies WHERE circ_lib = ' || NEW.id || ';';
1305 ELSIF TG_TABLE_NAME = 'copy_location' THEN
1306 remove_query := remove_query || 'location = ' || NEW.id || ');';
1307 ELSIF TG_TABLE_NAME = 'copy_status' THEN
1308 remove_query := remove_query || 'status = ' || NEW.id || ');';
1311 EXECUTE remove_query;
1317 $func$ LANGUAGE PLPGSQL;
1318 COMMENT ON FUNCTION asset.cache_copy_visibility() IS $$
1319 Trigger function to update the copy OPAC visiblity cache.
1321 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();
1322 CREATE TRIGGER a_opac_vis_mat_view_tgr AFTER INSERT OR UPDATE ON asset.copy FOR EACH ROW EXECUTE PROCEDURE asset.cache_copy_visibility();
1323 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();
1324 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();
1325 CREATE TRIGGER a_opac_vis_mat_view_tgr AFTER INSERT OR UPDATE ON serial.unit FOR EACH ROW EXECUTE PROCEDURE asset.cache_copy_visibility();
1326 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();
1327 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();
1330 CREATE TRIGGER fingerprint_tgr BEFORE INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE biblio.fingerprint_trigger ('eng','BKS');
1331 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 ();
1332 CREATE TRIGGER bbb_simple_rec_trigger AFTER INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE reporter.simple_rec_trigger ();
1334 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 ();
1336 -- Utility routines, callable via cstore
1338 CREATE OR REPLACE FUNCTION config.interval_to_seconds( interval_val INTERVAL )
1339 RETURNS INTEGER AS $$
1341 RETURN EXTRACT( EPOCH FROM interval_val );
1343 $$ LANGUAGE plpgsql;
1345 CREATE OR REPLACE FUNCTION config.interval_to_seconds( interval_string TEXT )
1346 RETURNS INTEGER AS $$
1348 RETURN config.interval_to_seconds( interval_string::INTERVAL );
1350 $$ LANGUAGE plpgsql;