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;
226 -- transfer picklists the same way we transfer buckets (see above)
230 WHERE owner = src_usr
232 suffix := ' (' || src_usr || ')';
236 SET owner = dest_usr, name = name || suffix
237 WHERE id = picklist_row.id;
238 EXCEPTION WHEN unique_violation THEN
239 suffix := suffix || ' ';
246 UPDATE acq.purchase_order SET owner = dest_usr WHERE owner = src_usr;
247 UPDATE acq.po_note SET creator = dest_usr WHERE creator = src_usr;
248 UPDATE acq.po_note SET editor = dest_usr WHERE editor = src_usr;
249 UPDATE acq.lineitem_note SET creator = dest_usr WHERE creator = src_usr;
250 UPDATE acq.lineitem_note SET editor = dest_usr WHERE editor = src_usr;
251 UPDATE acq.lineitem_usr_attr_definition SET usr = dest_usr WHERE usr = src_usr;
254 UPDATE asset.copy SET creator = dest_usr WHERE creator = src_usr;
255 UPDATE asset.copy SET editor = dest_usr WHERE editor = src_usr;
256 UPDATE asset.copy_note SET creator = dest_usr WHERE creator = src_usr;
257 UPDATE asset.call_number SET creator = dest_usr WHERE creator = src_usr;
258 UPDATE asset.call_number SET editor = dest_usr WHERE editor = src_usr;
259 UPDATE asset.call_number_note SET creator = dest_usr WHERE creator = src_usr;
262 UPDATE serial.record_entry SET creator = dest_usr WHERE creator = src_usr;
263 UPDATE serial.record_entry SET editor = dest_usr WHERE editor = src_usr;
266 -- It's not uncommon to define the reporter schema in a replica
267 -- DB only, so don't assume these tables exist in the write DB.
269 UPDATE reporter.template SET owner = dest_usr WHERE owner = src_usr;
270 EXCEPTION WHEN undefined_table THEN
274 UPDATE reporter.report SET owner = dest_usr WHERE owner = src_usr;
275 EXCEPTION WHEN undefined_table THEN
279 UPDATE reporter.schedule SET runner = dest_usr WHERE runner = src_usr;
280 EXCEPTION WHEN undefined_table THEN
284 -- transfer folders the same way we transfer buckets (see above)
287 FROM reporter.template_folder
288 WHERE owner = src_usr
290 suffix := ' (' || src_usr || ')';
293 UPDATE reporter.template_folder
294 SET owner = dest_usr, name = name || suffix
295 WHERE id = folder_row.id;
296 EXCEPTION WHEN unique_violation THEN
297 suffix := suffix || ' ';
303 EXCEPTION WHEN undefined_table THEN
307 -- transfer folders the same way we transfer buckets (see above)
310 FROM reporter.report_folder
311 WHERE owner = src_usr
313 suffix := ' (' || src_usr || ')';
316 UPDATE reporter.report_folder
317 SET owner = dest_usr, name = name || suffix
318 WHERE id = folder_row.id;
319 EXCEPTION WHEN unique_violation THEN
320 suffix := suffix || ' ';
326 EXCEPTION WHEN undefined_table THEN
330 -- transfer folders the same way we transfer buckets (see above)
333 FROM reporter.output_folder
334 WHERE owner = src_usr
336 suffix := ' (' || src_usr || ')';
339 UPDATE reporter.output_folder
340 SET owner = dest_usr, name = name || suffix
341 WHERE id = folder_row.id;
342 EXCEPTION WHEN unique_violation THEN
343 suffix := suffix || ' ';
349 EXCEPTION WHEN undefined_table THEN
353 -- Finally, delete the source user
354 DELETE FROM actor.usr WHERE id = src_usr;
359 COMMENT ON FUNCTION actor.usr_merge(INT, INT, BOOLEAN, BOOLEAN, BOOLEAN) IS $$
361 * Merges all user date from src_usr to dest_usr. When collisions occur,
362 * keep dest_usr's data and delete src_usr's data.
368 CREATE OR REPLACE FUNCTION actor.usr_purge_data(
374 renamable_row RECORD;
380 mailing_address = NULL,
381 billing_address = NULL
385 UPDATE acq.fund_allocation SET allocator = dest_usr WHERE allocator = src_usr;
386 UPDATE acq.lineitem SET creator = dest_usr WHERE creator = src_usr;
387 UPDATE acq.lineitem SET editor = dest_usr WHERE editor = src_usr;
388 UPDATE acq.lineitem SET selector = dest_usr WHERE selector = src_usr;
389 UPDATE acq.lineitem_note SET creator = dest_usr WHERE creator = src_usr;
390 UPDATE acq.lineitem_note SET editor = dest_usr WHERE editor = src_usr;
391 DELETE FROM acq.lineitem_usr_attr_definition WHERE usr = src_usr;
393 -- Update with a rename to avoid collisions
397 WHERE owner = src_usr
399 suffix := ' (' || src_usr || ')';
403 SET owner = dest_usr, name = name || suffix
404 WHERE id = renamable_row.id;
405 EXCEPTION WHEN unique_violation THEN
406 suffix := suffix || ' ';
413 UPDATE acq.picklist SET creator = dest_usr WHERE creator = src_usr;
414 UPDATE acq.picklist SET editor = dest_usr WHERE editor = src_usr;
415 UPDATE acq.po_note SET creator = dest_usr WHERE creator = src_usr;
416 UPDATE acq.po_note SET editor = dest_usr WHERE editor = src_usr;
417 UPDATE acq.purchase_order SET owner = dest_usr WHERE owner = src_usr;
418 UPDATE acq.purchase_order SET creator = dest_usr WHERE creator = src_usr;
419 UPDATE acq.purchase_order SET editor = dest_usr WHERE editor = src_usr;
422 DELETE FROM action.circulation WHERE usr = src_usr;
423 UPDATE action.circulation SET circ_staff = dest_usr WHERE circ_staff = src_usr;
424 UPDATE action.circulation SET checkin_staff = dest_usr WHERE checkin_staff = src_usr;
425 UPDATE action.hold_notification SET notify_staff = dest_usr WHERE notify_staff = src_usr;
426 UPDATE action.hold_request SET fulfillment_staff = dest_usr WHERE fulfillment_staff = src_usr;
427 UPDATE action.hold_request SET requestor = dest_usr WHERE requestor = src_usr;
428 DELETE FROM action.hold_request WHERE usr = src_usr;
429 UPDATE action.in_house_use SET staff = dest_usr WHERE staff = src_usr;
430 UPDATE action.non_cat_in_house_use SET staff = dest_usr WHERE staff = src_usr;
431 DELETE FROM action.non_cataloged_circulation WHERE patron = src_usr;
432 UPDATE action.non_cataloged_circulation SET staff = dest_usr WHERE staff = src_usr;
433 DELETE FROM action.survey_response WHERE usr = src_usr;
436 DELETE FROM actor.card WHERE usr = src_usr;
437 DELETE FROM actor.stat_cat_entry_usr_map WHERE target_usr = src_usr;
439 -- The following update is intended to avoid transient violations of a foreign
440 -- key constraint, whereby actor.usr_address references itself. It may not be
441 -- necessary, but it does no harm.
442 UPDATE actor.usr_address SET replaces = NULL
443 WHERE usr = src_usr AND replaces IS NOT NULL;
444 DELETE FROM actor.usr_address WHERE usr = src_usr;
445 DELETE FROM actor.usr_note WHERE usr = src_usr;
446 UPDATE actor.usr_note SET creator = dest_usr WHERE creator = src_usr;
447 DELETE FROM actor.usr_org_unit_opt_in WHERE usr = src_usr;
448 UPDATE actor.usr_org_unit_opt_in SET staff = dest_usr WHERE staff = src_usr;
449 DELETE FROM actor.usr_setting WHERE usr = src_usr;
450 DELETE FROM actor.usr_standing_penalty WHERE usr = src_usr;
451 UPDATE actor.usr_standing_penalty SET staff = dest_usr WHERE staff = src_usr;
454 UPDATE asset.call_number SET creator = dest_usr WHERE creator = src_usr;
455 UPDATE asset.call_number SET editor = dest_usr WHERE editor = src_usr;
456 UPDATE asset.call_number_note SET creator = dest_usr WHERE creator = src_usr;
457 UPDATE asset.copy SET creator = dest_usr WHERE creator = src_usr;
458 UPDATE asset.copy SET editor = dest_usr WHERE editor = src_usr;
459 UPDATE asset.copy_note SET creator = dest_usr WHERE creator = src_usr;
462 DELETE FROM auditor.actor_usr_address_history WHERE id = src_usr;
463 DELETE FROM auditor.actor_usr_history WHERE id = src_usr;
464 UPDATE auditor.asset_call_number_history SET creator = dest_usr WHERE creator = src_usr;
465 UPDATE auditor.asset_call_number_history SET editor = dest_usr WHERE editor = src_usr;
466 UPDATE auditor.asset_copy_history SET creator = dest_usr WHERE creator = src_usr;
467 UPDATE auditor.asset_copy_history SET editor = dest_usr WHERE editor = src_usr;
468 UPDATE auditor.biblio_record_entry_history SET creator = dest_usr WHERE creator = src_usr;
469 UPDATE auditor.biblio_record_entry_history SET editor = dest_usr WHERE editor = src_usr;
472 UPDATE biblio.record_entry SET creator = dest_usr WHERE creator = src_usr;
473 UPDATE biblio.record_entry SET editor = dest_usr WHERE editor = src_usr;
474 UPDATE biblio.record_note SET creator = dest_usr WHERE creator = src_usr;
475 UPDATE biblio.record_note SET editor = dest_usr WHERE editor = src_usr;
478 -- Update buckets with a rename to avoid collisions
481 FROM container.biblio_record_entry_bucket
482 WHERE owner = src_usr
484 suffix := ' (' || src_usr || ')';
487 UPDATE container.biblio_record_entry_bucket
488 SET owner = dest_usr, name = name || suffix
489 WHERE id = renamable_row.id;
490 EXCEPTION WHEN unique_violation THEN
491 suffix := suffix || ' ';
500 FROM container.call_number_bucket
501 WHERE owner = src_usr
503 suffix := ' (' || src_usr || ')';
506 UPDATE container.call_number_bucket
507 SET owner = dest_usr, name = name || suffix
508 WHERE id = renamable_row.id;
509 EXCEPTION WHEN unique_violation THEN
510 suffix := suffix || ' ';
519 FROM container.copy_bucket
520 WHERE owner = src_usr
522 suffix := ' (' || src_usr || ')';
525 UPDATE container.copy_bucket
526 SET owner = dest_usr, name = name || suffix
527 WHERE id = renamable_row.id;
528 EXCEPTION WHEN unique_violation THEN
529 suffix := suffix || ' ';
538 FROM container.user_bucket
539 WHERE owner = src_usr
541 suffix := ' (' || src_usr || ')';
544 UPDATE container.user_bucket
545 SET owner = dest_usr, name = name || suffix
546 WHERE id = renamable_row.id;
547 EXCEPTION WHEN unique_violation THEN
548 suffix := suffix || ' ';
555 DELETE FROM container.user_bucket_item WHERE target_user = src_usr;
558 DELETE FROM money.billable_xact WHERE usr = src_usr;
559 DELETE FROM money.collections_tracker WHERE usr = src_usr;
560 UPDATE money.collections_tracker SET collector = dest_usr WHERE collector = src_usr;
563 DELETE FROM permission.usr_grp_map WHERE usr = src_usr;
564 DELETE FROM permission.usr_object_perm_map WHERE usr = src_usr;
565 DELETE FROM permission.usr_perm_map WHERE usr = src_usr;
566 DELETE FROM permission.usr_work_ou_map WHERE usr = src_usr;
569 -- Update with a rename to avoid collisions
573 FROM reporter.output_folder
574 WHERE owner = src_usr
576 suffix := ' (' || src_usr || ')';
579 UPDATE reporter.output_folder
580 SET owner = dest_usr, name = name || suffix
581 WHERE id = renamable_row.id;
582 EXCEPTION WHEN unique_violation THEN
583 suffix := suffix || ' ';
589 EXCEPTION WHEN undefined_table THEN
594 UPDATE reporter.report SET owner = dest_usr WHERE owner = src_usr;
595 EXCEPTION WHEN undefined_table THEN
599 -- Update with a rename to avoid collisions
603 FROM reporter.report_folder
604 WHERE owner = src_usr
606 suffix := ' (' || src_usr || ')';
609 UPDATE reporter.report_folder
610 SET owner = dest_usr, name = name || suffix
611 WHERE id = renamable_row.id;
612 EXCEPTION WHEN unique_violation THEN
613 suffix := suffix || ' ';
619 EXCEPTION WHEN undefined_table THEN
624 UPDATE reporter.schedule SET runner = dest_usr WHERE runner = src_usr;
625 EXCEPTION WHEN undefined_table THEN
630 UPDATE reporter.template SET owner = dest_usr WHERE owner = src_usr;
631 EXCEPTION WHEN undefined_table THEN
635 -- Update with a rename to avoid collisions
639 FROM reporter.template_folder
640 WHERE owner = src_usr
642 suffix := ' (' || src_usr || ')';
645 UPDATE reporter.template_folder
646 SET owner = dest_usr, name = name || suffix
647 WHERE id = renamable_row.id;
648 EXCEPTION WHEN unique_violation THEN
649 suffix := suffix || ' ';
655 EXCEPTION WHEN undefined_table THEN
660 -- Update with a rename to avoid collisions
664 WHERE owner = src_usr
666 suffix := ' (' || src_usr || ')';
669 UPDATE vandelay.queue
670 SET owner = dest_usr, name = name || suffix
671 WHERE id = renamable_row.id;
672 EXCEPTION WHEN unique_violation THEN
673 suffix := suffix || ' ';
683 COMMENT ON FUNCTION actor.usr_purge_data(INT, INT) IS $$
685 * Finds rows dependent on a given row in actor.usr and either deletes them
686 * or reassigns them to a different user.
692 CREATE OR REPLACE FUNCTION actor.usr_delete(
697 old_profile actor.usr.profile%type;
698 old_home_ou actor.usr.home_ou%type;
699 new_profile actor.usr.profile%type;
700 new_home_ou actor.usr.home_ou%type;
702 new_dob actor.usr.dob%type;
705 id || '-PURGED-' || now(),
719 -- Quit if no such user
721 IF old_profile IS NULL THEN
725 perform actor.usr_purge_data( src_usr, dest_usr );
727 -- Find the root grp_tree and the root org_unit. This would be simpler if we
728 -- could assume that there is only one root. Theoretically, someday, maybe,
729 -- there could be multiple roots, so we take extra trouble to get the right ones.
736 permission.grp_ancestors( old_profile )
745 actor.org_unit_ancestors( old_home_ou )
749 -- Truncate date of birth
751 IF new_dob IS NOT NULL THEN
752 new_dob := date_trunc( 'year', new_dob );
759 profile = new_profile,
762 passwd = random()::text,
767 FROM config.identification_type
772 net_access_level = DEFAULT,
775 first_given_name = new_name,
776 second_given_name = NULL,
777 family_name = new_name,
781 evening_phone = NULL,
783 mailing_address = NULL,
784 billing_address = NULL,
785 home_ou = new_home_ou,
788 master_account = DEFAULT,
789 super_user = DEFAULT,
794 claims_returned_count = DEFAULT,
795 credit_forward_balance = DEFAULT,
796 last_xact_id = DEFAULT,
797 alert_message = NULL,
805 COMMENT ON FUNCTION actor.usr_delete(INT, INT) IS $$
807 * Logically deletes a user. Removes personally identifiable information,
808 * and purges associated data in other tables.
814 CREATE OR REPLACE FUNCTION actor.approve_pending_address(pending_id INT) RETURNS BIGINT AS $$
818 SELECT INTO old_id replaces FROM actor.usr_address where id = pending_id;
819 IF old_id IS NULL THEN
820 UPDATE actor.usr_address SET pending = 'f' WHERE id = pending_id;
823 -- address replaces an existing address
824 DELETE FROM actor.usr_address WHERE id = -old_id;
825 UPDATE actor.usr_address SET id = -id WHERE id = old_id;
826 UPDATE actor.usr_address SET replaces = NULL, id = old_id, pending = 'f' WHERE id = pending_id;
831 COMMENT ON FUNCTION actor.approve_pending_address(INT) IS $$
833 * Replaces an address with a pending address. This is done by giving the pending
834 * address the ID of the old address. The replaced address is retained with -id.
838 CREATE OR REPLACE FUNCTION container.clear_expired_circ_history_items(
842 -- Delete old circulation bucket items for a specified user.
843 -- "Old" means older than the interval specified by a
844 -- user-level setting, if it is so specified.
847 threshold TIMESTAMP WITH TIME ZONE;
850 IF ac_usr IS NULL THEN
853 -- Determine the threshold date that defines "old". Subtract the
854 -- interval from the system date, then truncate to midnight.
858 now() - CAST( translate( value, '"', '' ) AS INTERVAL )
866 AND name = 'patron.max_reading_list_interval';
868 IF threshold is null THEN
869 -- No interval defined; don't delete anything
870 -- RAISE NOTICE 'No interval defined for user %', ac_usr;
874 -- RAISE NOTICE 'Date threshold: %', threshold;
876 -- Threshold found; do the delete
877 delete from container.copy_bucket_item
884 container.copy_bucket
887 and btype = 'circ_history'
889 and create_time < threshold;
895 COMMENT ON FUNCTION container.clear_expired_circ_history_items( INTEGER ) IS $$
897 * Delete old circulation bucket items for a specified user.
898 * "Old" means older than the interval specified by a
899 * user-level setting, if it is so specified.
903 CREATE OR REPLACE FUNCTION container.clear_all_expired_circ_history_items( )
906 -- Delete expired circulation bucket items for all users that have
907 -- a setting for patron.max_reading_list_interval.
910 today TIMESTAMP WITH TIME ZONE;
911 threshold TIMESTAMP WITH TIME ZONE;
914 SELECT date_trunc( 'day', now() ) INTO today;
923 name = 'patron.max_reading_list_interval'
926 -- Make sure the setting is a valid interval
929 threshold := today - CAST( translate( usr_setting.value, '"', '' ) AS INTERVAL );
932 RAISE NOTICE 'Invalid setting patron.max_reading_list_interval for user %: ''%''',
933 usr_setting.usr, usr_setting.value;
937 --RAISE NOTICE 'User % threshold %', usr_setting.usr, threshold;
939 DELETE FROM container.copy_bucket_item
946 container.copy_bucket
948 owner = usr_setting.usr
949 AND btype = 'circ_history'
951 AND create_time < threshold;
957 COMMENT ON FUNCTION container.clear_all_expired_circ_history_items( ) IS $$
959 * Delete expired circulation bucket items for all users that have
960 * a setting for patron.max_reading_list_interval.
965 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
967 moved_objects INT := 0;
968 source_cn asset.call_number%ROWTYPE;
969 target_cn asset.call_number%ROWTYPE;
970 metarec metabib.metarecord%ROWTYPE;
971 hold action.hold_request%ROWTYPE;
972 ser_rec serial.record_entry%ROWTYPE;
979 -- move any 856 entries on records that have at least one MARC-mapped URI entry
980 SELECT INTO uri_count COUNT(*)
981 FROM asset.uri_call_number_map m
982 JOIN asset.call_number cn ON (m.call_number = cn.id)
983 WHERE cn.record = source_record;
985 IF uri_count > 0 THEN
987 SELECT COUNT(*) INTO counter
988 FROM oils_xpath_table(
991 'biblio.record_entry',
994 ) as t(i int,c text);
996 FOR i IN 1 .. counter LOOP
997 SELECT '<datafield xmlns="http://www.loc.gov/MARC21/slim" tag="856">' ||
1000 '<subfield code="' || subfield || '">' ||
1003 regexp_replace(data,'&','&','g'),
1009 ) || '</datafield>' INTO uri_datafield
1010 FROM oils_xpath_table(
1013 'biblio.record_entry',
1014 '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
1015 '//*[@tag="856"][position()=' || i || ']/*[@code]',
1016 'id=' || source_record
1017 ) as t(id int,subfield text,data text);
1019 uri_text := uri_text || uri_datafield;
1022 IF uri_text <> '' THEN
1023 UPDATE biblio.record_entry
1024 SET marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
1025 WHERE id = target_record;
1030 -- Find and move metarecords to the target record
1031 SELECT INTO metarec *
1032 FROM metabib.metarecord
1033 WHERE master_record = source_record;
1036 UPDATE metabib.metarecord
1037 SET master_record = target_record,
1039 WHERE id = metarec.id;
1041 moved_objects := moved_objects + 1;
1044 -- Find call numbers attached to the source ...
1045 FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
1047 SELECT INTO target_cn *
1048 FROM asset.call_number
1049 WHERE label = source_cn.label
1050 AND owning_lib = source_cn.owning_lib
1051 AND record = target_record;
1053 -- ... and if there's a conflicting one on the target ...
1056 -- ... move the copies to that, and ...
1058 SET call_number = target_cn.id
1059 WHERE call_number = source_cn.id;
1061 -- ... move V holds to the move-target call number
1062 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
1064 UPDATE action.hold_request
1065 SET target = target_cn.id
1068 moved_objects := moved_objects + 1;
1073 -- ... just move the call number to the target record
1074 UPDATE asset.call_number
1075 SET record = target_record
1076 WHERE id = source_cn.id;
1079 moved_objects := moved_objects + 1;
1082 -- Find T holds targeting the source record ...
1083 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
1085 -- ... and move them to the target record
1086 UPDATE action.hold_request
1087 SET target = target_record
1090 moved_objects := moved_objects + 1;
1093 -- Find serial records targeting the source record ...
1094 FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
1095 -- ... and move them to the target record
1096 UPDATE serial.record_entry
1097 SET record = target_record
1098 WHERE id = ser_rec.id;
1100 moved_objects := moved_objects + 1;
1103 -- Finally, "delete" the source record
1104 DELETE FROM biblio.record_entry WHERE id = source_record;
1106 -- That's all, folks!
1107 RETURN moved_objects;
1109 $func$ LANGUAGE plpgsql;
1112 CREATE TRIGGER fingerprint_tgr BEFORE INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE biblio.fingerprint_trigger ('eng','BKS');
1113 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 ();
1114 CREATE TRIGGER bbb_simple_rec_trigger AFTER INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE reporter.simple_rec_trigger ();