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.provider_note SET creator = dest_usr WHERE creator = src_usr;
250 UPDATE acq.provider_note SET editor = dest_usr WHERE editor = src_usr;
251 UPDATE acq.lineitem_note SET creator = dest_usr WHERE creator = src_usr;
252 UPDATE acq.lineitem_note SET editor = dest_usr WHERE editor = src_usr;
253 UPDATE acq.lineitem_usr_attr_definition SET usr = dest_usr WHERE usr = src_usr;
256 UPDATE asset.copy SET creator = dest_usr WHERE creator = src_usr;
257 UPDATE asset.copy SET editor = dest_usr WHERE editor = src_usr;
258 UPDATE asset.copy_note SET creator = dest_usr WHERE creator = src_usr;
259 UPDATE asset.call_number SET creator = dest_usr WHERE creator = src_usr;
260 UPDATE asset.call_number SET editor = dest_usr WHERE editor = src_usr;
261 UPDATE asset.call_number_note SET creator = dest_usr WHERE creator = src_usr;
264 UPDATE serial.record_entry SET creator = dest_usr WHERE creator = src_usr;
265 UPDATE serial.record_entry SET editor = dest_usr WHERE editor = src_usr;
268 -- It's not uncommon to define the reporter schema in a replica
269 -- DB only, so don't assume these tables exist in the write DB.
271 UPDATE reporter.template SET owner = dest_usr WHERE owner = src_usr;
272 EXCEPTION WHEN undefined_table THEN
276 UPDATE reporter.report SET owner = dest_usr WHERE owner = src_usr;
277 EXCEPTION WHEN undefined_table THEN
281 UPDATE reporter.schedule SET runner = dest_usr WHERE runner = src_usr;
282 EXCEPTION WHEN undefined_table THEN
286 -- transfer folders the same way we transfer buckets (see above)
289 FROM reporter.template_folder
290 WHERE owner = src_usr
292 suffix := ' (' || src_usr || ')';
295 UPDATE reporter.template_folder
296 SET owner = dest_usr, name = name || suffix
297 WHERE id = folder_row.id;
298 EXCEPTION WHEN unique_violation THEN
299 suffix := suffix || ' ';
305 EXCEPTION WHEN undefined_table THEN
309 -- transfer folders the same way we transfer buckets (see above)
312 FROM reporter.report_folder
313 WHERE owner = src_usr
315 suffix := ' (' || src_usr || ')';
318 UPDATE reporter.report_folder
319 SET owner = dest_usr, name = name || suffix
320 WHERE id = folder_row.id;
321 EXCEPTION WHEN unique_violation THEN
322 suffix := suffix || ' ';
328 EXCEPTION WHEN undefined_table THEN
332 -- transfer folders the same way we transfer buckets (see above)
335 FROM reporter.output_folder
336 WHERE owner = src_usr
338 suffix := ' (' || src_usr || ')';
341 UPDATE reporter.output_folder
342 SET owner = dest_usr, name = name || suffix
343 WHERE id = folder_row.id;
344 EXCEPTION WHEN unique_violation THEN
345 suffix := suffix || ' ';
351 EXCEPTION WHEN undefined_table THEN
355 -- Finally, delete the source user
356 DELETE FROM actor.usr WHERE id = src_usr;
361 COMMENT ON FUNCTION actor.usr_merge(INT, INT, BOOLEAN, BOOLEAN, BOOLEAN) IS $$
363 * Merges all user date from src_usr to dest_usr. When collisions occur,
364 * keep dest_usr's data and delete src_usr's data.
369 CREATE OR REPLACE FUNCTION actor.usr_purge_data(
375 renamable_row RECORD;
381 mailing_address = NULL,
382 billing_address = NULL
386 UPDATE acq.fund_allocation SET allocator = dest_usr WHERE allocator = src_usr;
387 UPDATE acq.lineitem SET creator = dest_usr WHERE creator = src_usr;
388 UPDATE acq.lineitem SET editor = dest_usr WHERE editor = src_usr;
389 UPDATE acq.lineitem SET selector = dest_usr WHERE selector = src_usr;
390 UPDATE acq.lineitem_note SET creator = dest_usr WHERE creator = src_usr;
391 UPDATE acq.lineitem_note SET editor = dest_usr WHERE editor = src_usr;
392 DELETE FROM acq.lineitem_usr_attr_definition WHERE usr = src_usr;
394 -- Update with a rename to avoid collisions
398 WHERE owner = src_usr
400 suffix := ' (' || src_usr || ')';
404 SET owner = dest_usr, name = name || suffix
405 WHERE id = renamable_row.id;
406 EXCEPTION WHEN unique_violation THEN
407 suffix := suffix || ' ';
414 UPDATE acq.picklist SET creator = dest_usr WHERE creator = src_usr;
415 UPDATE acq.picklist SET editor = dest_usr WHERE editor = src_usr;
416 UPDATE acq.po_note SET creator = dest_usr WHERE creator = src_usr;
417 UPDATE acq.po_note SET editor = dest_usr WHERE editor = src_usr;
418 UPDATE acq.purchase_order SET owner = dest_usr WHERE owner = src_usr;
419 UPDATE acq.purchase_order SET creator = dest_usr WHERE creator = src_usr;
420 UPDATE acq.purchase_order SET editor = dest_usr WHERE editor = src_usr;
421 UPDATE acq.claim_event SET creator = dest_usr WHERE creator = src_usr;
424 DELETE FROM action.circulation WHERE usr = src_usr;
425 UPDATE action.circulation SET circ_staff = dest_usr WHERE circ_staff = src_usr;
426 UPDATE action.circulation SET checkin_staff = dest_usr WHERE checkin_staff = src_usr;
427 UPDATE action.hold_notification SET notify_staff = dest_usr WHERE notify_staff = src_usr;
428 UPDATE action.hold_request SET fulfillment_staff = dest_usr WHERE fulfillment_staff = src_usr;
429 UPDATE action.hold_request SET requestor = dest_usr WHERE requestor = src_usr;
430 DELETE FROM action.hold_request WHERE usr = src_usr;
431 UPDATE action.in_house_use SET staff = dest_usr WHERE staff = src_usr;
432 UPDATE action.non_cat_in_house_use SET staff = dest_usr WHERE staff = src_usr;
433 DELETE FROM action.non_cataloged_circulation WHERE patron = src_usr;
434 UPDATE action.non_cataloged_circulation SET staff = dest_usr WHERE staff = src_usr;
435 DELETE FROM action.survey_response WHERE usr = src_usr;
438 DELETE FROM actor.card WHERE usr = src_usr;
439 DELETE FROM actor.stat_cat_entry_usr_map WHERE target_usr = src_usr;
441 -- The following update is intended to avoid transient violations of a foreign
442 -- key constraint, whereby actor.usr_address references itself. It may not be
443 -- necessary, but it does no harm.
444 UPDATE actor.usr_address SET replaces = NULL
445 WHERE usr = src_usr AND replaces IS NOT NULL;
446 DELETE FROM actor.usr_address WHERE usr = src_usr;
447 DELETE FROM actor.usr_note WHERE usr = src_usr;
448 UPDATE actor.usr_note SET creator = dest_usr WHERE creator = src_usr;
449 DELETE FROM actor.usr_org_unit_opt_in WHERE usr = src_usr;
450 UPDATE actor.usr_org_unit_opt_in SET staff = dest_usr WHERE staff = src_usr;
451 DELETE FROM actor.usr_setting WHERE usr = src_usr;
452 DELETE FROM actor.usr_standing_penalty WHERE usr = src_usr;
453 UPDATE actor.usr_standing_penalty SET staff = dest_usr WHERE staff = src_usr;
456 UPDATE asset.call_number SET creator = dest_usr WHERE creator = src_usr;
457 UPDATE asset.call_number SET editor = dest_usr WHERE editor = src_usr;
458 UPDATE asset.call_number_note SET creator = dest_usr WHERE creator = src_usr;
459 UPDATE asset.copy SET creator = dest_usr WHERE creator = src_usr;
460 UPDATE asset.copy SET editor = dest_usr WHERE editor = src_usr;
461 UPDATE asset.copy_note SET creator = dest_usr WHERE creator = src_usr;
464 DELETE FROM auditor.actor_usr_address_history WHERE id = src_usr;
465 DELETE FROM auditor.actor_usr_history WHERE id = src_usr;
466 UPDATE auditor.asset_call_number_history SET creator = dest_usr WHERE creator = src_usr;
467 UPDATE auditor.asset_call_number_history SET editor = dest_usr WHERE editor = src_usr;
468 UPDATE auditor.asset_copy_history SET creator = dest_usr WHERE creator = src_usr;
469 UPDATE auditor.asset_copy_history SET editor = dest_usr WHERE editor = src_usr;
470 UPDATE auditor.biblio_record_entry_history SET creator = dest_usr WHERE creator = src_usr;
471 UPDATE auditor.biblio_record_entry_history SET editor = dest_usr WHERE editor = src_usr;
474 UPDATE biblio.record_entry SET creator = dest_usr WHERE creator = src_usr;
475 UPDATE biblio.record_entry SET editor = dest_usr WHERE editor = src_usr;
476 UPDATE biblio.record_note SET creator = dest_usr WHERE creator = src_usr;
477 UPDATE biblio.record_note SET editor = dest_usr WHERE editor = src_usr;
480 -- Update buckets with a rename to avoid collisions
483 FROM container.biblio_record_entry_bucket
484 WHERE owner = src_usr
486 suffix := ' (' || src_usr || ')';
489 UPDATE container.biblio_record_entry_bucket
490 SET owner = dest_usr, name = name || suffix
491 WHERE id = renamable_row.id;
492 EXCEPTION WHEN unique_violation THEN
493 suffix := suffix || ' ';
502 FROM container.call_number_bucket
503 WHERE owner = src_usr
505 suffix := ' (' || src_usr || ')';
508 UPDATE container.call_number_bucket
509 SET owner = dest_usr, name = name || suffix
510 WHERE id = renamable_row.id;
511 EXCEPTION WHEN unique_violation THEN
512 suffix := suffix || ' ';
521 FROM container.copy_bucket
522 WHERE owner = src_usr
524 suffix := ' (' || src_usr || ')';
527 UPDATE container.copy_bucket
528 SET owner = dest_usr, name = name || suffix
529 WHERE id = renamable_row.id;
530 EXCEPTION WHEN unique_violation THEN
531 suffix := suffix || ' ';
540 FROM container.user_bucket
541 WHERE owner = src_usr
543 suffix := ' (' || src_usr || ')';
546 UPDATE container.user_bucket
547 SET owner = dest_usr, name = name || suffix
548 WHERE id = renamable_row.id;
549 EXCEPTION WHEN unique_violation THEN
550 suffix := suffix || ' ';
557 DELETE FROM container.user_bucket_item WHERE target_user = src_usr;
560 DELETE FROM money.billable_xact WHERE usr = src_usr;
561 DELETE FROM money.collections_tracker WHERE usr = src_usr;
562 UPDATE money.collections_tracker SET collector = dest_usr WHERE collector = src_usr;
565 DELETE FROM permission.usr_grp_map WHERE usr = src_usr;
566 DELETE FROM permission.usr_object_perm_map WHERE usr = src_usr;
567 DELETE FROM permission.usr_perm_map WHERE usr = src_usr;
568 DELETE FROM permission.usr_work_ou_map WHERE usr = src_usr;
571 -- Update with a rename to avoid collisions
575 FROM reporter.output_folder
576 WHERE owner = src_usr
578 suffix := ' (' || src_usr || ')';
581 UPDATE reporter.output_folder
582 SET owner = dest_usr, name = name || suffix
583 WHERE id = renamable_row.id;
584 EXCEPTION WHEN unique_violation THEN
585 suffix := suffix || ' ';
591 EXCEPTION WHEN undefined_table THEN
596 UPDATE reporter.report SET owner = dest_usr WHERE owner = src_usr;
597 EXCEPTION WHEN undefined_table THEN
601 -- Update with a rename to avoid collisions
605 FROM reporter.report_folder
606 WHERE owner = src_usr
608 suffix := ' (' || src_usr || ')';
611 UPDATE reporter.report_folder
612 SET owner = dest_usr, name = name || suffix
613 WHERE id = renamable_row.id;
614 EXCEPTION WHEN unique_violation THEN
615 suffix := suffix || ' ';
621 EXCEPTION WHEN undefined_table THEN
626 UPDATE reporter.schedule SET runner = dest_usr WHERE runner = src_usr;
627 EXCEPTION WHEN undefined_table THEN
632 UPDATE reporter.template SET owner = dest_usr WHERE owner = src_usr;
633 EXCEPTION WHEN undefined_table THEN
637 -- Update with a rename to avoid collisions
641 FROM reporter.template_folder
642 WHERE owner = src_usr
644 suffix := ' (' || src_usr || ')';
647 UPDATE reporter.template_folder
648 SET owner = dest_usr, name = name || suffix
649 WHERE id = renamable_row.id;
650 EXCEPTION WHEN unique_violation THEN
651 suffix := suffix || ' ';
657 EXCEPTION WHEN undefined_table THEN
662 -- Update with a rename to avoid collisions
666 WHERE owner = src_usr
668 suffix := ' (' || src_usr || ')';
671 UPDATE vandelay.queue
672 SET owner = dest_usr, name = name || suffix
673 WHERE id = renamable_row.id;
674 EXCEPTION WHEN unique_violation THEN
675 suffix := suffix || ' ';
685 COMMENT ON FUNCTION actor.usr_purge_data(INT, INT) IS $$
687 * Finds rows dependent on a given row in actor.usr and either deletes them
688 * or reassigns them to a different user.
694 CREATE OR REPLACE FUNCTION actor.usr_delete(
699 old_profile actor.usr.profile%type;
700 old_home_ou actor.usr.home_ou%type;
701 new_profile actor.usr.profile%type;
702 new_home_ou actor.usr.home_ou%type;
704 new_dob actor.usr.dob%type;
707 id || '-PURGED-' || now(),
721 -- Quit if no such user
723 IF old_profile IS NULL THEN
727 perform actor.usr_purge_data( src_usr, dest_usr );
729 -- Find the root grp_tree and the root org_unit. This would be simpler if we
730 -- could assume that there is only one root. Theoretically, someday, maybe,
731 -- there could be multiple roots, so we take extra trouble to get the right ones.
738 permission.grp_ancestors( old_profile )
747 actor.org_unit_ancestors( old_home_ou )
751 -- Truncate date of birth
753 IF new_dob IS NOT NULL THEN
754 new_dob := date_trunc( 'year', new_dob );
761 profile = new_profile,
764 passwd = random()::text,
769 FROM config.identification_type
774 net_access_level = DEFAULT,
777 first_given_name = new_name,
778 second_given_name = NULL,
779 family_name = new_name,
783 evening_phone = NULL,
785 mailing_address = NULL,
786 billing_address = NULL,
787 home_ou = new_home_ou,
790 master_account = DEFAULT,
791 super_user = DEFAULT,
796 claims_returned_count = DEFAULT,
797 credit_forward_balance = DEFAULT,
798 last_xact_id = DEFAULT,
799 alert_message = NULL,
807 COMMENT ON FUNCTION actor.usr_delete(INT, INT) IS $$
809 * Logically deletes a user. Removes personally identifiable information,
810 * and purges associated data in other tables.
816 CREATE OR REPLACE FUNCTION actor.approve_pending_address(pending_id INT) RETURNS BIGINT AS $$
820 SELECT INTO old_id replaces FROM actor.usr_address where id = pending_id;
821 IF old_id IS NULL THEN
822 UPDATE actor.usr_address SET pending = 'f' WHERE id = pending_id;
825 -- address replaces an existing address
826 DELETE FROM actor.usr_address WHERE id = -old_id;
827 UPDATE actor.usr_address SET id = -id WHERE id = old_id;
828 UPDATE actor.usr_address SET replaces = NULL, id = old_id, pending = 'f' WHERE id = pending_id;
833 COMMENT ON FUNCTION actor.approve_pending_address(INT) IS $$
835 * Replaces an address with a pending address. This is done by giving the pending
836 * address the ID of the old address. The replaced address is retained with -id.
840 CREATE OR REPLACE FUNCTION container.clear_expired_circ_history_items(
844 -- Delete old circulation bucket items for a specified user.
845 -- "Old" means older than the interval specified by a
846 -- user-level setting, if it is so specified.
849 threshold TIMESTAMP WITH TIME ZONE;
852 IF ac_usr IS NULL THEN
855 -- Determine the threshold date that defines "old". Subtract the
856 -- interval from the system date, then truncate to midnight.
860 now() - CAST( translate( value, '"', '' ) AS INTERVAL )
868 AND name = 'patron.max_reading_list_interval';
870 IF threshold is null THEN
871 -- No interval defined; don't delete anything
872 -- RAISE NOTICE 'No interval defined for user %', ac_usr;
876 -- RAISE NOTICE 'Date threshold: %', threshold;
878 -- Threshold found; do the delete
879 delete from container.copy_bucket_item
886 container.copy_bucket
889 and btype = 'circ_history'
891 and create_time < threshold;
897 COMMENT ON FUNCTION container.clear_expired_circ_history_items( INTEGER ) IS $$
899 * Delete old circulation bucket items for a specified user.
900 * "Old" means older than the interval specified by a
901 * user-level setting, if it is so specified.
905 CREATE OR REPLACE FUNCTION container.clear_all_expired_circ_history_items( )
908 -- Delete expired circulation bucket items for all users that have
909 -- a setting for patron.max_reading_list_interval.
912 today TIMESTAMP WITH TIME ZONE;
913 threshold TIMESTAMP WITH TIME ZONE;
916 SELECT date_trunc( 'day', now() ) INTO today;
925 name = 'patron.max_reading_list_interval'
928 -- Make sure the setting is a valid interval
931 threshold := today - CAST( translate( usr_setting.value, '"', '' ) AS INTERVAL );
934 RAISE NOTICE 'Invalid setting patron.max_reading_list_interval for user %: ''%''',
935 usr_setting.usr, usr_setting.value;
939 --RAISE NOTICE 'User % threshold %', usr_setting.usr, threshold;
941 DELETE FROM container.copy_bucket_item
948 container.copy_bucket
950 owner = usr_setting.usr
951 AND btype = 'circ_history'
953 AND create_time < threshold;
959 COMMENT ON FUNCTION container.clear_all_expired_circ_history_items( ) IS $$
961 * Delete expired circulation bucket items for all users that have
962 * 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;
1114 CREATE TRIGGER fingerprint_tgr BEFORE INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE biblio.fingerprint_trigger ('eng','BKS');
1115 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 ();
1116 CREATE TRIGGER bbb_simple_rec_trigger AFTER INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE reporter.simple_rec_trigger ();