2 CREATE OR REPLACE FUNCTION migration_tools.create_staff_user(
7 first_name TEXT DEFAULT '',
8 last_name TEXT DEFAULT ''
9 ) RETURNS VOID AS $func$
11 RAISE NOTICE '%', org ;
12 INSERT INTO actor.usr (usrname, passwd, ident_type, first_given_name, family_name, home_ou, profile)
13 SELECT username, password, 1, first_name, last_name, aou.id, pgt.id
14 FROM actor.org_unit aou, permission.grp_tree pgt
15 WHERE aou.shortname = org
16 AND pgt.name = perm_group;
21 -- FIXME: testing for STAFF_LOGIN perm is probably better
22 CREATE OR REPLACE FUNCTION migration_tools.is_staff_profile (INT) RETURNS BOOLEAN AS $$
26 RETURN CASE WHEN 'Staff' IN (select (permission.grp_ancestors(profile)).name) THEN TRUE ELSE FALSE END;
28 $$ LANGUAGE PLPGSQL STRICT STABLE;
30 -- TODO: make another version of the procedure below that can work with specified copy staging tables
31 -- The following should track the logic of OpenILS::Application::AppUtils::get_copy_price
32 CREATE OR REPLACE FUNCTION migration_tools.get_copy_price( item BIGINT ) RETURNS NUMERIC AS $$
35 charge_lost_on_zero BOOLEAN;
38 default_price NUMERIC;
39 working_price NUMERIC;
43 SELECT INTO context_lib CASE WHEN call_number = -1 THEN circ_lib ELSE owning_lib END
44 FROM asset.copy ac, asset.call_number acn WHERE ac.call_number = acn.id AND ac.id = item;
46 SELECT INTO charge_lost_on_zero value
47 FROM actor.org_unit_ancestor_setting('circ.charge_lost_on_zero',context_lib);
49 SELECT INTO min_price value
50 FROM actor.org_unit_ancestor_setting('circ.min_item_price',context_lib);
52 SELECT INTO max_price value
53 FROM actor.org_unit_ancestor_setting('circ.max_item_price',context_lib);
55 SELECT INTO default_price value
56 FROM actor.org_unit_ancestor_setting('cat.default_item_price',context_lib);
58 SELECT INTO working_price price FROM asset.copy WHERE id = item;
60 IF (working_price IS NULL OR (working_price = 0 AND charge_lost_on_zero)) THEN
61 working_price := default_price;
64 IF (max_price IS NOT NULL AND working_price > max_price) THEN
65 working_price := max_price;
68 IF (min_price IS NOT NULL AND working_price < min_price) THEN
69 IF (working_price <> 0 OR charge_lost_on_zero IS NULL OR charge_lost_on_zero) THEN
70 working_price := min_price;
80 CREATE OR REPLACE FUNCTION migration_tools.apply_circ_matrix( tablename TEXT ) RETURNS VOID AS $$
84 -- First make sure the circ matrix is loaded and the circulations
85 -- have been staged to the extent possible (but at the very least
86 -- circ_lib, target_copy, usr, and *_renewal). User profiles and
87 -- circ modifiers must also be in place.
89 -- SELECT migration_tools.apply_circ_matrix('m_pioneer.action_circulation');
97 this_duration_rule INT;
99 this_max_fine_rule INT;
100 rcd config.rule_circ_duration%ROWTYPE;
101 rrf config.rule_recurring_fine%ROWTYPE;
102 rmf config.rule_max_fine%ROWTYPE;
109 EXECUTE 'SELECT COUNT(*) FROM ' || tablename || ';' INTO n_circs;
111 FOR circ IN EXECUTE ('SELECT id FROM ' || tablename) LOOP
113 -- Fetch the correct rules for this circulation
120 WHEN phone_renewal OR desk_renewal OR opac_renewal THEN TRUE
123 FROM ' || tablename || ' WHERE id = ' || circ || ';')
124 INTO circ_lib, target_copy, usr, is_renewal ;
126 INTO this_duration_rule,
132 FROM action.item_user_circ_test(
138 SELECT INTO rcd * FROM config.rule_circ_duration
139 WHERE id = this_duration_rule;
140 SELECT INTO rrf * FROM config.rule_recurring_fine
141 WHERE id = this_fine_rule;
142 SELECT INTO rmf * FROM config.rule_max_fine
143 WHERE id = this_max_fine_rule;
145 -- Apply the rules to this circulation
146 EXECUTE ('UPDATE ' || tablename || ' c
148 duration_rule = rcd.name,
149 recurring_fine_rule = rrf.name,
150 max_fine_rule = rmf.name,
151 duration = rcd.normal,
152 recurring_fine = rrf.normal,
155 WHEN TRUE THEN (rmf.amount / 100.0) * ac.price
158 renewal_remaining = rcd.max_renewals
160 config.rule_circ_duration rcd,
161 config.rule_recurring_fine rrf,
162 config.rule_max_fine rmf,
165 rcd.id = ' || this_duration_rule || ' AND
166 rrf.id = ' || this_fine_rule || ' AND
167 rmf.id = ' || this_max_fine_rule || ' AND
168 ac.id = c.target_copy AND
169 c.id = ' || circ || ';');
171 -- Keep track of where we are in the process
173 IF (n % 100 = 0) THEN
174 RAISE INFO '%', n || ' of ' || n_circs
175 || ' (' || (100*n/n_circs) || '%) circs updated.';
185 CREATE OR REPLACE FUNCTION migration_tools.apply_circ_matrix_to_specific_circ( tablename TEXT, circ BIGINT ) RETURNS VOID AS $$
189 -- First make sure the circ matrix is loaded and the circulations
190 -- have been staged to the extent possible (but at the very least
191 -- circ_lib, target_copy, usr, and *_renewal). User profiles and
192 -- circ modifiers must also be in place.
194 -- SELECT migration_tools.apply_circ_matrix_to_specific_circ('m_nwrl.action_circulation', 18391960);
202 this_duration_rule INT;
204 this_max_fine_rule INT;
205 rcd config.rule_circ_duration%ROWTYPE;
206 rrf config.rule_recurring_fine%ROWTYPE;
207 rmf config.rule_max_fine%ROWTYPE;
213 --EXECUTE 'SELECT COUNT(*) FROM ' || tablename || ';' INTO n_circs;
215 --FOR circ IN EXECUTE ('SELECT id FROM ' || tablename) LOOP
217 -- Fetch the correct rules for this circulation
224 WHEN phone_renewal OR desk_renewal OR opac_renewal THEN TRUE
227 FROM ' || tablename || ' WHERE id = ' || circ || ';')
228 INTO circ_lib, target_copy, usr, is_renewal ;
230 INTO this_duration_rule,
233 (matchpoint).duration_rule,
234 (matchpoint).recurring_fine_rule,
235 (matchpoint).max_fine_rule
236 FROM action.find_circ_matrix_matchpoint(
242 SELECT INTO rcd * FROM config.rule_circ_duration
243 WHERE id = this_duration_rule;
244 SELECT INTO rrf * FROM config.rule_recurring_fine
245 WHERE id = this_fine_rule;
246 SELECT INTO rmf * FROM config.rule_max_fine
247 WHERE id = this_max_fine_rule;
249 -- Apply the rules to this circulation
250 EXECUTE ('UPDATE ' || tablename || ' c
252 duration_rule = rcd.name,
253 recurring_fine_rule = rrf.name,
254 max_fine_rule = rmf.name,
255 duration = rcd.normal,
256 recurring_fine = rrf.normal,
259 WHEN TRUE THEN (rmf.amount / 100.0) * migration_tools.get_copy_price(ac.id)
262 renewal_remaining = rcd.max_renewals,
263 grace_period = rrf.grace_period
265 config.rule_circ_duration rcd,
266 config.rule_recurring_fine rrf,
267 config.rule_max_fine rmf,
270 rcd.id = ' || this_duration_rule || ' AND
271 rrf.id = ' || this_fine_rule || ' AND
272 rmf.id = ' || this_max_fine_rule || ' AND
273 ac.id = c.target_copy AND
274 c.id = ' || circ || ';');
276 -- Keep track of where we are in the process
278 IF (n % 100 = 0) THEN
279 RAISE INFO '%', n || ' of ' || n_circs
280 || ' (' || (100*n/n_circs) || '%) circs updated.';
290 CREATE OR REPLACE FUNCTION migration_tools.assign_standing_penalties ( ) RETURNS VOID AS $$
292 -- USAGE: Once circulation data has been loaded, and group penalty thresholds have been set up, run this.
293 -- This will assign standing penalties as needed.
301 FOR org_unit IN EXECUTE ('SELECT DISTINCT org_unit FROM permission.grp_penalty_threshold;') LOOP
303 FOR usr IN EXECUTE ('SELECT id FROM actor.usr WHERE NOT deleted;') LOOP
305 EXECUTE('SELECT actor.calculate_system_penalties(' || usr || ', ' || org_unit || ');');
317 CREATE OR REPLACE FUNCTION migration_tools.insert_metarecords_for_pristine_database () RETURNS VOID AS $$
320 INSERT INTO metabib.metarecord (fingerprint, master_record)
321 SELECT DISTINCT ON (b.fingerprint) b.fingerprint, b.id
322 FROM biblio.record_entry b
324 AND b.id IN (SELECT r.id FROM biblio.record_entry r LEFT JOIN metabib.metarecord_source_map k ON (k.source = r.id) WHERE k.id IS NULL AND r.fingerprint IS NOT NULL)
325 AND NOT EXISTS ( SELECT 1 FROM metabib.metarecord WHERE fingerprint = b.fingerprint )
326 ORDER BY b.fingerprint, b.quality DESC;
327 INSERT INTO metabib.metarecord_source_map (metarecord, source)
329 FROM biblio.record_entry r
330 JOIN metabib.metarecord m USING (fingerprint)
337 CREATE OR REPLACE FUNCTION migration_tools.insert_metarecords_for_incumbent_database () RETURNS VOID AS $$
340 INSERT INTO metabib.metarecord (fingerprint, master_record)
341 SELECT DISTINCT ON (b.fingerprint) b.fingerprint, b.id
342 FROM biblio.record_entry b
344 AND b.id IN (SELECT r.id FROM biblio.record_entry r LEFT JOIN metabib.metarecord_source_map k ON (k.source = r.id) WHERE k.id IS NULL AND r.fingerprint IS NOT NULL)
345 AND NOT EXISTS ( SELECT 1 FROM metabib.metarecord WHERE fingerprint = b.fingerprint )
346 ORDER BY b.fingerprint, b.quality DESC;
347 INSERT INTO metabib.metarecord_source_map (metarecord, source)
349 FROM biblio.record_entry r
350 JOIN metabib.metarecord m USING (fingerprint)
352 AND r.id IN (SELECT b.id FROM biblio.record_entry b LEFT JOIN metabib.metarecord_source_map k ON (k.source = b.id) WHERE k.id IS NULL);
357 CREATE OR REPLACE FUNCTION migration_tools.create_cards( schemaname TEXT ) RETURNS VOID AS $$
359 -- USAGE: Make sure the patrons are staged in schemaname.actor_usr_legacy and have 'usrname' assigned.
360 -- Then SELECT migration_tools.create_cards('m_foo');
363 u TEXT := schemaname || '.actor_usr_legacy';
364 c TEXT := schemaname || '.actor_card';
368 EXECUTE ('DELETE FROM ' || c || ';');
369 EXECUTE ('INSERT INTO ' || c || ' (usr, barcode) SELECT id, usrname FROM ' || u || ';');
370 EXECUTE ('UPDATE ' || u || ' u SET card = c.id FROM ' || c || ' c WHERE c.usr = u.id;');
378 CREATE OR REPLACE FUNCTION migration_tools.check_ou_depth ( ) RETURNS VOID AS $$
385 errors_found BOOLEAN;
387 parent_shortname TEXT;
393 type_parent_depth INT;
398 errors_found := FALSE;
400 -- Checking actor.org_unit_type
402 FOR type_id IN EXECUTE ('SELECT id FROM actor.org_unit_type ORDER BY id;') LOOP
404 SELECT depth FROM actor.org_unit_type WHERE id = type_id INTO type_depth;
405 SELECT parent FROM actor.org_unit_type WHERE id = type_id INTO type_parent;
407 IF type_parent IS NOT NULL THEN
409 SELECT depth FROM actor.org_unit_type WHERE id = type_parent INTO type_parent_depth;
411 IF type_depth - type_parent_depth <> 1 THEN
412 SELECT name FROM actor.org_unit_type WHERE id = type_id INTO ou_type_name;
413 SELECT name FROM actor.org_unit_type WHERE id = type_parent INTO parent_type;
414 RAISE INFO 'The % org unit type has a depth of %, but its parent org unit type, %, has a depth of %.',
415 ou_type_name, type_depth, parent_type, type_parent_depth;
416 errors_found := TRUE;
424 -- Checking actor.org_unit
426 FOR ou IN EXECUTE ('SELECT id FROM actor.org_unit ORDER BY shortname;') LOOP
428 SELECT parent_ou FROM actor.org_unit WHERE id = ou INTO ou_parent;
429 SELECT t.depth FROM actor.org_unit_type t, actor.org_unit o WHERE o.ou_type = t.id and o.id = ou INTO org_unit_depth;
430 SELECT t.depth FROM actor.org_unit_type t, actor.org_unit o WHERE o.ou_type = t.id and o.id = ou_parent INTO parent_depth;
431 SELECT shortname FROM actor.org_unit WHERE id = ou INTO ou_shortname;
432 SELECT shortname FROM actor.org_unit WHERE id = ou_parent INTO parent_shortname;
433 SELECT t.name FROM actor.org_unit_type t, actor.org_unit o WHERE o.ou_type = t.id and o.id = ou INTO ou_type_name;
434 SELECT t.name FROM actor.org_unit_type t, actor.org_unit o WHERE o.ou_type = t.id and o.id = ou_parent INTO parent_type;
436 IF ou_parent IS NOT NULL THEN
438 IF (org_unit_depth - parent_depth <> 1) OR (
439 (SELECT parent FROM actor.org_unit_type WHERE name = ou_type_name) <> (SELECT id FROM actor.org_unit_type WHERE name = parent_type)
441 RAISE INFO '% (org unit %) is a % (depth %) but its parent, % (org unit %), is a % (depth %).',
442 ou_shortname, ou, ou_type_name, org_unit_depth, parent_shortname, ou_parent, parent_type, parent_depth;
443 errors_found := TRUE;
450 IF NOT errors_found THEN
451 RAISE INFO 'No errors found.';
460 CREATE OR REPLACE FUNCTION migration_tools.simple_export_library_config(dir TEXT, orgs INT[]) RETURNS VOID AS $FUNC$
462 EXECUTE $$COPY (SELECT * FROM actor.hours_of_operation WHERE id IN ($$ ||
463 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
464 $$) TO '$$ || dir || $$/actor_hours_of_operation'$$;
465 EXECUTE $$COPY (SELECT org_unit, close_start, close_end, reason FROM actor.org_unit_closed WHERE org_unit IN ($$ ||
466 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
467 $$) TO '$$ || dir || $$/actor_org_unit_closed'$$;
468 EXECUTE $$COPY (SELECT org_unit, name, value FROM actor.org_unit_setting WHERE org_unit IN ($$ ||
469 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
470 $$) TO '$$ || dir || $$/actor_org_unit_setting'$$;
471 EXECUTE $$COPY (SELECT name, owning_lib, holdable, hold_verify, opac_visible, circulate FROM asset.copy_location WHERE owning_lib IN ($$ ||
472 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
473 $$) TO '$$ || dir || $$/asset_copy_location'$$;
474 EXECUTE $$COPY (SELECT grp, org_unit, penalty, threshold FROM permission.grp_penalty_threshold WHERE org_unit IN ($$ ||
475 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
476 $$) TO '$$ || dir || $$/permission_grp_penalty_threshold'$$;
477 EXECUTE $$COPY (SELECT owning_lib, label, label_sortkey FROM asset.call_number_prefix WHERE owning_lib IN ($$ ||
478 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
479 $$) TO '$$ || dir || $$/asset_call_number_prefix'$$;
480 EXECUTE $$COPY (SELECT owning_lib, label, label_sortkey FROM asset.call_number_suffix WHERE owning_lib IN ($$ ||
481 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
482 $$) TO '$$ || dir || $$/asset_call_number_suffix'$$;
483 EXECUTE $$COPY config.rule_circ_duration TO '$$ || dir || $$/config_rule_circ_duration'$$;
484 EXECUTE $$COPY config.rule_age_hold_protect TO '$$ || dir || $$/config_rule_age_hold_protect'$$;
485 EXECUTE $$COPY config.rule_max_fine TO '$$ || dir || $$/config_rule_max_fine'$$;
486 EXECUTE $$COPY config.rule_recurring_fine TO '$$ || dir || $$/config_rule_recurring_fine'$$;
487 EXECUTE $$COPY permission.grp_tree TO '$$ || dir || $$/permission_grp_tree'$$;
489 $FUNC$ LANGUAGE PLPGSQL;
491 CREATE OR REPLACE FUNCTION migration_tools.simple_import_library_config(dir TEXT) RETURNS VOID AS $FUNC$
493 EXECUTE $$COPY actor.hours_of_operation FROM '$$ || dir || $$/actor_hours_of_operation'$$;
494 EXECUTE $$COPY actor.org_unit_closed (org_unit, close_start, close_end, reason) FROM '$$ || dir || $$/actor_org_unit_closed'$$;
495 EXECUTE $$COPY actor.org_unit_setting (org_unit, name, value) FROM '$$ || dir || $$/actor_org_unit_setting'$$;
496 EXECUTE $$COPY asset.copy_location (name, owning_lib, holdable, hold_verify, opac_visible, circulate) FROM '$$ || dir || $$/asset_copy_location'$$;
497 EXECUTE $$COPY permission.grp_penalty_threshold (grp, org_unit, penalty, threshold) FROM '$$ || dir || $$/permission_grp_penalty_threshold'$$;
498 EXECUTE $$COPY asset.call_number_prefix (owning_lib, label, label_sortkey) FROM '$$ || dir || $$/asset_call_number_prefix'$$;
499 EXECUTE $$COPY asset.call_number_suffix (owning_lib, label, label_sortkey) FROM '$$ || dir || $$/asset_call_number_suffix'$$;
501 -- import any new circ rules
502 PERFORM migration_tools.simple_import_new_rows_by_value(dir, 'config', 'rule_circ_duration', 'id', 'name');
503 PERFORM migration_tools.simple_import_new_rows_by_value(dir, 'config', 'rule_age_hold_protect', 'id', 'name');
504 PERFORM migration_tools.simple_import_new_rows_by_value(dir, 'config', 'rule_max_fine', 'id', 'name');
505 PERFORM migration_tools.simple_import_new_rows_by_value(dir, 'config', 'rule_recurring_fine', 'id', 'name');
507 -- and permission groups
508 PERFORM migration_tools.simple_import_new_rows_by_value(dir, 'permission', 'grp_tree', 'id', 'name');
511 $FUNC$ LANGUAGE PLPGSQL;
513 -- example: SELECT * FROM migration_tools.duplicate_template(5,'{3,4}');
514 CREATE OR REPLACE FUNCTION migration_tools.duplicate_template (INTEGER, INTEGER[]) RETURNS VOID AS $$
516 target_event_def ALIAS FOR $1;
519 DROP TABLE IF EXISTS new_atevdefs;
520 CREATE TEMP TABLE new_atevdefs (atevdef INTEGER);
521 FOR i IN array_lower(orgs,1) .. array_upper(orgs,1) LOOP
522 INSERT INTO action_trigger.event_definition (
543 ,name || ' (clone of '||target_event_def||')'
559 action_trigger.event_definition
561 id = target_event_def
563 RAISE INFO 'created atevdef with id = %', currval('action_trigger.event_definition_id_seq');
564 INSERT INTO new_atevdefs SELECT currval('action_trigger.event_definition_id_seq');
565 INSERT INTO action_trigger.environment (
571 currval('action_trigger.event_definition_id_seq')
576 action_trigger.environment
578 event_def = target_event_def
580 INSERT INTO action_trigger.event_params (
585 currval('action_trigger.event_definition_id_seq')
589 action_trigger.event_params
591 event_def = target_event_def
594 RAISE INFO '-- UPDATE action_trigger.event_definition SET active = CASE WHEN id = % THEN FALSE ELSE TRUE END WHERE id in (%,%);', target_event_def, target_event_def, (SELECT array_to_string(array_agg(atevdef),',') from new_atevdefs);
596 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
598 -- example: SELECT * FROM migration_tools.duplicate_template_but_change_delay(5,'{3,4}','00:30:00'::INTERVAL);
599 CREATE OR REPLACE FUNCTION migration_tools.duplicate_template_but_change_delay (INTEGER, INTEGER[], INTERVAL) RETURNS VOID AS $$
601 target_event_def ALIAS FOR $1;
603 new_interval ALIAS FOR $3;
605 DROP TABLE IF EXISTS new_atevdefs;
606 CREATE TEMP TABLE new_atevdefs (atevdef INTEGER);
607 FOR i IN array_lower(orgs,1) .. array_upper(orgs,1) LOOP
608 INSERT INTO action_trigger.event_definition (
629 ,name || ' (clone of '||target_event_def||')'
645 action_trigger.event_definition
647 id = target_event_def
649 RAISE INFO 'created atevdef with id = %', currval('action_trigger.event_definition_id_seq');
650 INSERT INTO new_atevdefs SELECT currval('action_trigger.event_definition_id_seq');
651 INSERT INTO action_trigger.environment (
657 currval('action_trigger.event_definition_id_seq')
662 action_trigger.environment
664 event_def = target_event_def
666 INSERT INTO action_trigger.event_params (
671 currval('action_trigger.event_definition_id_seq')
675 action_trigger.event_params
677 event_def = target_event_def
680 RAISE INFO '-- UPDATE action_trigger.event_definition SET active = CASE WHEN id = % THEN FALSE ELSE TRUE END WHERE id in (%,%);', target_event_def, target_event_def, (SELECT array_to_string(array_agg(atevdef),',') from new_atevdefs);
682 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
684 -- example: SELECT * FROM migration_tools.duplicate_templates(3,'{5,6}');
685 CREATE OR REPLACE FUNCTION migration_tools.duplicate_templates (INTEGER, INTEGER[]) RETURNS VOID AS $$
688 target_event_defs ALIAS FOR $2;
690 DROP TABLE IF EXISTS new_atevdefs;
691 CREATE TEMP TABLE new_atevdefs (atevdef INTEGER);
692 FOR i IN array_lower(target_event_defs,1) .. array_upper(target_event_defs,1) LOOP
693 INSERT INTO action_trigger.event_definition (
714 ,name || ' (clone of '||target_event_defs[i]||')'
730 action_trigger.event_definition
732 id = target_event_defs[i]
734 RAISE INFO 'created atevdef with id = %', currval('action_trigger.event_definition_id_seq');
735 INSERT INTO new_atevdefs SELECT currval('action_trigger.event_definition_id_seq');
736 INSERT INTO action_trigger.environment (
742 currval('action_trigger.event_definition_id_seq')
747 action_trigger.environment
749 event_def = target_event_defs[i]
751 INSERT INTO action_trigger.event_params (
756 currval('action_trigger.event_definition_id_seq')
760 action_trigger.event_params
762 event_def = target_event_defs[i]
765 RAISE INFO '-- UPDATE action_trigger.event_definition SET active = TRUE WHERE id in (%);', (SELECT array_to_string(array_agg(atevdef),',') from new_atevdefs);
767 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
769 CREATE OR REPLACE FUNCTION migration_tools.reset_event (BIGINT) RETURNS VOID AS $$
775 ,complete_time = NULL
776 ,update_process = NULL
778 ,template_output = NULL
785 CREATE OR REPLACE FUNCTION migration_tools.find_hold_matrix_matchpoint (INTEGER) RETURNS INTEGER AS $$
786 SELECT action.find_hold_matrix_matchpoint(
787 (SELECT pickup_lib FROM action.hold_request WHERE id = $1),
788 (SELECT request_lib FROM action.hold_request WHERE id = $1),
789 (SELECT current_copy FROM action.hold_request WHERE id = $1),
790 (SELECT usr FROM action.hold_request WHERE id = $1),
791 (SELECT requestor FROM action.hold_request WHERE id = $1)
795 CREATE OR REPLACE FUNCTION migration_tools.find_hold_matrix_matchpoint2 (INTEGER) RETURNS SETOF action.matrix_test_result AS $$
796 SELECT action.hold_request_permit_test(
797 (SELECT pickup_lib FROM action.hold_request WHERE id = $1),
798 (SELECT request_lib FROM action.hold_request WHERE id = $1),
799 (SELECT current_copy FROM action.hold_request WHERE id = $1),
800 (SELECT usr FROM action.hold_request WHERE id = $1),
801 (SELECT requestor FROM action.hold_request WHERE id = $1)
805 CREATE OR REPLACE FUNCTION migration_tools.find_circ_matrix_matchpoint (INTEGER) RETURNS SETOF action.found_circ_matrix_matchpoint AS $$
806 SELECT action.find_circ_matrix_matchpoint(
807 (SELECT circ_lib FROM action.circulation WHERE id = $1),
808 (SELECT target_copy FROM action.circulation WHERE id = $1),
809 (SELECT usr FROM action.circulation WHERE id = $1),
811 NULLIF(phone_renewal,false),
812 NULLIF(desk_renewal,false),
813 NULLIF(opac_renewal,false),
815 ) FROM action.circulation WHERE id = $1
820 -- set a new salted password
822 CREATE OR REPLACE FUNCTION migration_tools.set_salted_passwd(INTEGER,TEXT) RETURNS BOOLEAN AS $$
825 plain_passwd ALIAS FOR $2;
830 SELECT actor.create_salt('main') INTO plain_salt;
832 SELECT MD5(plain_passwd) INTO md5_passwd;
834 PERFORM actor.set_passwd(usr_id, 'main', MD5(plain_salt || md5_passwd), plain_salt);
839 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
841 -- convenience functions for handling copy_location maps
842 CREATE OR REPLACE FUNCTION migration_tools.handle_shelf (TEXT,TEXT,TEXT,INTEGER) RETURNS VOID AS $$
843 SELECT migration_tools._handle_shelf($1,$2,$3,$4,TRUE);
846 CREATE OR REPLACE FUNCTION migration_tools._handle_shelf (TEXT,TEXT,TEXT,INTEGER,BOOLEAN) RETURNS VOID AS $$
848 table_schema ALIAS FOR $1;
849 table_name ALIAS FOR $2;
850 org_shortname ALIAS FOR $3;
851 org_range ALIAS FOR $4;
852 make_assertion ALIAS FOR $5;
855 -- if x_org is on the mapping table, it'll take precedence over the passed org_shortname param
856 -- though we'll still use the passed org for the full path traversal when needed
863 EXECUTE 'SELECT EXISTS (
865 FROM information_schema.columns
866 WHERE table_schema = $1
868 and column_name = ''desired_shelf''
869 )' INTO proceed USING table_schema, table_name;
871 RAISE EXCEPTION 'Missing column desired_shelf';
874 EXECUTE 'SELECT EXISTS (
876 FROM information_schema.columns
877 WHERE table_schema = $1
879 and column_name = ''x_org''
880 )' INTO x_org_found USING table_schema, table_name;
882 SELECT id INTO org FROM actor.org_unit WHERE shortname = org_shortname;
884 RAISE EXCEPTION 'Cannot find org by shortname';
887 SELECT INTO org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( org );
889 EXECUTE 'ALTER TABLE '
890 || quote_ident(table_name)
891 || ' DROP COLUMN IF EXISTS x_shelf';
892 EXECUTE 'ALTER TABLE '
893 || quote_ident(table_name)
894 || ' ADD COLUMN x_shelf INTEGER';
897 RAISE INFO 'Found x_org column';
898 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
899 || ' SET x_shelf = b.id FROM asset_copy_location b'
900 || ' WHERE BTRIM(UPPER(a.desired_shelf)) = BTRIM(UPPER(b.name))'
901 || ' AND b.owning_lib = x_org'
902 || ' AND NOT b.deleted';
903 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
904 || ' SET x_shelf = b.id FROM asset.copy_location b'
905 || ' WHERE BTRIM(UPPER(a.desired_shelf)) = BTRIM(UPPER(b.name))'
906 || ' AND b.owning_lib = x_org'
907 || ' AND x_shelf IS NULL'
908 || ' AND NOT b.deleted';
910 RAISE INFO 'Did not find x_org column';
911 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
912 || ' SET x_shelf = b.id FROM asset_copy_location b'
913 || ' WHERE BTRIM(UPPER(a.desired_shelf)) = BTRIM(UPPER(b.name))'
914 || ' AND b.owning_lib = $1'
915 || ' AND NOT b.deleted'
917 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
918 || ' SET x_shelf = b.id FROM asset_copy_location b'
919 || ' WHERE BTRIM(UPPER(a.desired_shelf)) = BTRIM(UPPER(b.name))'
920 || ' AND b.owning_lib = $1'
921 || ' AND x_shelf IS NULL'
922 || ' AND NOT b.deleted'
926 FOREACH o IN ARRAY org_list LOOP
927 RAISE INFO 'Considering org %', o;
928 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
929 || ' SET x_shelf = b.id FROM asset.copy_location b'
930 || ' WHERE BTRIM(UPPER(a.desired_shelf)) = BTRIM(UPPER(b.name))'
931 || ' AND b.owning_lib = $1 AND x_shelf IS NULL'
932 || ' AND NOT b.deleted'
934 GET DIAGNOSTICS row_count = ROW_COUNT;
935 RAISE INFO 'Updated % rows', row_count;
938 IF make_assertion THEN
939 EXECUTE 'SELECT migration_tools.assert(
940 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_shelf <> '''' AND x_shelf IS NULL),
941 ''Cannot find a desired location'',
942 ''Found all desired locations''
947 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
949 -- convenience functions for handling circmod maps
951 CREATE OR REPLACE FUNCTION migration_tools.handle_circmod (TEXT,TEXT) RETURNS VOID AS $$
953 table_schema ALIAS FOR $1;
954 table_name ALIAS FOR $2;
957 EXECUTE 'SELECT EXISTS (
959 FROM information_schema.columns
960 WHERE table_schema = $1
962 and column_name = ''desired_circmod''
963 )' INTO proceed USING table_schema, table_name;
965 RAISE EXCEPTION 'Missing column desired_circmod';
968 EXECUTE 'ALTER TABLE '
969 || quote_ident(table_name)
970 || ' DROP COLUMN IF EXISTS x_circmod';
971 EXECUTE 'ALTER TABLE '
972 || quote_ident(table_name)
973 || ' ADD COLUMN x_circmod TEXT';
975 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
976 || ' SET x_circmod = code FROM config.circ_modifier b'
977 || ' WHERE BTRIM(UPPER(a.desired_circmod)) = BTRIM(UPPER(b.code))';
979 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
980 || ' SET x_circmod = code FROM config.circ_modifier b'
981 || ' WHERE BTRIM(UPPER(a.desired_circmod)) = BTRIM(UPPER(b.name))'
982 || ' AND x_circmod IS NULL';
984 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
985 || ' SET x_circmod = code FROM config.circ_modifier b'
986 || ' WHERE BTRIM(UPPER(a.desired_circmod)) = BTRIM(UPPER(b.description))'
987 || ' AND x_circmod IS NULL';
989 EXECUTE 'SELECT migration_tools.assert(
990 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_circmod <> '''' AND x_circmod IS NULL),
991 ''Cannot find a desired circulation modifier'',
992 ''Found all desired circulation modifiers''
996 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
998 -- convenience functions for handling item status maps
1000 CREATE OR REPLACE FUNCTION migration_tools.handle_status (TEXT,TEXT) RETURNS VOID AS $$
1002 table_schema ALIAS FOR $1;
1003 table_name ALIAS FOR $2;
1006 EXECUTE 'SELECT EXISTS (
1008 FROM information_schema.columns
1009 WHERE table_schema = $1
1011 and column_name = ''desired_status''
1012 )' INTO proceed USING table_schema, table_name;
1014 RAISE EXCEPTION 'Missing column desired_status';
1017 EXECUTE 'ALTER TABLE '
1018 || quote_ident(table_name)
1019 || ' DROP COLUMN IF EXISTS x_status';
1020 EXECUTE 'ALTER TABLE '
1021 || quote_ident(table_name)
1022 || ' ADD COLUMN x_status INTEGER';
1024 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1025 || ' SET x_status = id FROM config.copy_status b'
1026 || ' WHERE BTRIM(UPPER(a.desired_status)) = BTRIM(UPPER(b.name))';
1028 EXECUTE 'SELECT migration_tools.assert(
1029 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_status <> '''' AND x_status IS NULL),
1030 ''Cannot find a desired copy status'',
1031 ''Found all desired copy statuses''
1035 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1037 -- convenience functions for handling org maps
1039 CREATE OR REPLACE FUNCTION migration_tools.handle_org (TEXT,TEXT) RETURNS VOID AS $$
1041 table_schema ALIAS FOR $1;
1042 table_name ALIAS FOR $2;
1045 EXECUTE 'SELECT EXISTS (
1047 FROM information_schema.columns
1048 WHERE table_schema = $1
1050 and column_name = ''desired_org''
1051 )' INTO proceed USING table_schema, table_name;
1053 RAISE EXCEPTION 'Missing column desired_org';
1056 EXECUTE 'ALTER TABLE '
1057 || quote_ident(table_name)
1058 || ' DROP COLUMN IF EXISTS x_org';
1059 EXECUTE 'ALTER TABLE '
1060 || quote_ident(table_name)
1061 || ' ADD COLUMN x_org INTEGER';
1063 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1064 || ' SET x_org = b.id FROM actor.org_unit b'
1065 || ' WHERE BTRIM(a.desired_org) = BTRIM(b.shortname)';
1067 EXECUTE 'SELECT migration_tools.assert(
1068 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_org <> '''' AND x_org IS NULL),
1069 ''Cannot find a desired org unit'',
1070 ''Found all desired org units''
1074 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1076 -- convenience function for handling desired_not_migrate
1078 CREATE OR REPLACE FUNCTION migration_tools.handle_not_migrate (TEXT,TEXT) RETURNS VOID AS $$
1080 table_schema ALIAS FOR $1;
1081 table_name ALIAS FOR $2;
1084 EXECUTE 'SELECT EXISTS (
1086 FROM information_schema.columns
1087 WHERE table_schema = $1
1089 and column_name = ''desired_not_migrate''
1090 )' INTO proceed USING table_schema, table_name;
1092 RAISE EXCEPTION 'Missing column desired_not_migrate';
1095 EXECUTE 'ALTER TABLE '
1096 || quote_ident(table_name)
1097 || ' DROP COLUMN IF EXISTS x_migrate';
1098 EXECUTE 'ALTER TABLE '
1099 || quote_ident(table_name)
1100 || ' ADD COLUMN x_migrate BOOLEAN';
1102 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1103 || ' SET x_migrate = CASE'
1104 || ' WHEN BTRIM(desired_not_migrate) = ''TRUE'' THEN FALSE'
1105 || ' WHEN BTRIM(desired_not_migrate) = ''DNM'' THEN FALSE'
1106 || ' WHEN BTRIM(desired_not_migrate) = ''Do Not Migrate'' THEN FALSE'
1107 || ' WHEN BTRIM(desired_not_migrate) = ''FALSE'' THEN TRUE'
1108 || ' WHEN BTRIM(desired_not_migrate) = ''Migrate'' THEN TRUE'
1109 || ' WHEN BTRIM(desired_not_migrate) = '''' THEN TRUE'
1112 EXECUTE 'SELECT migration_tools.assert(
1113 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE x_migrate IS NULL),
1114 ''Not all desired_not_migrate values understood'',
1115 ''All desired_not_migrate values understood''
1119 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1121 -- convenience function for handling desired_not_migrate
1123 CREATE OR REPLACE FUNCTION migration_tools.handle_barred_or_blocked (TEXT,TEXT) RETURNS VOID AS $$
1125 table_schema ALIAS FOR $1;
1126 table_name ALIAS FOR $2;
1129 EXECUTE 'SELECT EXISTS (
1131 FROM information_schema.columns
1132 WHERE table_schema = $1
1134 and column_name = ''desired_barred_or_blocked''
1135 )' INTO proceed USING table_schema, table_name;
1137 RAISE EXCEPTION 'Missing column desired_barred_or_blocked';
1140 EXECUTE 'ALTER TABLE '
1141 || quote_ident(table_name)
1142 || ' DROP COLUMN IF EXISTS x_barred';
1143 EXECUTE 'ALTER TABLE '
1144 || quote_ident(table_name)
1145 || ' ADD COLUMN x_barred BOOLEAN';
1147 EXECUTE 'ALTER TABLE '
1148 || quote_ident(table_name)
1149 || ' DROP COLUMN IF EXISTS x_blocked';
1150 EXECUTE 'ALTER TABLE '
1151 || quote_ident(table_name)
1152 || ' ADD COLUMN x_blocked BOOLEAN';
1154 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1155 || ' SET x_barred = CASE'
1156 || ' WHEN BTRIM(desired_barred_or_blocked) = ''Barred'' THEN TRUE'
1157 || ' WHEN BTRIM(desired_barred_or_blocked) = ''Blocked'' THEN FALSE'
1158 || ' WHEN BTRIM(desired_barred_or_blocked) = ''Neither'' THEN FALSE'
1159 || ' WHEN BTRIM(desired_barred_or_blocked) = '''' THEN FALSE'
1162 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1163 || ' SET x_blocked = CASE'
1164 || ' WHEN BTRIM(desired_barred_or_blocked) = ''Blocked'' THEN TRUE'
1165 || ' WHEN BTRIM(desired_barred_or_blocked) = ''Barred'' THEN FALSE'
1166 || ' WHEN BTRIM(desired_barred_or_blocked) = ''Neither'' THEN FALSE'
1167 || ' WHEN BTRIM(desired_barred_or_blocked) = '''' THEN FALSE'
1170 EXECUTE 'SELECT migration_tools.assert(
1171 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE x_barred IS NULL or x_blocked IS NULL),
1172 ''Not all desired_barred_or_blocked values understood'',
1173 ''All desired_barred_or_blocked values understood''
1177 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1179 -- convenience function for handling desired_profile
1181 CREATE OR REPLACE FUNCTION migration_tools.handle_profile (TEXT,TEXT) RETURNS VOID AS $$
1183 table_schema ALIAS FOR $1;
1184 table_name ALIAS FOR $2;
1187 EXECUTE 'SELECT EXISTS (
1189 FROM information_schema.columns
1190 WHERE table_schema = $1
1192 and column_name = ''desired_profile''
1193 )' INTO proceed USING table_schema, table_name;
1195 RAISE EXCEPTION 'Missing column desired_profile';
1198 EXECUTE 'ALTER TABLE '
1199 || quote_ident(table_name)
1200 || ' DROP COLUMN IF EXISTS x_profile';
1201 EXECUTE 'ALTER TABLE '
1202 || quote_ident(table_name)
1203 || ' ADD COLUMN x_profile INTEGER';
1205 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1206 || ' SET x_profile = b.id FROM permission.grp_tree b'
1207 || ' WHERE BTRIM(UPPER(a.desired_profile)) = BTRIM(UPPER(b.name))';
1209 EXECUTE 'SELECT migration_tools.assert(
1210 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_profile <> '''' AND x_profile IS NULL),
1211 ''Cannot find a desired profile'',
1212 ''Found all desired profiles''
1216 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1218 -- convenience function for handling desired actor stat cats
1220 CREATE OR REPLACE FUNCTION migration_tools.vivicate_actor_sc_and_sce (TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1222 table_schema ALIAS FOR $1;
1223 table_name ALIAS FOR $2;
1224 field_suffix ALIAS FOR $3; -- for distinguishing between desired_sce1, desired_sce2, etc.
1225 org_shortname ALIAS FOR $4;
1233 SELECT 'desired_sc' || field_suffix INTO sc;
1234 SELECT 'desired_sce' || field_suffix INTO sce;
1236 EXECUTE 'SELECT EXISTS (
1238 FROM information_schema.columns
1239 WHERE table_schema = $1
1241 and column_name = $3
1242 )' INTO proceed USING table_schema, table_name, sc;
1244 RAISE EXCEPTION 'Missing column %', sc;
1246 EXECUTE 'SELECT EXISTS (
1248 FROM information_schema.columns
1249 WHERE table_schema = $1
1251 and column_name = $3
1252 )' INTO proceed USING table_schema, table_name, sce;
1254 RAISE EXCEPTION 'Missing column %', sce;
1257 SELECT id INTO org FROM actor.org_unit WHERE shortname = org_shortname;
1259 RAISE EXCEPTION 'Cannot find org by shortname';
1261 SELECT INTO org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( org );
1263 -- caller responsible for their own truncates though we try to prevent duplicates
1264 EXECUTE 'INSERT INTO actor_stat_cat (owner, name)
1269 ' || quote_ident(table_name) || '
1271 NULLIF(BTRIM('||sc||'),'''') IS NOT NULL
1275 WHERE owner = ANY ($2)
1276 AND name = BTRIM('||sc||')
1281 WHERE owner = ANY ($2)
1282 AND name = BTRIM('||sc||')
1285 USING org, org_list;
1287 EXECUTE 'INSERT INTO actor_stat_cat_entry (stat_cat, owner, value)
1292 WHERE owner = ANY ($2)
1293 AND BTRIM('||sc||') = BTRIM(name))
1296 WHERE owner = ANY ($2)
1297 AND BTRIM('||sc||') = BTRIM(name))
1302 ' || quote_ident(table_name) || '
1304 NULLIF(BTRIM('||sc||'),'''') IS NOT NULL
1305 AND NULLIF(BTRIM('||sce||'),'''') IS NOT NULL
1308 FROM actor.stat_cat_entry
1312 WHERE owner = ANY ($2)
1313 AND BTRIM('||sc||') = BTRIM(name)
1314 ) AND value = BTRIM('||sce||')
1315 AND owner = ANY ($2)
1319 FROM actor_stat_cat_entry
1323 WHERE owner = ANY ($2)
1324 AND BTRIM('||sc||') = BTRIM(name)
1325 ) AND value = BTRIM('||sce||')
1326 AND owner = ANY ($2)
1329 USING org, org_list;
1331 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1333 CREATE OR REPLACE FUNCTION migration_tools.handle_actor_sc_and_sce (TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1335 table_schema ALIAS FOR $1;
1336 table_name ALIAS FOR $2;
1337 field_suffix ALIAS FOR $3; -- for distinguishing between desired_sce1, desired_sce2, etc.
1338 org_shortname ALIAS FOR $4;
1346 SELECT 'desired_sc' || field_suffix INTO sc;
1347 SELECT 'desired_sce' || field_suffix INTO sce;
1348 EXECUTE 'SELECT EXISTS (
1350 FROM information_schema.columns
1351 WHERE table_schema = $1
1353 and column_name = $3
1354 )' INTO proceed USING table_schema, table_name, sc;
1356 RAISE EXCEPTION 'Missing column %', sc;
1358 EXECUTE 'SELECT EXISTS (
1360 FROM information_schema.columns
1361 WHERE table_schema = $1
1363 and column_name = $3
1364 )' INTO proceed USING table_schema, table_name, sce;
1366 RAISE EXCEPTION 'Missing column %', sce;
1369 SELECT id INTO org FROM actor.org_unit WHERE shortname = org_shortname;
1371 RAISE EXCEPTION 'Cannot find org by shortname';
1374 SELECT INTO org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( org );
1376 EXECUTE 'ALTER TABLE '
1377 || quote_ident(table_name)
1378 || ' DROP COLUMN IF EXISTS x_sc' || field_suffix;
1379 EXECUTE 'ALTER TABLE '
1380 || quote_ident(table_name)
1381 || ' ADD COLUMN x_sc' || field_suffix || ' INTEGER';
1382 EXECUTE 'ALTER TABLE '
1383 || quote_ident(table_name)
1384 || ' DROP COLUMN IF EXISTS x_sce' || field_suffix;
1385 EXECUTE 'ALTER TABLE '
1386 || quote_ident(table_name)
1387 || ' ADD COLUMN x_sce' || field_suffix || ' INTEGER';
1390 EXECUTE 'UPDATE ' || quote_ident(table_name) || '
1392 x_sc' || field_suffix || ' = id
1394 (SELECT id, name, owner FROM actor_stat_cat
1395 UNION SELECT id, name, owner FROM actor.stat_cat) u
1397 BTRIM(UPPER(u.name)) = BTRIM(UPPER(' || sc || '))
1398 AND u.owner = ANY ($1);'
1401 EXECUTE 'UPDATE ' || quote_ident(table_name) || '
1403 x_sce' || field_suffix || ' = id
1405 (SELECT id, stat_cat, owner, value FROM actor_stat_cat_entry
1406 UNION SELECT id, stat_cat, owner, value FROM actor.stat_cat_entry) u
1408 u.stat_cat = x_sc' || field_suffix || '
1409 AND BTRIM(UPPER(u.value)) = BTRIM(UPPER(' || sce || '))
1410 AND u.owner = ANY ($1);'
1413 EXECUTE 'SELECT migration_tools.assert(
1414 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_sc' || field_suffix || ' <> '''' AND x_sc' || field_suffix || ' IS NULL),
1415 ''Cannot find a desired stat cat'',
1416 ''Found all desired stat cats''
1419 EXECUTE 'SELECT migration_tools.assert(
1420 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_sce' || field_suffix || ' <> '''' AND x_sce' || field_suffix || ' IS NULL),
1421 ''Cannot find a desired stat cat entry'',
1422 ''Found all desired stat cat entries''
1426 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1428 -- convenience functions for adding shelving locations
1429 DROP FUNCTION IF EXISTS migration_tools.find_shelf(INT,TEXT);
1430 CREATE OR REPLACE FUNCTION migration_tools.find_shelf(org_id INT, shelf_name TEXT) RETURNS INTEGER AS $$
1436 SELECT INTO d MAX(distance) FROM actor.org_unit_ancestors_distance(org_id);
1439 SELECT INTO cur_id id FROM actor.org_unit_ancestor_at_depth(org_id,d);
1440 SELECT INTO return_id id FROM asset.copy_location WHERE owning_lib = cur_id AND name ILIKE shelf_name;
1441 IF return_id IS NOT NULL THEN
1449 $$ LANGUAGE plpgsql;
1451 -- may remove later but testing using this with new migration scripts and not loading acls until go live
1453 DROP FUNCTION IF EXISTS migration_tools.find_mig_shelf(INT,TEXT);
1454 CREATE OR REPLACE FUNCTION migration_tools.find_mig_shelf(org_id INT, shelf_name TEXT) RETURNS INTEGER AS $$
1460 SELECT INTO d MAX(distance) FROM actor.org_unit_ancestors_distance(org_id);
1463 SELECT INTO cur_id id FROM actor.org_unit_ancestor_at_depth(org_id,d);
1465 SELECT INTO return_id id FROM
1466 (SELECT * FROM asset.copy_location UNION ALL SELECT * FROM asset_copy_location) x
1467 WHERE owning_lib = cur_id AND name ILIKE shelf_name;
1468 IF return_id IS NOT NULL THEN
1476 $$ LANGUAGE plpgsql;
1478 -- convenience function for linking to the item staging table
1480 CREATE OR REPLACE FUNCTION migration_tools.handle_item_barcode (TEXT,TEXT,TEXT,TEXT,BOOLEAN) RETURNS VOID AS $$
1482 table_schema ALIAS FOR $1;
1483 table_name ALIAS FOR $2;
1484 foreign_column_name ALIAS FOR $3;
1485 main_column_name ALIAS FOR $4;
1486 btrim_desired ALIAS FOR $5;
1489 EXECUTE 'SELECT EXISTS (
1491 FROM information_schema.columns
1492 WHERE table_schema = $1
1494 and column_name = $3
1495 )' INTO proceed USING table_schema, table_name, foreign_column_name;
1497 RAISE EXCEPTION '%.% missing column %', table_schema, table_name, foreign_column_name;
1500 EXECUTE 'SELECT EXISTS (
1502 FROM information_schema.columns
1503 WHERE table_schema = $1
1504 AND table_name = ''asset_copy_legacy''
1505 and column_name = $2
1506 )' INTO proceed USING table_schema, main_column_name;
1508 RAISE EXCEPTION 'No %.asset_copy_legacy with column %', table_schema, main_column_name;
1511 EXECUTE 'ALTER TABLE '
1512 || quote_ident(table_name)
1513 || ' DROP COLUMN IF EXISTS x_item';
1514 EXECUTE 'ALTER TABLE '
1515 || quote_ident(table_name)
1516 || ' ADD COLUMN x_item BIGINT';
1518 IF btrim_desired THEN
1519 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1520 || ' SET x_item = b.id FROM asset_copy_legacy b'
1521 || ' WHERE BTRIM(a.' || quote_ident(foreign_column_name)
1522 || ') = BTRIM(b.' || quote_ident(main_column_name) || ')';
1524 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1525 || ' SET x_item = b.id FROM asset_copy_legacy b'
1526 || ' WHERE a.' || quote_ident(foreign_column_name)
1527 || ' = b.' || quote_ident(main_column_name);
1530 --EXECUTE 'SELECT migration_tools.assert(
1531 -- NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE ' || quote_ident(foreign_column_name) || ' <> '''' AND x_item IS NULL),
1532 -- ''Cannot link every barcode'',
1533 -- ''Every barcode linked''
1537 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1539 -- convenience function for linking to the user staging table
1541 CREATE OR REPLACE FUNCTION migration_tools.handle_user_barcode (TEXT,TEXT,TEXT,TEXT,BOOLEAN) RETURNS VOID AS $$
1543 table_schema ALIAS FOR $1;
1544 table_name ALIAS FOR $2;
1545 foreign_column_name ALIAS FOR $3;
1546 main_column_name ALIAS FOR $4;
1547 btrim_desired ALIAS FOR $5;
1550 EXECUTE 'SELECT EXISTS (
1552 FROM information_schema.columns
1553 WHERE table_schema = $1
1555 and column_name = $3
1556 )' INTO proceed USING table_schema, table_name, foreign_column_name;
1558 RAISE EXCEPTION '%.% missing column %', table_schema, table_name, foreign_column_name;
1561 EXECUTE 'SELECT EXISTS (
1563 FROM information_schema.columns
1564 WHERE table_schema = $1
1565 AND table_name = ''actor_usr_legacy''
1566 and column_name = $2
1567 )' INTO proceed USING table_schema, main_column_name;
1569 RAISE EXCEPTION 'No %.actor_usr_legacy with column %', table_schema, main_column_name;
1572 EXECUTE 'ALTER TABLE '
1573 || quote_ident(table_name)
1574 || ' DROP COLUMN IF EXISTS x_user';
1575 EXECUTE 'ALTER TABLE '
1576 || quote_ident(table_name)
1577 || ' ADD COLUMN x_user INTEGER';
1579 IF btrim_desired THEN
1580 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1581 || ' SET x_user = b.id FROM actor_usr_legacy b'
1582 || ' WHERE BTRIM(a.' || quote_ident(foreign_column_name)
1583 || ') = BTRIM(b.' || quote_ident(main_column_name) || ')';
1585 EXECUTE 'UPDATE ' || quote_ident(table_name) || ' a'
1586 || ' SET x_user = b.id FROM actor_usr_legacy b'
1587 || ' WHERE a.' || quote_ident(foreign_column_name)
1588 || ' = b.' || quote_ident(main_column_name);
1591 --EXECUTE 'SELECT migration_tools.assert(
1592 -- NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE ' || quote_ident(foreign_column_name) || ' <> '''' AND x_user IS NULL),
1593 -- ''Cannot link every barcode'',
1594 -- ''Every barcode linked''
1598 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1600 -- convenience function for handling desired asset stat cats
1602 CREATE OR REPLACE FUNCTION migration_tools.vivicate_asset_sc_and_sce (TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1604 table_schema ALIAS FOR $1;
1605 table_name ALIAS FOR $2;
1606 field_suffix ALIAS FOR $3; -- for distinguishing between desired_sce1, desired_sce2, etc.
1607 org_shortname ALIAS FOR $4;
1615 SELECT 'desired_sc' || field_suffix INTO sc;
1616 SELECT 'desired_sce' || field_suffix INTO sce;
1618 EXECUTE 'SELECT EXISTS (
1620 FROM information_schema.columns
1621 WHERE table_schema = $1
1623 and column_name = $3
1624 )' INTO proceed USING table_schema, table_name, sc;
1626 RAISE EXCEPTION 'Missing column %', sc;
1628 EXECUTE 'SELECT EXISTS (
1630 FROM information_schema.columns
1631 WHERE table_schema = $1
1633 and column_name = $3
1634 )' INTO proceed USING table_schema, table_name, sce;
1636 RAISE EXCEPTION 'Missing column %', sce;
1639 SELECT id INTO org FROM actor.org_unit WHERE shortname = org_shortname;
1641 RAISE EXCEPTION 'Cannot find org by shortname';
1643 SELECT INTO org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( org );
1645 -- caller responsible for their own truncates though we try to prevent duplicates
1646 EXECUTE 'INSERT INTO asset_stat_cat (owner, name)
1651 ' || quote_ident(table_name) || '
1653 NULLIF(BTRIM('||sc||'),'''') IS NOT NULL
1657 WHERE owner = ANY ($2)
1658 AND name = BTRIM('||sc||')
1663 WHERE owner = ANY ($2)
1664 AND name = BTRIM('||sc||')
1667 USING org, org_list;
1669 EXECUTE 'INSERT INTO asset_stat_cat_entry (stat_cat, owner, value)
1674 WHERE owner = ANY ($2)
1675 AND BTRIM('||sc||') = BTRIM(name))
1678 WHERE owner = ANY ($2)
1679 AND BTRIM('||sc||') = BTRIM(name))
1684 ' || quote_ident(table_name) || '
1686 NULLIF(BTRIM('||sc||'),'''') IS NOT NULL
1687 AND NULLIF(BTRIM('||sce||'),'''') IS NOT NULL
1690 FROM asset.stat_cat_entry
1694 WHERE owner = ANY ($2)
1695 AND BTRIM('||sc||') = BTRIM(name)
1696 ) AND value = BTRIM('||sce||')
1697 AND owner = ANY ($2)
1701 FROM asset_stat_cat_entry
1705 WHERE owner = ANY ($2)
1706 AND BTRIM('||sc||') = BTRIM(name)
1707 ) AND value = BTRIM('||sce||')
1708 AND owner = ANY ($2)
1711 USING org, org_list;
1713 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1715 CREATE OR REPLACE FUNCTION migration_tools.handle_asset_sc_and_sce (TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
1717 table_schema ALIAS FOR $1;
1718 table_name ALIAS FOR $2;
1719 field_suffix ALIAS FOR $3; -- for distinguishing between desired_sce1, desired_sce2, etc.
1720 org_shortname ALIAS FOR $4;
1728 SELECT 'desired_sc' || field_suffix INTO sc;
1729 SELECT 'desired_sce' || field_suffix INTO sce;
1730 EXECUTE 'SELECT EXISTS (
1732 FROM information_schema.columns
1733 WHERE table_schema = $1
1735 and column_name = $3
1736 )' INTO proceed USING table_schema, table_name, sc;
1738 RAISE EXCEPTION 'Missing column %', sc;
1740 EXECUTE 'SELECT EXISTS (
1742 FROM information_schema.columns
1743 WHERE table_schema = $1
1745 and column_name = $3
1746 )' INTO proceed USING table_schema, table_name, sce;
1748 RAISE EXCEPTION 'Missing column %', sce;
1751 SELECT id INTO org FROM actor.org_unit WHERE shortname = org_shortname;
1753 RAISE EXCEPTION 'Cannot find org by shortname';
1756 SELECT INTO org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( org );
1758 EXECUTE 'ALTER TABLE '
1759 || quote_ident(table_name)
1760 || ' DROP COLUMN IF EXISTS x_sc' || field_suffix;
1761 EXECUTE 'ALTER TABLE '
1762 || quote_ident(table_name)
1763 || ' ADD COLUMN x_sc' || field_suffix || ' INTEGER';
1764 EXECUTE 'ALTER TABLE '
1765 || quote_ident(table_name)
1766 || ' DROP COLUMN IF EXISTS x_sce' || field_suffix;
1767 EXECUTE 'ALTER TABLE '
1768 || quote_ident(table_name)
1769 || ' ADD COLUMN x_sce' || field_suffix || ' INTEGER';
1772 EXECUTE 'UPDATE ' || quote_ident(table_name) || '
1774 x_sc' || field_suffix || ' = id
1776 (SELECT id, name, owner FROM asset_stat_cat
1777 UNION SELECT id, name, owner FROM asset.stat_cat) u
1779 BTRIM(UPPER(u.name)) = BTRIM(UPPER(' || sc || '))
1780 AND u.owner = ANY ($1);'
1783 EXECUTE 'UPDATE ' || quote_ident(table_name) || '
1785 x_sce' || field_suffix || ' = id
1787 (SELECT id, stat_cat, owner, value FROM asset_stat_cat_entry
1788 UNION SELECT id, stat_cat, owner, value FROM asset.stat_cat_entry) u
1790 u.stat_cat = x_sc' || field_suffix || '
1791 AND BTRIM(UPPER(u.value)) = BTRIM(UPPER(' || sce || '))
1792 AND u.owner = ANY ($1);'
1795 EXECUTE 'SELECT migration_tools.assert(
1796 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_sc' || field_suffix || ' <> '''' AND x_sc' || field_suffix || ' IS NULL),
1797 ''Cannot find a desired stat cat'',
1798 ''Found all desired stat cats''
1801 EXECUTE 'SELECT migration_tools.assert(
1802 NOT EXISTS (SELECT 1 FROM ' || quote_ident(table_name) || ' WHERE desired_sce' || field_suffix || ' <> '''' AND x_sce' || field_suffix || ' IS NULL),
1803 ''Cannot find a desired stat cat entry'',
1804 ''Found all desired stat cat entries''
1808 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1810 -- convenience function for handling item barcode collisions in asset_copy_legacy
1812 CREATE OR REPLACE FUNCTION migration_tools.handle_asset_barcode_collisions(migration_schema TEXT) RETURNS VOID AS $function$
1817 internal_collision_count NUMERIC := 0;
1818 incumbent_collision_count NUMERIC := 0;
1820 FOR x_barcode IN SELECT barcode FROM asset_copy_legacy WHERE x_migrate GROUP BY 1 HAVING COUNT(*) > 1
1822 FOR x_id IN SELECT id FROM asset_copy WHERE barcode = x_barcode
1824 UPDATE asset_copy SET barcode = migration_schema || '_internal_collision_' || id || '_' || barcode WHERE id = x_id;
1825 GET DIAGNOSTICS row_count = ROW_COUNT;
1826 internal_collision_count := internal_collision_count + row_count;
1829 RAISE INFO '% internal collisions', internal_collision_count;
1830 FOR x_barcode IN SELECT a.barcode FROM asset.copy a, asset_copy_legacy b WHERE x_migrate AND a.deleted IS FALSE AND a.barcode = b.barcode
1832 FOR x_id IN SELECT id FROM asset_copy_legacy WHERE barcode = x_barcode
1834 UPDATE asset_copy_legacy SET barcode = migration_schema || '_incumbent_collision_' || id || '_' || barcode WHERE id = x_id;
1835 GET DIAGNOSTICS row_count = ROW_COUNT;
1836 incumbent_collision_count := incumbent_collision_count + row_count;
1839 RAISE INFO '% incumbent collisions', incumbent_collision_count;
1841 $function$ LANGUAGE plpgsql;
1843 -- convenience function for handling patron barcode/usrname collisions in actor_usr_legacy
1844 -- this should be ran prior to populating actor_card
1846 CREATE OR REPLACE FUNCTION migration_tools.handle_actor_barcode_collisions(migration_schema TEXT) RETURNS VOID AS $function$
1851 internal_collision_count NUMERIC := 0;
1852 incumbent_barcode_collision_count NUMERIC := 0;
1853 incumbent_usrname_collision_count NUMERIC := 0;
1855 FOR x_barcode IN SELECT usrname FROM actor_usr_legacy WHERE x_migrate GROUP BY 1 HAVING COUNT(*) > 1
1857 FOR x_id IN SELECT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1859 UPDATE actor_usr_legacy SET usrname = migration_schema || '_internal_collision_' || id || '_' || usrname WHERE id = x_id;
1860 GET DIAGNOSTICS row_count = ROW_COUNT;
1861 internal_collision_count := internal_collision_count + row_count;
1864 RAISE INFO '% internal usrname/barcode collisions', internal_collision_count;
1867 SELECT a.barcode FROM actor.card a, actor_usr_legacy b WHERE x_migrate AND a.barcode = b.usrname
1869 FOR x_id IN SELECT DISTINCT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1871 UPDATE actor_usr_legacy SET usrname = migration_schema || '_incumbent_barcode_collision_' || id || '_' || usrname WHERE id = x_id;
1872 GET DIAGNOSTICS row_count = ROW_COUNT;
1873 incumbent_barcode_collision_count := incumbent_barcode_collision_count + row_count;
1876 RAISE INFO '% incumbent barcode collisions', incumbent_barcode_collision_count;
1879 SELECT a.usrname FROM actor.usr a, actor_usr_legacy b WHERE x_migrate AND a.usrname = b.usrname
1881 FOR x_id IN SELECT DISTINCT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1883 UPDATE actor_usr_legacy SET usrname = migration_schema || '_incumbent_usrname_collision_' || id || '_' || usrname WHERE id = x_id;
1884 GET DIAGNOSTICS row_count = ROW_COUNT;
1885 incumbent_usrname_collision_count := incumbent_usrname_collision_count + row_count;
1888 RAISE INFO '% incumbent usrname collisions (post barcode collision munging)', incumbent_usrname_collision_count;
1890 $function$ LANGUAGE plpgsql;
1892 -- alternate version: convenience function for handling item barcode collisions in asset_copy_legacy
1894 CREATE OR REPLACE FUNCTION migration_tools.handle_asset_barcode_collisions2(migration_schema TEXT) RETURNS VOID AS $function$
1899 internal_collision_count NUMERIC := 0;
1900 incumbent_collision_count NUMERIC := 0;
1902 FOR x_barcode IN SELECT barcode FROM asset_copy_legacy WHERE x_migrate GROUP BY 1 HAVING COUNT(*) > 1
1904 FOR x_id IN SELECT id FROM asset_copy WHERE barcode = x_barcode
1906 UPDATE asset_copy SET barcode = migration_schema || '_internal_collision_' || id || '_' || barcode WHERE id = x_id;
1907 GET DIAGNOSTICS row_count = ROW_COUNT;
1908 internal_collision_count := internal_collision_count + row_count;
1911 RAISE INFO '% internal collisions', internal_collision_count;
1912 FOR x_barcode IN SELECT a.barcode FROM asset.copy a, asset_copy_legacy b WHERE x_migrate AND a.deleted IS FALSE AND a.barcode = b.barcode
1914 FOR x_id IN SELECT id FROM asset_copy_legacy WHERE barcode = x_barcode
1916 UPDATE asset_copy_legacy SET barcode = migration_schema || '_' || barcode WHERE id = x_id;
1917 GET DIAGNOSTICS row_count = ROW_COUNT;
1918 incumbent_collision_count := incumbent_collision_count + row_count;
1921 RAISE INFO '% incumbent collisions', incumbent_collision_count;
1923 $function$ LANGUAGE plpgsql;
1925 -- alternate version: convenience function for handling patron barcode/usrname collisions in actor_usr_legacy
1926 -- this should be ran prior to populating actor_card
1928 CREATE OR REPLACE FUNCTION migration_tools.handle_actor_barcode_collisions2(migration_schema TEXT) RETURNS VOID AS $function$
1933 internal_collision_count NUMERIC := 0;
1934 incumbent_barcode_collision_count NUMERIC := 0;
1935 incumbent_usrname_collision_count NUMERIC := 0;
1937 FOR x_barcode IN SELECT usrname FROM actor_usr_legacy WHERE x_migrate GROUP BY 1 HAVING COUNT(*) > 1
1939 FOR x_id IN SELECT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1941 UPDATE actor_usr_legacy SET usrname = migration_schema || '_internal_collision_' || id || '_' || usrname WHERE id = x_id;
1942 GET DIAGNOSTICS row_count = ROW_COUNT;
1943 internal_collision_count := internal_collision_count + row_count;
1946 RAISE INFO '% internal usrname/barcode collisions', internal_collision_count;
1949 SELECT a.barcode FROM actor.card a, actor_usr_legacy b WHERE x_migrate AND a.barcode = b.usrname
1951 FOR x_id IN SELECT DISTINCT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1953 UPDATE actor_usr_legacy SET usrname = migration_schema || '_' || usrname WHERE id = x_id;
1954 GET DIAGNOSTICS row_count = ROW_COUNT;
1955 incumbent_barcode_collision_count := incumbent_barcode_collision_count + row_count;
1958 RAISE INFO '% incumbent barcode collisions', incumbent_barcode_collision_count;
1961 SELECT a.usrname FROM actor.usr a, actor_usr_legacy b WHERE x_migrate AND a.usrname = b.usrname
1963 FOR x_id IN SELECT DISTINCT id FROM actor_usr_legacy WHERE x_migrate AND usrname = x_barcode
1965 UPDATE actor_usr_legacy SET usrname = migration_schema || '_' || usrname WHERE id = x_id;
1966 GET DIAGNOSTICS row_count = ROW_COUNT;
1967 incumbent_usrname_collision_count := incumbent_usrname_collision_count + row_count;
1970 RAISE INFO '% incumbent usrname collisions (post barcode collision munging)', incumbent_usrname_collision_count;
1972 $function$ LANGUAGE plpgsql;
1974 CREATE OR REPLACE FUNCTION migration_tools.is_circ_rule_safe_to_delete( test_matchpoint INTEGER ) RETURNS BOOLEAN AS $func$
1975 -- WARNING: Use at your own risk
1976 -- FIXME: not considering marc_type, marc_form, marc_bib_level, marc_vr_format, usr_age_lower_bound, usr_age_upper_bound, item_age
1978 item_object asset.copy%ROWTYPE;
1979 user_object actor.usr%ROWTYPE;
1980 test_rule_object config.circ_matrix_matchpoint%ROWTYPE;
1981 result_rule_object config.circ_matrix_matchpoint%ROWTYPE;
1982 safe_to_delete BOOLEAN := FALSE;
1983 m action.found_circ_matrix_matchpoint;
1984 n action.found_circ_matrix_matchpoint;
1985 -- ( success BOOL, matchpoint config.circ_matrix_matchpoint, buildrows INT[] )
1986 result_matchpoint INTEGER;
1988 SELECT INTO test_rule_object * FROM config.circ_matrix_matchpoint WHERE id = test_matchpoint;
1989 RAISE INFO 'testing rule: %', test_rule_object;
1991 INSERT INTO actor.usr (
2001 COALESCE(test_rule_object.grp, 2),
2002 'is_circ_rule_safe_to_delete_' || test_matchpoint || '_' || NOW()::text,
2007 COALESCE(test_rule_object.user_home_ou, test_rule_object.org_unit),
2008 COALESCE(test_rule_object.juvenile_flag, FALSE)
2011 SELECT INTO user_object * FROM actor.usr WHERE id = currval('actor.usr_id_seq');
2013 INSERT INTO asset.call_number (
2024 COALESCE(test_rule_object.copy_owning_lib,test_rule_object.org_unit),
2025 'is_circ_rule_safe_to_delete_' || test_matchpoint || '_' || NOW()::text,
2029 INSERT INTO asset.copy (
2041 'is_circ_rule_safe_to_delete_' || test_matchpoint || '_' || NOW()::text,
2042 COALESCE(test_rule_object.copy_circ_lib,test_rule_object.org_unit),
2044 currval('asset.call_number_id_seq'),
2046 COALESCE(test_rule_object.copy_location,1),
2049 COALESCE(test_rule_object.ref_flag,FALSE),
2050 test_rule_object.circ_modifier
2053 SELECT INTO item_object * FROM asset.copy WHERE id = currval('asset.copy_id_seq');
2055 SELECT INTO m * FROM action.find_circ_matrix_matchpoint(
2056 test_rule_object.org_unit,
2059 COALESCE(test_rule_object.is_renewal,FALSE)
2061 RAISE INFO ' action.find_circ_matrix_matchpoint(%,%,%,%) = (%,%,%)',
2062 test_rule_object.org_unit,
2065 COALESCE(test_rule_object.is_renewal,FALSE),
2071 -- disable the rule being tested to see if the outcome changes
2072 UPDATE config.circ_matrix_matchpoint SET active = FALSE WHERE id = (m.matchpoint).id;
2074 SELECT INTO n * FROM action.find_circ_matrix_matchpoint(
2075 test_rule_object.org_unit,
2078 COALESCE(test_rule_object.is_renewal,FALSE)
2080 RAISE INFO 'VS action.find_circ_matrix_matchpoint(%,%,%,%) = (%,%,%)',
2081 test_rule_object.org_unit,
2084 COALESCE(test_rule_object.is_renewal,FALSE),
2090 -- FIXME: We could dig deeper and see if the referenced config.rule_*
2091 -- entries are effectively equivalent, but for now, let's assume no
2092 -- duplicate rules at that level
2094 (m.matchpoint).circulate = (n.matchpoint).circulate
2095 AND (m.matchpoint).duration_rule = (n.matchpoint).duration_rule
2096 AND (m.matchpoint).recurring_fine_rule = (n.matchpoint).recurring_fine_rule
2097 AND (m.matchpoint).max_fine_rule = (n.matchpoint).max_fine_rule
2099 (m.matchpoint).hard_due_date = (n.matchpoint).hard_due_date
2101 (m.matchpoint).hard_due_date IS NULL
2102 AND (n.matchpoint).hard_due_date IS NULL
2106 (m.matchpoint).renewals = (n.matchpoint).renewals
2108 (m.matchpoint).renewals IS NULL
2109 AND (n.matchpoint).renewals IS NULL
2113 (m.matchpoint).grace_period = (n.matchpoint).grace_period
2115 (m.matchpoint).grace_period IS NULL
2116 AND (n.matchpoint).grace_period IS NULL
2120 (m.matchpoint).total_copy_hold_ratio = (n.matchpoint).total_copy_hold_ratio
2122 (m.matchpoint).total_copy_hold_ratio IS NULL
2123 AND (n.matchpoint).total_copy_hold_ratio IS NULL
2127 (m.matchpoint).available_copy_hold_ratio = (n.matchpoint).available_copy_hold_ratio
2129 (m.matchpoint).available_copy_hold_ratio IS NULL
2130 AND (n.matchpoint).available_copy_hold_ratio IS NULL
2134 SELECT limit_set, fallthrough
2135 FROM config.circ_matrix_limit_set_map
2136 WHERE active and matchpoint = (m.matchpoint).id
2138 SELECT limit_set, fallthrough
2139 FROM config.circ_matrix_limit_set_map
2140 WHERE active and matchpoint = (n.matchpoint).id
2144 RAISE INFO 'rule has same outcome';
2145 safe_to_delete := TRUE;
2147 RAISE INFO 'rule has different outcome';
2148 safe_to_delete := FALSE;
2151 RAISE EXCEPTION 'rollback the temporary changes';
2153 EXCEPTION WHEN OTHERS THEN
2155 RAISE INFO 'inside exception block: %, %', SQLSTATE, SQLERRM;
2156 RETURN safe_to_delete;
2159 $func$ LANGUAGE plpgsql;