1 -- Copyright 2009-2012, Equinox Software, Inc.
3 -- This program is free software; you can redistribute it and/or
4 -- modify it under the terms of the GNU General Public License
5 -- as published by the Free Software Foundation; either version 2
6 -- of the License, or (at your option) any later version.
8 -- This program is distributed in the hope that it will be useful,
9 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
10 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 -- GNU General Public License for more details.
13 -- You should have received a copy of the GNU General Public License
14 -- along with this program; if not, write to the Free Software
15 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 --------------------------------------------------------------------------
18 -- An example of how to use:
20 -- DROP SCHEMA foo CASCADE; CREATE SCHEMA foo;
22 -- SELECT migration_tools.init('foo');
23 -- SELECT migration_tools.build('foo');
24 -- SELECT * FROM foo.fields_requiring_mapping;
26 -- create some incoming ILS specific staging tables, like CREATE foo.legacy_items ( l_barcode TEXT, .. ) INHERITS (foo.asset_copy);
27 -- Do some mapping, like UPDATE foo.legacy_items SET barcode = TRIM(BOTH ' ' FROM l_barcode);
28 -- Then, to move into production, do: select migration_tools.insert_base_into_production('foo')
30 CREATE SCHEMA migration_tools;
32 CREATE OR REPLACE FUNCTION migration_tools.production_tables (TEXT) RETURNS TEXT[] AS $$
34 migration_schema ALIAS FOR $1;
38 EXECUTE 'SELECT string_to_array(value,'','') AS tables FROM ' || migration_schema || '.config WHERE key = ''production_tables'';'
43 $$ LANGUAGE PLPGSQL STRICT STABLE;
45 CREATE OR REPLACE FUNCTION migration_tools.country_code (TEXT) RETURNS TEXT AS $$
47 migration_schema ALIAS FOR $1;
51 EXECUTE 'SELECT value FROM ' || migration_schema || '.config WHERE key = ''country_code'';'
56 $$ LANGUAGE PLPGSQL STRICT STABLE;
59 CREATE OR REPLACE FUNCTION migration_tools.log (TEXT,TEXT,INTEGER) RETURNS VOID AS $$
61 migration_schema ALIAS FOR $1;
65 EXECUTE 'INSERT INTO ' || migration_schema || '.sql_log ( sql, row_count ) VALUES ( ' || quote_literal(sql) || ', ' || nrows || ' );';
67 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
69 CREATE OR REPLACE FUNCTION migration_tools.exec (TEXT,TEXT) RETURNS VOID AS $$
71 migration_schema ALIAS FOR $1;
75 EXECUTE 'UPDATE ' || migration_schema || '.sql_current SET sql = ' || quote_literal(sql) || ';';
76 --RAISE INFO '%', sql;
78 GET DIAGNOSTICS nrows = ROW_COUNT;
79 PERFORM migration_tools.log(migration_schema,sql,nrows);
82 RAISE EXCEPTION '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
84 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
86 CREATE OR REPLACE FUNCTION migration_tools.debug_exec (TEXT,TEXT) RETURNS VOID AS $$
88 migration_schema ALIAS FOR $1;
92 EXECUTE 'UPDATE ' || migration_schema || '.sql_current SET sql = ' || quote_literal(sql) || ';';
93 RAISE INFO 'debug_exec sql = %', sql;
95 GET DIAGNOSTICS nrows = ROW_COUNT;
96 PERFORM migration_tools.log(migration_schema,sql,nrows);
99 RAISE EXCEPTION '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
101 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
103 CREATE OR REPLACE FUNCTION migration_tools.init (TEXT) RETURNS VOID AS $$
105 migration_schema ALIAS FOR $1;
108 EXECUTE 'DROP TABLE IF EXISTS ' || migration_schema || '.sql_current;';
109 EXECUTE 'CREATE TABLE ' || migration_schema || '.sql_current ( sql TEXT);';
110 EXECUTE 'INSERT INTO ' || migration_schema || '.sql_current ( sql ) VALUES ( '''' );';
112 SELECT 'CREATE TABLE ' || migration_schema || '.sql_log ( time TIMESTAMP NOT NULL DEFAULT NOW(), row_count INTEGER, sql TEXT );' INTO STRICT sql;
116 RAISE INFO '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
118 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.config;' );
119 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.config ( key TEXT UNIQUE, value TEXT);' );
120 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''production_tables'', ''asset.call_number,asset.call_number_prefix,asset.call_number_suffix,asset.copy_location,asset.copy,asset.stat_cat,asset.stat_cat_entry,asset.stat_cat_entry_copy_map,asset.copy_note,actor.usr,actor.card,actor.usr_address,actor.stat_cat,actor.stat_cat_entry,actor.stat_cat_entry_usr_map,actor.usr_note,actor.usr_standing_penalty,action.circulation,action.hold_request,action.hold_notification,action.hold_request_note,action.hold_transit_copy,action.transit_copy,money.grocery,money.billing,money.cash_payment,money.forgive_payment,acq.provider,acq.provider_address,acq.provider_note,acq.provider_contact,acq.provider_contact_address,acq.fund,acq.fund_allocation,acq.fund_tag,acq.fund_tag_map,acq.funding_source,acq.funding_source_credit,acq.lineitem,acq.purchase_order,acq.po_item,acq.invoice,acq.invoice_item,acq.invoice_entry,acq.lineitem_detail,acq.fund_debit,acq.fund_transfer,acq.po_note,config.circ_matrix_matchpoint,config.circ_matrix_limit_set_map,config.hold_matrix_matchpoint'' );' );
121 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''country_code'', ''USA'' );' );
122 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.fields_requiring_mapping;' );
123 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.fields_requiring_mapping( table_schema TEXT, table_name TEXT, column_name TEXT, data_type TEXT);' );
124 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.base_profile_map;' );
125 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || E'.base_profile_map (
128 transcribed_perm_group TEXT,
136 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''base_profile_map'', ''base_profile_map'' );' );
137 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.base_item_dynamic_field_map;' );
138 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || E'.base_item_dynamic_field_map (
140 evergreen_field TEXT,
141 evergreen_value TEXT,
142 evergreen_datatype TEXT,
150 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_item_dynamic_lf1_idx ON ' || migration_schema || '.base_item_dynamic_field_map (legacy_field1,legacy_value1);' );
151 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_item_dynamic_lf2_idx ON ' || migration_schema || '.base_item_dynamic_field_map (legacy_field2,legacy_value2);' );
152 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_item_dynamic_lf3_idx ON ' || migration_schema || '.base_item_dynamic_field_map (legacy_field3,legacy_value3);' );
153 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''base_item_dynamic_field_map'', ''base_item_dynamic_field_map'' );' );
154 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.base_copy_location_map;' );
155 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || E'.base_copy_location_map (
158 holdable BOOLEAN NOT NULL DEFAULT TRUE,
159 hold_verify BOOLEAN NOT NULL DEFAULT FALSE,
160 opac_visible BOOLEAN NOT NULL DEFAULT TRUE,
161 circulate BOOLEAN NOT NULL DEFAULT TRUE,
162 transcribed_location TEXT,
170 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_base_copy_location_lf1_idx ON ' || migration_schema || '.base_copy_location_map (legacy_field1,legacy_value1);' );
171 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_base_copy_location_lf2_idx ON ' || migration_schema || '.base_copy_location_map (legacy_field2,legacy_value2);' );
172 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_base_copy_location_lf3_idx ON ' || migration_schema || '.base_copy_location_map (legacy_field3,legacy_value3);' );
173 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_base_copy_location_loc_idx ON ' || migration_schema || '.base_copy_location_map (transcribed_location);' );
174 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''base_copy_location_map'', ''base_copy_location_map'' );' );
175 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.base_circ_field_map;' );
176 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || E'.base_circ_field_map (
194 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_circ_dynamic_lf1_idx ON ' || migration_schema || '.base_circ_field_map (item_field1,item_value1);' );
195 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_circ_dynamic_lf2_idx ON ' || migration_schema || '.base_circ_field_map (item_field2,item_value2);' );
196 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_circ_dynamic_lf3_idx ON ' || migration_schema || '.base_circ_field_map (patron_field1,patron_value1);' );
197 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_circ_dynamic_lf4_idx ON ' || migration_schema || '.base_circ_field_map (patron_field2,patron_value2);' );
198 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''base_circ_field_map'', ''base_circ_field_map'' );' );
201 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_init'', now() );' );
203 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_init'';' );
206 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
208 CREATE OR REPLACE FUNCTION migration_tools.build (TEXT) RETURNS VOID AS $$
210 migration_schema ALIAS FOR $1;
211 production_tables TEXT[];
213 --RAISE INFO 'In migration_tools.build(%)', migration_schema;
214 SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
215 PERFORM migration_tools.build_base_staging_tables(migration_schema,production_tables);
216 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_barcode_key ON ' || migration_schema || '.actor_card ( barcode );' );
217 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_usrname_key ON ' || migration_schema || '.actor_usr ( usrname );' );
218 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_copy_barcode_key ON ' || migration_schema || '.asset_copy ( barcode );' );
219 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_copy_id_key ON ' || migration_schema || '.asset_copy ( id );' );
220 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_callnum_record_idx ON ' || migration_schema || '.asset_call_number ( record );' );
221 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_callnum_upper_label_id_lib_idx ON ' || migration_schema || '.asset_call_number ( UPPER(label),id,owning_lib );' );
222 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_callnum_label_once_per_lib ON ' || migration_schema || '.asset_call_number ( record,owning_lib,label,prefix,suffix );' );
224 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
226 CREATE OR REPLACE FUNCTION migration_tools.build_base_staging_tables (TEXT,TEXT[]) RETURNS VOID AS $$
228 migration_schema ALIAS FOR $1;
229 production_tables ALIAS FOR $2;
231 --RAISE INFO 'In migration_tools.build_base_staging_tables(%,%)', migration_schema, production_tables;
232 FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
233 PERFORM migration_tools.build_specific_base_staging_table(migration_schema,production_tables[i]);
236 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
238 CREATE OR REPLACE FUNCTION migration_tools.build_specific_base_staging_table (TEXT,TEXT) RETURNS VOID AS $$
240 migration_schema ALIAS FOR $1;
241 production_table ALIAS FOR $2;
242 base_staging_table TEXT;
245 base_staging_table = REPLACE( production_table, '.', '_' );
246 --RAISE INFO 'In migration_tools.build_specific_base_staging_table(%,%) -> %', migration_schema, production_table, base_staging_table;
247 PERFORM migration_tools.exec( $1, 'CREATE UNLOGGED TABLE ' || migration_schema || '.' || base_staging_table || ' ( LIKE ' || production_table || ' INCLUDING DEFAULTS EXCLUDING CONSTRAINTS );' );
248 PERFORM migration_tools.exec( $1, '
249 INSERT INTO ' || migration_schema || '.fields_requiring_mapping
250 SELECT table_schema, table_name, column_name, data_type
251 FROM information_schema.columns
252 WHERE table_schema = ''' || migration_schema || ''' AND table_name = ''' || base_staging_table || ''' AND is_nullable = ''NO'' AND column_default IS NULL;
255 SELECT table_schema, table_name, column_name, data_type
256 FROM information_schema.columns
257 WHERE table_schema = migration_schema AND table_name = base_staging_table AND is_nullable = 'NO' AND column_default IS NULL
259 PERFORM migration_tools.exec( $1, 'ALTER TABLE ' || columns.table_schema || '.' || columns.table_name || ' ALTER COLUMN ' || columns.column_name || ' DROP NOT NULL;' );
262 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
264 CREATE OR REPLACE FUNCTION migration_tools.insert_base_into_production (TEXT) RETURNS VOID AS $$
266 migration_schema ALIAS FOR $1;
267 production_tables TEXT[];
269 --RAISE INFO 'In migration_tools.insert_into_production(%)', migration_schema;
270 SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
271 FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
272 PERFORM migration_tools.insert_into_production(migration_schema,production_tables[i]);
275 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
277 CREATE OR REPLACE FUNCTION migration_tools.insert_into_production (TEXT,TEXT) RETURNS VOID AS $$
279 migration_schema ALIAS FOR $1;
280 production_table ALIAS FOR $2;
281 base_staging_table TEXT;
284 base_staging_table = REPLACE( production_table, '.', '_' );
285 --RAISE INFO 'In migration_tools.insert_into_production(%,%) -> %', migration_schema, production_table, base_staging_table;
286 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || production_table || ' SELECT * FROM ' || migration_schema || '.' || base_staging_table || ';' );
288 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
290 CREATE OR REPLACE FUNCTION migration_tools.name_parse_out_last_comma_prefix_first_middle_suffix (TEXT) RETURNS TEXT[] AS $$
292 full_name TEXT := $1;
294 family_name TEXT := '';
295 first_given_name TEXT := '';
296 second_given_name TEXT := '';
301 -- Use values, not structure, for prefix/suffix, unless we come up with a better idea
302 IF temp ilike '%MR.%' THEN
304 temp := REGEXP_REPLACE( temp, E'MR\.\\s*', '', 'i' );
306 IF temp ilike '%MRS.%' THEN
308 temp := REGEXP_REPLACE( temp, E'MRS\.\\s*', '', 'i' );
310 IF temp ilike '%MS.%' THEN
312 temp := REGEXP_REPLACE( temp, E'MS\.\\s*', '', 'i' );
314 IF temp ilike '%DR.%' THEN
316 temp := REGEXP_REPLACE( temp, E'DR\.\\s*', '', 'i' );
318 IF temp ilike '%JR%' THEN
320 temp := REGEXP_REPLACE( temp, E'JR\.?\\s*', '', 'i' );
322 IF temp ilike '%JR,%' THEN
324 temp := REGEXP_REPLACE( temp, E'JR,\\s*', ',', 'i' );
326 IF temp ilike '%SR%' THEN
328 temp := REGEXP_REPLACE( temp, E'SR\.?\\s*', '', 'i' );
330 IF temp ilike '%SR,%' THEN
332 temp := REGEXP_REPLACE( temp, E'SR,\\s*', ',', 'i' );
334 IF temp ~ E'\\sII$' THEN
336 temp := REGEXP_REPLACE( temp, E'II$', '', 'i' );
338 IF temp ~ E'\\sIII$' THEN
340 temp := REGEXP_REPLACE( temp, E'III$', '', 'i' );
342 IF temp ~ E'\\sIV$' THEN
344 temp := REGEXP_REPLACE( temp, E'IV$', '', 'i' );
347 family_name := BTRIM( REGEXP_REPLACE(temp,E'^([^,]*)\\s*,.*$',E'\\1') );
348 first_given_name := BTRIM( CASE WHEN temp ~ ',' THEN REGEXP_REPLACE(temp,E'^[^,]*\\s*,\\s*([^,\\s]*)\\s*.*$',E'\\1') ELSE 'N/A' END );
349 second_given_name := BTRIM( CASE WHEN temp ~ ',' THEN REGEXP_REPLACE(temp,E'^[^,]*\\s*,\\s*[^,\\s]*\\s*(.*)$',E'\\1') ELSE '' END );
351 RETURN ARRAY[ family_name, prefix, first_given_name, second_given_name, suffix ];
353 $$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
355 CREATE OR REPLACE FUNCTION migration_tools.name_parse_out_last_first_middle_and_random_affix (TEXT) RETURNS TEXT[] AS $$
357 full_name TEXT := $1;
359 family_name TEXT := '';
360 first_given_name TEXT := '';
361 second_given_name TEXT := '';
366 -- Use values, not structure, for prefix/suffix, unless we come up with a better idea
367 IF temp ilike '%MR.%' THEN
369 temp := REGEXP_REPLACE( temp, E'MR\.\\s*', '', 'i' );
371 IF temp ilike '%MRS.%' THEN
373 temp := REGEXP_REPLACE( temp, E'MRS\.\\s*', '', 'i' );
375 IF temp ilike '%MS.%' THEN
377 temp := REGEXP_REPLACE( temp, E'MS\.\\s*', '', 'i' );
379 IF temp ilike '%DR.%' THEN
381 temp := REGEXP_REPLACE( temp, E'DR\.\\s*', '', 'i' );
383 IF temp ilike '%JR.%' THEN
385 temp := REGEXP_REPLACE( temp, E'JR\.\\s*', '', 'i' );
387 IF temp ilike '%JR,%' THEN
389 temp := REGEXP_REPLACE( temp, E'JR,\\s*', ',', 'i' );
391 IF temp ilike '%SR.%' THEN
393 temp := REGEXP_REPLACE( temp, E'SR\.\\s*', '', 'i' );
395 IF temp ilike '%SR,%' THEN
397 temp := REGEXP_REPLACE( temp, E'SR,\\s*', ',', 'i' );
399 IF temp like '%III%' THEN
401 temp := REGEXP_REPLACE( temp, E'III', '' );
403 IF temp like '%II%' THEN
405 temp := REGEXP_REPLACE( temp, E'II', '' );
407 IF temp like '%IV%' THEN
409 temp := REGEXP_REPLACE( temp, E'IV', '' );
412 temp := REGEXP_REPLACE( temp, '\(\)', '');
413 family_name := BTRIM( REGEXP_REPLACE(temp,E'^(\\S+).*$',E'\\1') );
414 family_name := REGEXP_REPLACE( family_name, ',', '' );
415 first_given_name := CASE WHEN temp ~ E'^\\S+$' THEN 'N/A' ELSE BTRIM( REGEXP_REPLACE(temp,E'^\\S+\\s+(\\S+).*$',E'\\1') ) END;
416 first_given_name := REGEXP_REPLACE( first_given_name, ',', '' );
417 second_given_name := CASE WHEN temp ~ E'^\\S+$' THEN '' ELSE BTRIM( REGEXP_REPLACE(temp,E'^\\S+\\s+\\S+\\s*(.*)$',E'\\1') ) END;
418 second_given_name := REGEXP_REPLACE( second_given_name, ',', '' );
420 RETURN ARRAY[ family_name, prefix, first_given_name, second_given_name, suffix ];
422 $$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
425 CREATE OR REPLACE FUNCTION migration_tools.address_parse_out_citystatezip (TEXT) RETURNS TEXT[] AS $$
427 city_state_zip TEXT := $1;
432 zip := CASE WHEN city_state_zip ~ E'\\d\\d\\d\\d\\d' THEN REGEXP_REPLACE( city_state_zip, E'^.*(\\d\\d\\d\\d\\d-?\\d*).*$', E'\\1' ) ELSE '' END;
433 city_state_zip := REGEXP_REPLACE( city_state_zip, E'^(.*)\\d\\d\\d\\d\\d-?\\d*(.*)$', E'\\1\\2');
434 IF city_state_zip ~ ',' THEN
435 state := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\2');
436 city := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\1');
438 IF city_state_zip ~ E'\\s+[A-Z][A-Z]\\s*$' THEN
439 state := REGEXP_REPLACE( city_state_zip, E'^.*,?\\s+([A-Z][A-Z])\\s*$', E'\\1' );
440 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s+[A-Z][A-Z](\\s*)$', E'\\1\\2' );
442 IF city_state_zip ~ E'^\\S+$' THEN
443 city := city_state_zip;
446 state := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\2');
447 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\1');
451 RETURN ARRAY[ TRIM(BOTH ' ' FROM city), TRIM(BOTH ' ' FROM state), TRIM(BOTH ' ' FROM zip) ];
453 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
455 -- try to parse data like this: 100 MAIN ST$COVINGTON, GA 30016
456 CREATE OR REPLACE FUNCTION migration_tools.parse_out_address (TEXT) RETURNS TEXT[] AS $$
458 fullstring TEXT := $1;
468 WHEN fullstring ~ E'\\d\\d\\d\\d\\d'
469 THEN REGEXP_REPLACE( fullstring, E'^.*(\\d\\d\\d\\d\\d-?\\d*).*$', E'\\1' )
472 fullstring := REGEXP_REPLACE( fullstring, E'^(.*)\\d\\d\\d\\d\\d-?\\d*(.*)$', E'\\1\\2');
474 IF fullstring ~ ',' THEN
475 state := REGEXP_REPLACE( fullstring, E'^(.*),(.*)$', E'\\2');
476 scratch1 := REGEXP_REPLACE( fullstring, E'^(.*),(.*)$', E'\\1');
478 IF fullstring ~ E'\\s+[A-Z][A-Z]\\s*$' THEN
479 state := REGEXP_REPLACE( fullstring, E'^.*,?\\s+([A-Z][A-Z])\\s*$', E'\\1' );
480 scratch1 := REGEXP_REPLACE( fullstring, E'^(.*?),?\\s+[A-Z][A-Z](\\s*)$', E'\\1\\2' );
482 IF fullstring ~ E'^\\S+$' THEN
483 scratch1 := fullstring;
486 state := REGEXP_REPLACE( fullstring, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\2');
487 scratch1 := REGEXP_REPLACE( fullstring, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\1');
492 IF scratch1 ~ '[\$]' THEN
493 scratch2 := REGEXP_REPLACE( scratch1, E'^(.+)[\$](.+?)$', E'\\1');
494 city := REGEXP_REPLACE( scratch1, E'^(.+)[\$](.+?)$', E'\\2');
496 IF scratch1 ~ '\s' THEN
497 scratch2 := REGEXP_REPLACE( scratch1, E'^(.+)\\s+(.+?)$', E'\\1');
498 city := REGEXP_REPLACE( scratch1, E'^(.+)\\s+(.+?)$', E'\\2');
505 IF scratch2 ~ '^\d' THEN
506 address1 := scratch2;
509 address1 := REGEXP_REPLACE( scratch2, E'^(.+?)(\\d.+)$', E'\\1');
510 address2 := REGEXP_REPLACE( scratch2, E'^(.+?)(\\d.+)$', E'\\2');
514 TRIM(BOTH ' ' FROM address1)
515 ,TRIM(BOTH ' ' FROM address2)
516 ,TRIM(BOTH ' ' FROM city)
517 ,TRIM(BOTH ' ' FROM state)
518 ,TRIM(BOTH ' ' FROM zip)
521 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
523 CREATE OR REPLACE FUNCTION migration_tools.rebarcode (o TEXT, t BIGINT) RETURNS TEXT AS $$
527 IF o ~ E'^\\d+$' AND o !~ E'^0' AND length(o) < 19 THEN -- for reference, the max value for a bigint is 9223372036854775807. May also want to consider the case where folks want to add prefixes to non-numeric barcodes
528 IF o::BIGINT < t THEN
535 $$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
537 CREATE OR REPLACE FUNCTION migration_tools.base_profile_map (TEXT) RETURNS TEXT AS $$
539 migration_schema ALIAS FOR $1;
543 EXECUTE 'SELECT ''' || migration_schema || '.'' || value FROM ' || migration_schema || '.config WHERE key = ''base_profile_map'';'
548 $$ LANGUAGE PLPGSQL STRICT STABLE;
550 CREATE OR REPLACE FUNCTION migration_tools.base_item_dynamic_field_map (TEXT) RETURNS TEXT AS $$
552 migration_schema ALIAS FOR $1;
556 EXECUTE 'SELECT ''' || migration_schema || '.'' || value FROM ' || migration_schema || '.config WHERE key = ''base_item_dynamic_field_map'';'
561 $$ LANGUAGE PLPGSQL STRICT STABLE;
563 CREATE OR REPLACE FUNCTION migration_tools.base_copy_location_map (TEXT) RETURNS TEXT AS $$
565 migration_schema ALIAS FOR $1;
569 EXECUTE 'SELECT ''' || migration_schema || '.'' || value FROM ' || migration_schema || '.config WHERE key = ''base_copy_location_map'';'
574 $$ LANGUAGE PLPGSQL STRICT STABLE;
576 CREATE OR REPLACE FUNCTION migration_tools.base_circ_field_map (TEXT) RETURNS TEXT AS $$
578 migration_schema ALIAS FOR $1;
582 EXECUTE 'SELECT ''' || migration_schema || '.'' || value FROM ' || migration_schema || '.config WHERE key = ''base_circ_field_map'';'
587 $$ LANGUAGE PLPGSQL STRICT STABLE;
589 CREATE OR REPLACE FUNCTION migration_tools.map_base_patron_profile (TEXT,TEXT,INTEGER) RETURNS VOID AS $$
591 migration_schema ALIAS FOR $1;
593 patron_table ALIAS FOR $2;
594 default_patron_profile ALIAS FOR $3;
597 sql_where1 TEXT := '';
598 sql_where2 TEXT := '';
599 sql_where3 TEXT := '';
602 SELECT migration_tools.base_profile_map(migration_schema) INTO STRICT profile_map;
604 EXECUTE 'SELECT * FROM ' || profile_map || E' ORDER BY id;'
606 sql_update := 'UPDATE ' || patron_table || ' AS u SET profile = perm_grp_id FROM ' || profile_map || ' AS m WHERE ';
607 sql_where1 := NULLIF(output.legacy_field1,'') || ' = ' || quote_literal( output.legacy_value1 ) || ' AND legacy_field1 = ' || quote_literal(output.legacy_field1) || ' AND legacy_value1 = ' || quote_literal(output.legacy_value1);
608 sql_where2 := NULLIF(output.legacy_field2,'') || ' = ' || quote_literal( output.legacy_value2 ) || ' AND legacy_field2 = ' || quote_literal(output.legacy_field2) || ' AND legacy_value2 = ' || quote_literal(output.legacy_value2);
609 sql_where3 := NULLIF(output.legacy_field3,'') || ' = ' || quote_literal( output.legacy_value3 ) || ' AND legacy_field3 = ' || quote_literal(output.legacy_field3) || ' AND legacy_value3 = ' || quote_literal(output.legacy_value3);
610 sql := sql_update || COALESCE(sql_where1,'') || CASE WHEN sql_where1 <> '' AND sql_where2<> '' THEN ' AND ' ELSE '' END || COALESCE(sql_where2,'') || CASE WHEN sql_where2 <> '' AND sql_where3 <> '' THEN ' AND ' ELSE '' END || COALESCE(sql_where3,'') || ';';
611 --RAISE INFO 'sql = %', sql;
612 PERFORM migration_tools.exec( $1, sql );
614 PERFORM migration_tools.exec( $1, 'UPDATE ' || patron_table || ' AS u SET profile = ' || quote_literal(default_patron_profile) || ' WHERE profile IS NULL;' );
616 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_base_patron_mapping_profile'', now() );' );
618 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_base_patron_mapping_profile'';' );
621 $$ LANGUAGE PLPGSQL STRICT STABLE;
623 CREATE OR REPLACE FUNCTION migration_tools.map_base_item_table_dynamic (TEXT,TEXT) RETURNS VOID AS $$
625 migration_schema ALIAS FOR $1;
627 item_table ALIAS FOR $2;
630 sql_where1 TEXT := '';
631 sql_where2 TEXT := '';
632 sql_where3 TEXT := '';
635 SELECT migration_tools.base_item_dynamic_field_map(migration_schema) INTO STRICT field_map;
637 EXECUTE 'SELECT * FROM ' || field_map || E' ORDER BY id;'
639 sql_update := 'UPDATE ' || item_table || ' AS i SET ' || output.evergreen_field || E' = ' || quote_literal(output.evergreen_value) || '::' || output.evergreen_datatype || E' FROM ' || field_map || ' AS m WHERE ';
640 sql_where1 := NULLIF(output.legacy_field1,'') || ' = ' || quote_literal( output.legacy_value1 ) || ' AND legacy_field1 = ' || quote_literal(output.legacy_field1) || ' AND legacy_value1 = ' || quote_literal(output.legacy_value1);
641 sql_where2 := NULLIF(output.legacy_field2,'') || ' = ' || quote_literal( output.legacy_value2 ) || ' AND legacy_field2 = ' || quote_literal(output.legacy_field2) || ' AND legacy_value2 = ' || quote_literal(output.legacy_value2);
642 sql_where3 := NULLIF(output.legacy_field3,'') || ' = ' || quote_literal( output.legacy_value3 ) || ' AND legacy_field3 = ' || quote_literal(output.legacy_field3) || ' AND legacy_value3 = ' || quote_literal(output.legacy_value3);
643 sql := sql_update || COALESCE(sql_where1,'') || CASE WHEN sql_where1 <> '' AND sql_where2<> '' THEN ' AND ' ELSE '' END || COALESCE(sql_where2,'') || CASE WHEN sql_where2 <> '' AND sql_where3 <> '' THEN ' AND ' ELSE '' END || COALESCE(sql_where3,'') || ';';
644 --RAISE INFO 'sql = %', sql;
645 PERFORM migration_tools.exec( $1, sql );
648 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_base_item_mapping_dynamic'', now() );' );
650 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_base_item_mapping_dynamic'';' );
653 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
655 CREATE OR REPLACE FUNCTION migration_tools.map_base_item_table_locations (TEXT,TEXT) RETURNS VOID AS $$
657 migration_schema ALIAS FOR $1;
658 base_copy_location_map TEXT;
659 item_table ALIAS FOR $2;
662 sql_where1 TEXT := '';
663 sql_where2 TEXT := '';
664 sql_where3 TEXT := '';
667 SELECT migration_tools.base_copy_location_map(migration_schema) INTO STRICT base_copy_location_map;
669 EXECUTE 'SELECT * FROM ' || base_copy_location_map || E' ORDER BY id;'
671 sql_update := 'UPDATE ' || item_table || ' AS i SET location = m.location FROM ' || base_copy_location_map || ' AS m WHERE ';
672 sql_where1 := NULLIF(output.legacy_field1,'') || ' = ' || quote_literal( output.legacy_value1 ) || ' AND legacy_field1 = ' || quote_literal(output.legacy_field1) || ' AND legacy_value1 = ' || quote_literal(output.legacy_value1);
673 sql_where2 := NULLIF(output.legacy_field2,'') || ' = ' || quote_literal( output.legacy_value2 ) || ' AND legacy_field2 = ' || quote_literal(output.legacy_field2) || ' AND legacy_value2 = ' || quote_literal(output.legacy_value2);
674 sql_where3 := NULLIF(output.legacy_field3,'') || ' = ' || quote_literal( output.legacy_value3 ) || ' AND legacy_field3 = ' || quote_literal(output.legacy_field3) || ' AND legacy_value3 = ' || quote_literal(output.legacy_value3);
675 sql := sql_update || COALESCE(sql_where1,'') || CASE WHEN sql_where1 <> '' AND sql_where2<> '' THEN ' AND ' ELSE '' END || COALESCE(sql_where2,'') || CASE WHEN sql_where2 <> '' AND sql_where3 <> '' THEN ' AND ' ELSE '' END || COALESCE(sql_where3,'') || ';';
676 --RAISE INFO 'sql = %', sql;
677 PERFORM migration_tools.exec( $1, sql );
680 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_base_item_mapping_locations'', now() );' );
682 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_base_item_mapping_locations'';' );
685 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
687 -- circulate loan period max renewals max out fine amount fine interval max fine item field 1 item value 1 item field 2 item value 2 patron field 1 patron value 1 patron field 2 patron value 2
688 CREATE OR REPLACE FUNCTION migration_tools.map_base_circ_table_dynamic (TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
690 migration_schema ALIAS FOR $1;
692 circ_table ALIAS FOR $2;
693 item_table ALIAS FOR $3;
694 patron_table ALIAS FOR $4;
697 sql_where1 TEXT := '';
698 sql_where2 TEXT := '';
699 sql_where3 TEXT := '';
700 sql_where4 TEXT := '';
703 SELECT migration_tools.base_circ_field_map(migration_schema) INTO STRICT field_map;
705 EXECUTE 'SELECT * FROM ' || field_map || E' ORDER BY id;'
707 sql_update := 'UPDATE ' || circ_table || ' AS c SET duration = ' || quote_literal(output.loan_period) || '::INTERVAL, renewal_remaining = ' || quote_literal(output.max_renewals) || '::INTEGER, recuring_fine = ' || quote_literal(output.fine_amount) || '::NUMERIC(6,2), fine_interval = ' || quote_literal(output.fine_interval) || '::INTERVAL, max_fine = ' || quote_literal(output.max_fine) || '::NUMERIC(6,2) FROM ' || field_map || ' AS m, ' || item_table || ' AS i, ' || patron_table || ' AS u WHERE c.usr = u.id AND c.target_copy = i.id AND ';
708 sql_where1 := NULLIF(output.item_field1,'') || ' = ' || quote_literal( output.item_value1 ) || ' AND item_field1 = ' || quote_literal(output.item_field1) || ' AND item_value1 = ' || quote_literal(output.item_value1);
709 sql_where2 := NULLIF(output.item_field2,'') || ' = ' || quote_literal( output.item_value2 ) || ' AND item_field2 = ' || quote_literal(output.item_field2) || ' AND item_value2 = ' || quote_literal(output.item_value2);
710 sql_where3 := NULLIF(output.patron_field1,'') || ' = ' || quote_literal( output.patron_value1 ) || ' AND patron_field1 = ' || quote_literal(output.patron_field1) || ' AND patron_value1 = ' || quote_literal(output.patron_value1);
711 sql_where4 := NULLIF(output.patron_field2,'') || ' = ' || quote_literal( output.patron_value2 ) || ' AND patron_field2 = ' || quote_literal(output.patron_field2) || ' AND patron_value2 = ' || quote_literal(output.patron_value2);
712 sql := sql_update || COALESCE(sql_where1,'') || CASE WHEN sql_where1 <> '' AND sql_where2<> '' THEN ' AND ' ELSE '' END || COALESCE(sql_where2,'') || CASE WHEN sql_where2 <> '' AND sql_where3 <> '' THEN ' AND ' ELSE '' END || COALESCE(sql_where3,'') || CASE WHEN sql_where3 <> '' AND sql_where4 <> '' THEN ' AND ' ELSE '' END || COALESCE(sql_where4,'') || ';';
713 --RAISE INFO 'sql = %', sql;
714 PERFORM migration_tools.exec( $1, sql );
717 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_base_circ_field_mapping'', now() );' );
719 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_base_circ_field_mapping'';' );
722 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
725 -- $barcode source barcode
726 -- $prefix prefix to add to barcode, NULL = add no prefix
727 -- $maxlen maximum length of barcode; default to 14 if left NULL
728 -- $pad padding string to apply to left of source barcode before adding
729 -- prefix and suffix; set to NULL or '' if no padding is desired
730 -- $suffix suffix to add to barcode, NULL = add no suffix
732 -- Returns a new string consisting of prefix concatenated with padded barcode and suffix.
733 -- If new barcode would be longer than $maxlen, the original barcode is returned instead.
735 CREATE OR REPLACE FUNCTION migration_tools.expand_barcode (TEXT, TEXT, INTEGER, TEXT, TEXT) RETURNS TEXT AS $$
736 my ($barcode, $prefix, $maxlen, $pad, $suffix) = @_;
739 return unless defined $barcode;
741 $prefix = '' unless defined $prefix;
743 $pad = '0' unless defined $pad;
744 $suffix = '' unless defined $suffix;
746 # bail out if adding prefix and suffix would bring new barcode over max length
747 return $barcode if (length($prefix) + length($barcode) + length($suffix)) > $maxlen;
749 my $new_barcode = $barcode;
751 my $pad_length = $maxlen - length($prefix) - length($suffix);
752 if (length($barcode) < $pad_length) {
753 # assuming we always want padding on the left
754 # also assuming that it is possible to have the pad string be longer than 1 character
755 $new_barcode = substr($pad x ($pad_length - length($barcode)), 0, $pad_length - length($barcode)) . $new_barcode;
759 # bail out if adding prefix and suffix would bring new barcode over max length
760 return $barcode if (length($prefix) + length($new_barcode) + length($suffix)) > $maxlen;
762 return "$prefix$new_barcode$suffix";
763 $$ LANGUAGE PLPERLU STABLE;
765 -- remove previous version of this function
766 DROP FUNCTION IF EXISTS migration_tools.attempt_cast(TEXT, TEXT, TEXT);
768 CREATE OR REPLACE FUNCTION migration_tools.attempt_cast (TEXT, TEXT) RETURNS TEXT AS $$
770 attempt_value ALIAS FOR $1;
771 datatype ALIAS FOR $2;
773 EXECUTE 'SELECT ' || quote_literal(attempt_value) || '::' || datatype || ' AS a;';
774 RETURN attempt_value;
776 WHEN OTHERS THEN RETURN NULL;
778 $$ LANGUAGE PLPGSQL STRICT STABLE;
780 CREATE OR REPLACE FUNCTION migration_tools.attempt_date (TEXT,TEXT) RETURNS DATE AS $$
782 attempt_value ALIAS FOR $1;
783 fail_value ALIAS FOR $2;
787 EXECUTE 'SELECT ' || quote_literal(REGEXP_REPLACE(attempt_value,'^(\d\d)(\d\d)(\d\d)$','\1-\2-\3')) || '::date AS a;'
794 EXECUTE 'SELECT ' || quote_literal(fail_value) || '::date AS a;'
799 $$ LANGUAGE PLPGSQL STRICT STABLE;
801 CREATE OR REPLACE FUNCTION migration_tools.attempt_timestamptz (TEXT,TEXT) RETURNS TIMESTAMPTZ AS $$
803 attempt_value ALIAS FOR $1;
804 fail_value ALIAS FOR $2;
808 EXECUTE 'SELECT ' || quote_literal(attempt_value) || '::TIMESTAMPTZ AS a;'
815 EXECUTE 'SELECT ' || quote_literal(fail_value) || '::TIMESTAMPTZ AS a;'
820 $$ LANGUAGE PLPGSQL STRICT STABLE;
822 CREATE OR REPLACE FUNCTION migration_tools.attempt_hz_date (TEXT,TEXT) RETURNS DATE AS $$
824 attempt_value ALIAS FOR $1;
825 fail_value ALIAS FOR $2;
829 EXECUTE E'SELECT (\'1970-01-01\'::date + \'' || attempt_value || E' days\'::interval)::date AS a;'
836 EXECUTE 'SELECT ' || quote_literal(fail_value) || '::date AS a;'
841 $$ LANGUAGE PLPGSQL STRICT STABLE;
843 CREATE OR REPLACE FUNCTION migration_tools.attempt_money (TEXT,TEXT) RETURNS NUMERIC(8,2) AS $$
845 attempt_value ALIAS FOR $1;
846 fail_value ALIAS FOR $2;
850 EXECUTE 'SELECT ' || quote_literal(REPLACE(REPLACE(attempt_value,'$',''),',','')) || '::NUMERIC(8,2) AS a;'
857 EXECUTE 'SELECT ' || quote_literal(fail_value) || '::NUMERIC(8,2) AS a;'
862 $$ LANGUAGE PLPGSQL STRICT STABLE;
864 CREATE OR REPLACE FUNCTION migration_tools.attempt_money6 (TEXT,TEXT) RETURNS NUMERIC(6,2) AS $$
866 attempt_value ALIAS FOR $1;
867 fail_value ALIAS FOR $2;
871 EXECUTE 'SELECT ' || quote_literal(REPLACE(REPLACE(attempt_value,'$',''),',','')) || '::NUMERIC(6,2) AS a;'
878 EXECUTE 'SELECT ' || quote_literal(fail_value) || '::NUMERIC(6,2) AS a;'
883 $$ LANGUAGE PLPGSQL STRICT STABLE;
885 CREATE OR REPLACE FUNCTION migration_tools.attempt_money_from_pennies (TEXT,TEXT) RETURNS NUMERIC(8,2) AS $$
887 attempt_value ALIAS FOR $1;
888 fail_value ALIAS FOR $2;
891 IF length(regexp_replace(attempt_value,'^0+','')) > 10 THEN
892 RAISE EXCEPTION 'too many digits';
895 EXECUTE 'SELECT ' || quote_literal((left(lpad(regexp_replace(attempt_value,'^0+',''),10,'0'),-2) || '.' || right(lpad(regexp_replace(attempt_value,'^0+',''),10,'0'),2))::numeric(8,2)) || '::NUMERIC(8,2) AS a;'
902 EXECUTE 'SELECT ' || quote_literal(fail_value) || '::NUMERIC(8,2) AS a;'
907 $$ LANGUAGE PLPGSQL STRICT STABLE;
909 CREATE OR REPLACE FUNCTION migration_tools.attempt_money_from_pennies6 (TEXT,TEXT) RETURNS NUMERIC(6,2) AS $$
911 attempt_value ALIAS FOR $1;
912 fail_value ALIAS FOR $2;
915 IF length(regexp_replace(attempt_value,'^0+','')) > 8 THEN
916 RAISE EXCEPTION 'too many digits';
919 EXECUTE 'SELECT ' || quote_literal((left(lpad(regexp_replace(attempt_value,'^0+',''),8,'0'),-2) || '.' || right(lpad(regexp_replace(attempt_value,'^0+',''),8,'0'),2))::numeric(6,2)) || '::NUMERIC(6,2) AS a;'
926 EXECUTE 'SELECT ' || quote_literal(fail_value) || '::NUMERIC(6,2) AS a;'
931 $$ LANGUAGE PLPGSQL STRICT STABLE;
933 -- add_codabar_checkdigit
934 -- $barcode source barcode
936 -- If the source string is 13 or 14 characters long and contains only digits, adds or replaces the 14
937 -- character with a checkdigit computed according to the usual algorithm for library barcodes
938 -- using the Codabar symbology - see <http://www.makebarcode.com/specs/codabar.html>. If the
939 -- input string does not meet those requirements, it is returned unchanged.
941 CREATE OR REPLACE FUNCTION migration_tools.add_codabar_checkdigit (TEXT) RETURNS TEXT AS $$
944 return $barcode if $barcode !~ /^\d{13,14}$/;
945 $barcode = substr($barcode, 0, 13); # ignore 14th digit
946 my @digits = split //, $barcode;
948 $total += $digits[$_] foreach (1, 3, 5, 7, 9, 11);
949 $total += (2 * $digits[$_] >= 10) ? (2 * $digits[$_] - 9) : (2 * $digits[$_]) foreach (0, 2, 4, 6, 8, 10, 12);
950 my $remainder = $total % 10;
951 my $checkdigit = ($remainder == 0) ? $remainder : 10 - $remainder;
952 return $barcode . $checkdigit;
953 $$ LANGUAGE PLPERLU STRICT STABLE;
955 -- add_code39mod43_checkdigit
956 -- $barcode source barcode
958 -- If the source string is 13 or 14 characters long and contains only valid
959 -- Code 39 mod 43 characters, adds or replaces the 14th
960 -- character with a checkdigit computed according to the usual algorithm for library barcodes
961 -- using the Code 39 mod 43 symbology - see <http://en.wikipedia.org/wiki/Code_39#Code_39_mod_43>. If the
962 -- input string does not meet those requirements, it is returned unchanged.
964 CREATE OR REPLACE FUNCTION migration_tools.add_code39mod43_checkdigit (TEXT) RETURNS TEXT AS $$
967 return $barcode if $barcode !~ /^[0-9A-Z. $\/+%-]{13,14}$/;
968 $barcode = substr($barcode, 0, 13); # ignore 14th character
970 my @valid_chars = split //, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%';
971 my %nums = map { $valid_chars[$_] => $_ } (0..42);
974 $total += $nums{$_} foreach split(//, $barcode);
975 my $remainder = $total % 43;
976 my $checkdigit = $valid_chars[$remainder];
977 return $barcode . $checkdigit;
978 $$ LANGUAGE PLPERLU STRICT STABLE;
980 CREATE OR REPLACE FUNCTION migration_tools.attempt_phone (TEXT,TEXT) RETURNS TEXT AS $$
986 n_digits INTEGER := 0;
989 temp := REGEXP_REPLACE(temp, '^1*[^0-9]*(?=[0-9])', '');
990 temp := REGEXP_REPLACE(temp, '[^0-9]*([0-9]{3})[^0-9]*([0-9]{3})[^0-9]*([0-9]{4})', E'\\1-\\2-\\3');
991 n_digits := LENGTH(REGEXP_REPLACE(REGEXP_REPLACE(temp, '(.*)?[a-zA-Z].*', E'\\1') , '[^0-9]', '', 'g'));
992 IF n_digits = 7 AND areacode <> '' THEN
993 temp := REGEXP_REPLACE(temp, '[^0-9]*([0-9]{3})[^0-9]*([0-9]{4})', E'\\1-\\2');
994 output := (areacode || '-' || temp);
1001 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
1003 CREATE OR REPLACE FUNCTION migration_tools.set_leader (TEXT, INT, TEXT) RETURNS TEXT AS $$
1004 my ($marcxml, $pos, $value) = @_;
1007 use MARC::File::XML;
1011 my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8');
1012 my $leader = $marc->leader();
1013 substr($leader, $pos, 1) = $value;
1014 $marc->leader($leader);
1015 $xml = $marc->as_xml_record;
1016 $xml =~ s/^<\?.+?\?>$//mo;
1018 $xml =~ s/>\s+</></sgo;
1021 $$ LANGUAGE PLPERLU STABLE;
1023 CREATE OR REPLACE FUNCTION migration_tools.set_008 (TEXT, INT, TEXT) RETURNS TEXT AS $$
1024 my ($marcxml, $pos, $value) = @_;
1027 use MARC::File::XML;
1031 my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8');
1032 my $f008 = $marc->field('008');
1035 my $field = $f008->data();
1036 substr($field, $pos, 1) = $value;
1037 $f008->update($field);
1038 $xml = $marc->as_xml_record;
1039 $xml =~ s/^<\?.+?\?>$//mo;
1041 $xml =~ s/>\s+</></sgo;
1045 $$ LANGUAGE PLPERLU STABLE;
1048 CREATE OR REPLACE FUNCTION migration_tools.is_staff_profile (INT) RETURNS BOOLEAN AS $$
1050 profile ALIAS FOR $1;
1052 RETURN CASE WHEN 'Staff' IN (select (permission.grp_ancestors(profile)).name) THEN TRUE ELSE FALSE END;
1054 $$ LANGUAGE PLPGSQL STRICT STABLE;
1057 CREATE OR REPLACE FUNCTION migration_tools.is_blank (TEXT) RETURNS BOOLEAN AS $$
1059 RETURN CASE WHEN $1 = '' THEN TRUE ELSE FALSE END;
1061 $$ LANGUAGE PLPGSQL STRICT STABLE;
1064 CREATE OR REPLACE FUNCTION migration_tools.insert_tags (TEXT, TEXT) RETURNS TEXT AS $$
1066 my ($marcxml, $tags) = @_;
1069 use MARC::File::XML;
1074 my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8');
1075 my $to_insert = MARC::Record->new_from_xml("<record>$tags</record>", 'UTF-8');
1077 my @incumbents = ();
1079 foreach my $field ( $marc->fields() ) {
1080 push @incumbents, $field->as_formatted();
1083 foreach $field ( $to_insert->fields() ) {
1084 if (!grep {$_ eq $field->as_formatted()} @incumbents) {
1085 $marc->insert_fields_ordered( ($field) );
1089 $xml = $marc->as_xml_record;
1090 $xml =~ s/^<\?.+?\?>$//mo;
1092 $xml =~ s/>\s+</></sgo;
1097 $$ LANGUAGE PLPERLU STABLE;
1099 CREATE OR REPLACE FUNCTION migration_tools.apply_circ_matrix( tablename TEXT ) RETURNS VOID AS $$
1103 -- First make sure the circ matrix is loaded and the circulations
1104 -- have been staged to the extent possible (but at the very least
1105 -- circ_lib, target_copy, usr, and *_renewal). User profiles and
1106 -- circ modifiers must also be in place.
1108 -- SELECT migration_tools.apply_circ_matrix('m_pioneer.action_circulation');
1116 this_duration_rule INT;
1118 this_max_fine_rule INT;
1119 rcd config.rule_circ_duration%ROWTYPE;
1120 rrf config.rule_recurring_fine%ROWTYPE;
1121 rmf config.rule_max_fine%ROWTYPE;
1128 EXECUTE 'SELECT COUNT(*) FROM ' || tablename || ';' INTO n_circs;
1130 FOR circ IN EXECUTE ('SELECT id FROM ' || tablename) LOOP
1132 -- Fetch the correct rules for this circulation
1139 WHEN phone_renewal OR desk_renewal OR opac_renewal THEN TRUE
1142 FROM ' || tablename || ' WHERE id = ' || circ || ';')
1143 INTO circ_lib, target_copy, usr, is_renewal ;
1145 INTO this_duration_rule,
1149 recurring_fine_rule,
1151 FROM action.item_user_circ_test(
1157 SELECT INTO rcd * FROM config.rule_circ_duration
1158 WHERE id = this_duration_rule;
1159 SELECT INTO rrf * FROM config.rule_recurring_fine
1160 WHERE id = this_fine_rule;
1161 SELECT INTO rmf * FROM config.rule_max_fine
1162 WHERE id = this_max_fine_rule;
1164 -- Apply the rules to this circulation
1165 EXECUTE ('UPDATE ' || tablename || ' c
1167 duration_rule = rcd.name,
1168 recurring_fine_rule = rrf.name,
1169 max_fine_rule = rmf.name,
1170 duration = rcd.normal,
1171 recurring_fine = rrf.normal,
1174 WHEN TRUE THEN (rmf.amount / 100.0) * ac.price
1177 renewal_remaining = rcd.max_renewals
1179 config.rule_circ_duration rcd,
1180 config.rule_recurring_fine rrf,
1181 config.rule_max_fine rmf,
1184 rcd.id = ' || this_duration_rule || ' AND
1185 rrf.id = ' || this_fine_rule || ' AND
1186 rmf.id = ' || this_max_fine_rule || ' AND
1187 ac.id = c.target_copy AND
1188 c.id = ' || circ || ';');
1190 -- Keep track of where we are in the process
1192 IF (n % 100 = 0) THEN
1193 RAISE INFO '%', n || ' of ' || n_circs
1194 || ' (' || (100*n/n_circs) || '%) circs updated.';
1202 $$ LANGUAGE plpgsql;
1204 CREATE OR REPLACE FUNCTION migration_tools.apply_circ_matrix_before_20( tablename TEXT ) RETURNS VOID AS $$
1208 -- First make sure the circ matrix is loaded and the circulations
1209 -- have been staged to the extent possible (but at the very least
1210 -- circ_lib, target_copy, usr, and *_renewal). User profiles and
1211 -- circ modifiers must also be in place.
1213 -- SELECT migration_tools.apply_circ_matrix('m_pioneer.action_circulation');
1221 this_duration_rule INT;
1223 this_max_fine_rule INT;
1224 rcd config.rule_circ_duration%ROWTYPE;
1225 rrf config.rule_recurring_fine%ROWTYPE;
1226 rmf config.rule_max_fine%ROWTYPE;
1233 EXECUTE 'SELECT COUNT(*) FROM ' || tablename || ';' INTO n_circs;
1235 FOR circ IN EXECUTE ('SELECT id FROM ' || tablename) LOOP
1237 -- Fetch the correct rules for this circulation
1244 WHEN phone_renewal OR desk_renewal OR opac_renewal THEN TRUE
1247 FROM ' || tablename || ' WHERE id = ' || circ || ';')
1248 INTO circ_lib, target_copy, usr, is_renewal ;
1250 INTO this_duration_rule,
1256 FROM action.find_circ_matrix_matchpoint(
1262 SELECT INTO rcd * FROM config.rule_circ_duration
1263 WHERE id = this_duration_rule;
1264 SELECT INTO rrf * FROM config.rule_recurring_fine
1265 WHERE id = this_fine_rule;
1266 SELECT INTO rmf * FROM config.rule_max_fine
1267 WHERE id = this_max_fine_rule;
1269 -- Apply the rules to this circulation
1270 EXECUTE ('UPDATE ' || tablename || ' c
1272 duration_rule = rcd.name,
1273 recuring_fine_rule = rrf.name,
1274 max_fine_rule = rmf.name,
1275 duration = rcd.normal,
1276 recuring_fine = rrf.normal,
1279 WHEN TRUE THEN (rmf.amount / 100.0) * ac.price
1282 renewal_remaining = rcd.max_renewals
1284 config.rule_circ_duration rcd,
1285 config.rule_recuring_fine rrf,
1286 config.rule_max_fine rmf,
1289 rcd.id = ' || this_duration_rule || ' AND
1290 rrf.id = ' || this_fine_rule || ' AND
1291 rmf.id = ' || this_max_fine_rule || ' AND
1292 ac.id = c.target_copy AND
1293 c.id = ' || circ || ';');
1295 -- Keep track of where we are in the process
1297 IF (n % 100 = 0) THEN
1298 RAISE INFO '%', n || ' of ' || n_circs
1299 || ' (' || (100*n/n_circs) || '%) circs updated.';
1307 $$ LANGUAGE plpgsql;
1309 CREATE OR REPLACE FUNCTION migration_tools.apply_circ_matrix_after_20( tablename TEXT ) RETURNS VOID AS $$
1313 -- First make sure the circ matrix is loaded and the circulations
1314 -- have been staged to the extent possible (but at the very least
1315 -- circ_lib, target_copy, usr, and *_renewal). User profiles and
1316 -- circ modifiers must also be in place.
1318 -- SELECT migration_tools.apply_circ_matrix('m_pioneer.action_circulation');
1326 this_duration_rule INT;
1328 this_max_fine_rule INT;
1329 rcd config.rule_circ_duration%ROWTYPE;
1330 rrf config.rule_recurring_fine%ROWTYPE;
1331 rmf config.rule_max_fine%ROWTYPE;
1338 EXECUTE 'SELECT COUNT(*) FROM ' || tablename || ';' INTO n_circs;
1340 FOR circ IN EXECUTE ('SELECT id FROM ' || tablename) LOOP
1342 -- Fetch the correct rules for this circulation
1349 WHEN phone_renewal OR desk_renewal OR opac_renewal THEN TRUE
1352 FROM ' || tablename || ' WHERE id = ' || circ || ';')
1353 INTO circ_lib, target_copy, usr, is_renewal ;
1355 INTO this_duration_rule,
1358 (matchpoint).duration_rule,
1359 (matchpoint).recurring_fine_rule,
1360 (matchpoint).max_fine_rule
1361 FROM action.find_circ_matrix_matchpoint(
1367 SELECT INTO rcd * FROM config.rule_circ_duration
1368 WHERE id = this_duration_rule;
1369 SELECT INTO rrf * FROM config.rule_recurring_fine
1370 WHERE id = this_fine_rule;
1371 SELECT INTO rmf * FROM config.rule_max_fine
1372 WHERE id = this_max_fine_rule;
1374 -- Apply the rules to this circulation
1375 EXECUTE ('UPDATE ' || tablename || ' c
1377 duration_rule = rcd.name,
1378 recurring_fine_rule = rrf.name,
1379 max_fine_rule = rmf.name,
1380 duration = rcd.normal,
1381 recurring_fine = rrf.normal,
1384 WHEN TRUE THEN (rmf.amount / 100.0) * ac.price
1387 renewal_remaining = rcd.max_renewals,
1388 grace_period = rrf.grace_period
1390 config.rule_circ_duration rcd,
1391 config.rule_recurring_fine rrf,
1392 config.rule_max_fine rmf,
1395 rcd.id = ' || this_duration_rule || ' AND
1396 rrf.id = ' || this_fine_rule || ' AND
1397 rmf.id = ' || this_max_fine_rule || ' AND
1398 ac.id = c.target_copy AND
1399 c.id = ' || circ || ';');
1401 -- Keep track of where we are in the process
1403 IF (n % 100 = 0) THEN
1404 RAISE INFO '%', n || ' of ' || n_circs
1405 || ' (' || (100*n/n_circs) || '%) circs updated.';
1413 $$ LANGUAGE plpgsql;
1415 CREATE OR REPLACE FUNCTION migration_tools.apply_circ_matrix_to_specific_circ( tablename TEXT, circ BIGINT ) RETURNS VOID AS $$
1419 -- First make sure the circ matrix is loaded and the circulations
1420 -- have been staged to the extent possible (but at the very least
1421 -- circ_lib, target_copy, usr, and *_renewal). User profiles and
1422 -- circ modifiers must also be in place.
1424 -- SELECT migration_tools.apply_circ_matrix_to_specific_circ('m_nwrl.action_circulation', 18391960);
1432 this_duration_rule INT;
1434 this_max_fine_rule INT;
1435 rcd config.rule_circ_duration%ROWTYPE;
1436 rrf config.rule_recurring_fine%ROWTYPE;
1437 rmf config.rule_max_fine%ROWTYPE;
1443 --EXECUTE 'SELECT COUNT(*) FROM ' || tablename || ';' INTO n_circs;
1445 --FOR circ IN EXECUTE ('SELECT id FROM ' || tablename) LOOP
1447 -- Fetch the correct rules for this circulation
1454 WHEN phone_renewal OR desk_renewal OR opac_renewal THEN TRUE
1457 FROM ' || tablename || ' WHERE id = ' || circ || ';')
1458 INTO circ_lib, target_copy, usr, is_renewal ;
1460 INTO this_duration_rule,
1463 (matchpoint).duration_rule,
1464 (matchpoint).recurring_fine_rule,
1465 (matchpoint).max_fine_rule
1466 FROM action.find_circ_matrix_matchpoint(
1472 SELECT INTO rcd * FROM config.rule_circ_duration
1473 WHERE id = this_duration_rule;
1474 SELECT INTO rrf * FROM config.rule_recurring_fine
1475 WHERE id = this_fine_rule;
1476 SELECT INTO rmf * FROM config.rule_max_fine
1477 WHERE id = this_max_fine_rule;
1479 -- Apply the rules to this circulation
1480 EXECUTE ('UPDATE ' || tablename || ' c
1482 duration_rule = rcd.name,
1483 recurring_fine_rule = rrf.name,
1484 max_fine_rule = rmf.name,
1485 duration = rcd.normal,
1486 recurring_fine = rrf.normal,
1489 WHEN TRUE THEN (rmf.amount / 100.0) * ac.price
1492 renewal_remaining = rcd.max_renewals,
1493 grace_period = rrf.grace_period
1495 config.rule_circ_duration rcd,
1496 config.rule_recurring_fine rrf,
1497 config.rule_max_fine rmf,
1500 rcd.id = ' || this_duration_rule || ' AND
1501 rrf.id = ' || this_fine_rule || ' AND
1502 rmf.id = ' || this_max_fine_rule || ' AND
1503 ac.id = c.target_copy AND
1504 c.id = ' || circ || ';');
1506 -- Keep track of where we are in the process
1508 IF (n % 100 = 0) THEN
1509 RAISE INFO '%', n || ' of ' || n_circs
1510 || ' (' || (100*n/n_circs) || '%) circs updated.';
1518 $$ LANGUAGE plpgsql;
1523 CREATE OR REPLACE FUNCTION migration_tools.stage_not_applicable_asset_stat_cats( schemaname TEXT ) RETURNS VOID AS $$
1525 -- USAGE: Make sure the stat_cat and stat_cat_entry tables are populated, including exactly one 'Not Applicable' entry per stat cat.
1526 -- Then SELECT migration_tools.stage_not_applicable_asset_stat_cats('m_foo');
1528 -- TODO: Make a variant that will go directly to production tables -- which would be useful for retrofixing the absence of N/A cats.
1529 -- TODO: Add a similar tool for actor stat cats, which behave differently.
1532 c TEXT := schemaname || '.asset_copy_legacy';
1533 sc TEXT := schemaname || '.asset_stat_cat';
1534 sce TEXT := schemaname || '.asset_stat_cat_entry';
1535 scecm TEXT := schemaname || '.asset_stat_cat_entry_copy_map';
1541 FOR stat_cat IN EXECUTE ('SELECT id FROM ' || sc) LOOP
1543 EXECUTE ('SELECT id FROM ' || sce || ' WHERE stat_cat = ' || stat_cat || E' AND value = \'Not Applicable\';') INTO stat_cat_entry;
1545 EXECUTE ('INSERT INTO ' || scecm || ' (owning_copy, stat_cat, stat_cat_entry)
1546 SELECT c.id, ' || stat_cat || ', ' || stat_cat_entry || ' FROM ' || c || ' c WHERE c.id NOT IN
1547 (SELECT owning_copy FROM ' || scecm || ' WHERE stat_cat = ' || stat_cat || ');');
1554 $$ LANGUAGE plpgsql;
1556 CREATE OR REPLACE FUNCTION migration_tools.assign_standing_penalties ( ) RETURNS VOID AS $$
1558 -- USAGE: Once circulation data has been loaded, and group penalty thresholds have been set up, run this.
1559 -- This will assign standing penalties as needed.
1567 FOR org_unit IN EXECUTE ('SELECT DISTINCT org_unit FROM permission.grp_penalty_threshold;') LOOP
1569 FOR usr IN EXECUTE ('SELECT id FROM actor.usr WHERE NOT deleted;') LOOP
1571 EXECUTE('SELECT actor.calculate_system_penalties(' || usr || ', ' || org_unit || ');');
1581 $$ LANGUAGE plpgsql;
1584 CREATE OR REPLACE FUNCTION migration_tools.insert_metarecords_for_pristine_database () RETURNS VOID AS $$
1587 INSERT INTO metabib.metarecord (fingerprint, master_record)
1588 SELECT DISTINCT ON (b.fingerprint) b.fingerprint, b.id
1589 FROM biblio.record_entry b
1591 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)
1592 AND NOT EXISTS ( SELECT 1 FROM metabib.metarecord WHERE fingerprint = b.fingerprint )
1593 ORDER BY b.fingerprint, b.quality DESC;
1594 INSERT INTO metabib.metarecord_source_map (metarecord, source)
1596 FROM biblio.record_entry r
1597 JOIN metabib.metarecord m USING (fingerprint)
1598 WHERE NOT r.deleted;
1601 $$ LANGUAGE plpgsql;
1604 CREATE OR REPLACE FUNCTION migration_tools.insert_metarecords_for_incumbent_database () RETURNS VOID AS $$
1607 INSERT INTO metabib.metarecord (fingerprint, master_record)
1608 SELECT DISTINCT ON (b.fingerprint) b.fingerprint, b.id
1609 FROM biblio.record_entry b
1611 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)
1612 AND NOT EXISTS ( SELECT 1 FROM metabib.metarecord WHERE fingerprint = b.fingerprint )
1613 ORDER BY b.fingerprint, b.quality DESC;
1614 INSERT INTO metabib.metarecord_source_map (metarecord, source)
1616 FROM biblio.record_entry r
1617 JOIN metabib.metarecord m USING (fingerprint)
1619 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);
1622 $$ LANGUAGE plpgsql;
1625 CREATE OR REPLACE FUNCTION migration_tools.create_cards( schemaname TEXT ) RETURNS VOID AS $$
1627 -- USAGE: Make sure the patrons are staged in schemaname.actor_usr_legacy and have 'usrname' assigned.
1628 -- Then SELECT migration_tools.create_cards('m_foo');
1631 u TEXT := schemaname || '.actor_usr_legacy';
1632 c TEXT := schemaname || '.actor_card';
1636 EXECUTE ('DELETE FROM ' || c || ';');
1637 EXECUTE ('INSERT INTO ' || c || ' (usr, barcode) SELECT id, usrname FROM ' || u || ';');
1638 EXECUTE ('UPDATE ' || u || ' u SET card = c.id FROM ' || c || ' c WHERE c.usr = u.id;');
1644 $$ LANGUAGE plpgsql;
1647 CREATE OR REPLACE FUNCTION migration_tools.insert_856_9_conditional (TEXT, TEXT) RETURNS TEXT AS $$
1649 ## USAGE: UPDATE biblio.record_entry SET marc = migration_tools.insert_856_9(marc, 'ABC') WHERE [...];
1651 my ($marcxml, $shortname) = @_;
1654 use MARC::File::XML;
1659 my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8');
1661 foreach my $field ( $marc->field('856') ) {
1662 if ( scalar(grep( /(contentreserve|netlibrary|overdrive)\.com/i, $field->subfield('u'))) > 0 &&
1663 ! ( $field->as_string('9') =~ m/$shortname/ ) ) {
1664 $field->add_subfields( '9' => $shortname );
1665 $field->update( ind2 => '0');
1669 $xml = $marc->as_xml_record;
1670 $xml =~ s/^<\?.+?\?>$//mo;
1672 $xml =~ s/>\s+</></sgo;
1677 $$ LANGUAGE PLPERLU STABLE;
1679 CREATE OR REPLACE FUNCTION migration_tools.insert_856_9 (TEXT, TEXT) RETURNS TEXT AS $$
1681 ## USAGE: UPDATE biblio.record_entry SET marc = migration_tools.insert_856_9(marc, 'ABC') WHERE [...];
1683 my ($marcxml, $shortname) = @_;
1686 use MARC::File::XML;
1691 my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8');
1693 foreach my $field ( $marc->field('856') ) {
1694 if ( ! $field->as_string('9') ) {
1695 $field->add_subfields( '9' => $shortname );
1699 $xml = $marc->as_xml_record;
1700 $xml =~ s/^<\?.+?\?>$//mo;
1702 $xml =~ s/>\s+</></sgo;
1707 $$ LANGUAGE PLPERLU STABLE;
1710 CREATE OR REPLACE FUNCTION migration_tools.change_call_number(copy_id BIGINT, new_label TEXT, cn_class BIGINT) RETURNS VOID AS $$
1722 -- Bail out if asked to change the label to ##URI##
1723 IF new_label = '##URI##' THEN
1727 -- Gather information
1728 SELECT call_number INTO old_volume FROM asset.copy WHERE id = copy_id;
1729 SELECT record INTO bib FROM asset.call_number WHERE id = old_volume;
1730 SELECT owning_lib, label INTO owner, old_label FROM asset.call_number WHERE id = old_volume;
1732 -- Bail out if the label already is ##URI##
1733 IF old_label = '##URI##' THEN
1737 -- Bail out if the call number label is already correct
1738 IF new_volume = old_volume THEN
1742 -- Check whether we already have a destination volume available
1743 SELECT id INTO new_volume FROM asset.call_number
1746 owning_lib = owner AND
1747 label = new_label AND
1750 -- Create destination volume if needed
1752 INSERT INTO asset.call_number (creator, editor, record, owning_lib, label, label_class)
1753 VALUES (1, 1, bib, owner, new_label, cn_class);
1754 SELECT id INTO new_volume FROM asset.call_number
1757 owning_lib = owner AND
1758 label = new_label AND
1762 -- Move copy to destination
1763 UPDATE asset.copy SET call_number = new_volume WHERE id = copy_id;
1765 -- Delete source volume if it is now empty
1766 SELECT id INTO remainder FROM asset.copy WHERE call_number = old_volume AND NOT deleted;
1768 DELETE FROM asset.call_number WHERE id = old_volume;
1773 $$ LANGUAGE plpgsql;
1775 CREATE OR REPLACE FUNCTION migration_tools.zip_to_city_state_county (TEXT) RETURNS TEXT[] AS $$
1780 open (FH, '<', '/openils/var/data/zips.txt') or return ('No File Found', 'No File Found', 'No File Found');
1784 my ($junk, $state, $city, $zip, $foo, $bar, $county, $baz, $morejunk) = split(/\|/);
1785 $zipdata{$zip} = [$city, $state, $county];
1788 if (defined $zipdata{$input}) {
1789 my ($city, $state, $county) = @{$zipdata{$input}};
1790 return [$city, $state, $county];
1791 } elsif (defined $zipdata{substr $input, 0, 5}) {
1792 my ($city, $state, $county) = @{$zipdata{substr $input, 0, 5}};
1793 return [$city, $state, $county];
1795 return ['ZIP not found', 'ZIP not found', 'ZIP not found'];
1798 $$ LANGUAGE PLPERLU STABLE;
1800 CREATE OR REPLACE FUNCTION migration_tools.check_ou_depth ( ) RETURNS VOID AS $$
1807 errors_found BOOLEAN;
1809 parent_shortname TEXT;
1815 type_parent_depth INT;
1820 errors_found := FALSE;
1822 -- Checking actor.org_unit_type
1824 FOR type_id IN EXECUTE ('SELECT id FROM actor.org_unit_type ORDER BY id;') LOOP
1826 SELECT depth FROM actor.org_unit_type WHERE id = type_id INTO type_depth;
1827 SELECT parent FROM actor.org_unit_type WHERE id = type_id INTO type_parent;
1829 IF type_parent IS NOT NULL THEN
1831 SELECT depth FROM actor.org_unit_type WHERE id = type_parent INTO type_parent_depth;
1833 IF type_depth - type_parent_depth <> 1 THEN
1834 SELECT name FROM actor.org_unit_type WHERE id = type_id INTO ou_type_name;
1835 SELECT name FROM actor.org_unit_type WHERE id = type_parent INTO parent_type;
1836 RAISE INFO 'The % org unit type has a depth of %, but its parent org unit type, %, has a depth of %.',
1837 ou_type_name, type_depth, parent_type, type_parent_depth;
1838 errors_found := TRUE;
1846 -- Checking actor.org_unit
1848 FOR ou IN EXECUTE ('SELECT id FROM actor.org_unit ORDER BY shortname;') LOOP
1850 SELECT parent_ou FROM actor.org_unit WHERE id = ou INTO ou_parent;
1851 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;
1852 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;
1853 SELECT shortname FROM actor.org_unit WHERE id = ou INTO ou_shortname;
1854 SELECT shortname FROM actor.org_unit WHERE id = ou_parent INTO parent_shortname;
1855 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;
1856 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;
1858 IF ou_parent IS NOT NULL THEN
1860 IF (org_unit_depth - parent_depth <> 1) OR (
1861 (SELECT parent FROM actor.org_unit_type WHERE name = ou_type_name) <> (SELECT id FROM actor.org_unit_type WHERE name = parent_type)
1863 RAISE INFO '% (org unit %) is a % (depth %) but its parent, % (org unit %), is a % (depth %).',
1864 ou_shortname, ou, ou_type_name, org_unit_depth, parent_shortname, ou_parent, parent_type, parent_depth;
1865 errors_found := TRUE;
1872 IF NOT errors_found THEN
1873 RAISE INFO 'No errors found.';
1880 $$ LANGUAGE plpgsql;
1883 CREATE OR REPLACE FUNCTION migration_tools.refresh_opac_visible_copies ( ) RETURNS VOID AS $$
1887 DELETE FROM asset.opac_visible_copies;
1889 INSERT INTO asset.opac_visible_copies (id, circ_lib, record)
1891 cp.id, cp.circ_lib, cn.record
1894 JOIN asset.call_number cn ON (cn.id = cp.call_number)
1895 JOIN actor.org_unit a ON (cp.circ_lib = a.id)
1896 JOIN asset.copy_location cl ON (cp.location = cl.id)
1897 JOIN config.copy_status cs ON (cp.status = cs.id)
1898 JOIN biblio.record_entry b ON (cn.record = b.id)
1907 cp.id NOT IN (SELECT id FROM asset.opac_visible_copies);
1911 $$ LANGUAGE plpgsql;
1914 CREATE OR REPLACE FUNCTION migration_tools.change_owning_lib(copy_id BIGINT, new_owning_lib INTEGER) RETURNS VOID AS $$
1920 old_owning_lib INTEGER;
1926 -- Gather information
1927 SELECT call_number INTO old_volume FROM asset.copy WHERE id = copy_id;
1928 SELECT record INTO bib FROM asset.call_number WHERE id = old_volume;
1929 SELECT owning_lib, label INTO old_owning_lib, old_label FROM asset.call_number WHERE id = old_volume;
1931 -- Bail out if the new_owning_lib is not the ID of an org_unit
1932 IF new_owning_lib NOT IN (SELECT id FROM actor.org_unit) THEN
1934 '% is not a valid actor.org_unit ID; no change made.',
1939 -- Bail out discreetly if the owning_lib is already correct
1940 IF new_owning_lib = old_owning_lib THEN
1944 -- Check whether we already have a destination volume available
1945 SELECT id INTO new_volume FROM asset.call_number
1948 owning_lib = new_owning_lib AND
1949 label = old_label AND
1952 -- Create destination volume if needed
1954 INSERT INTO asset.call_number (creator, editor, record, owning_lib, label)
1955 VALUES (1, 1, bib, new_owning_lib, old_label);
1956 SELECT id INTO new_volume FROM asset.call_number
1959 owning_lib = new_owning_lib AND
1960 label = old_label AND
1964 -- Move copy to destination
1965 UPDATE asset.copy SET call_number = new_volume WHERE id = copy_id;
1967 -- Delete source volume if it is now empty
1968 SELECT id INTO remainder FROM asset.copy WHERE call_number = old_volume AND NOT deleted;
1970 DELETE FROM asset.call_number WHERE id = old_volume;
1975 $$ LANGUAGE plpgsql;
1978 CREATE OR REPLACE FUNCTION migration_tools.change_owning_lib(copy_id BIGINT, new_owner TEXT) RETURNS VOID AS $$
1980 -- You can use shortnames with this function, which looks up the org unit ID and passes it to change_owning_lib(BIGINT,INTEGER).
1983 new_owning_lib INTEGER;
1987 -- Parse the new_owner as an org unit ID or shortname
1988 IF new_owner IN (SELECT shortname FROM actor.org_unit) THEN
1989 SELECT id INTO new_owning_lib FROM actor.org_unit WHERE shortname = new_owner;
1990 PERFORM migration_tools.change_owning_lib(copy_id, new_owning_lib);
1991 ELSIF new_owner ~ E'^[0-9]+$' THEN
1992 IF new_owner::INTEGER IN (SELECT id FROM actor.org_unit) THEN
1995 E'You don\'t need to put the actor.org_unit ID in quotes; '
1996 || E'if you put it in quotes, I\'m going to try to parse it as a shortname first.';
1997 new_owning_lib := new_owner::INTEGER;
1998 PERFORM migration_tools.change_owning_lib(copy_id, new_owning_lib);
2002 '% is not a valid actor.org_unit shortname or ID; no change made.',
2009 $$ LANGUAGE plpgsql;
2011 CREATE OR REPLACE FUNCTION migration_tools.marc_parses( TEXT ) RETURNS BOOLEAN AS $func$
2014 use MARC::File::XML (BinaryEncoding => 'UTF-8');
2017 MARC::Charset->assume_unicode(1);
2022 my $r = MARC::Record->new_from_xml( $xml );
2023 my $output_xml = $r->as_xml_record();
2031 $func$ LANGUAGE PLPERLU;
2032 COMMENT ON FUNCTION migration_tools.marc_parses(TEXT) IS 'Return boolean indicating if MARCXML string is parseable by MARC::File::XML';
2034 CREATE OR REPLACE FUNCTION migration_tools.simple_export_library_config(dir TEXT, orgs INT[]) RETURNS VOID AS $FUNC$
2036 EXECUTE $$COPY (SELECT * FROM actor.hours_of_operation WHERE id IN ($$ ||
2037 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
2038 $$) TO '$$ || dir || $$/actor_hours_of_operation'$$;
2039 EXECUTE $$COPY (SELECT org_unit, close_start, close_end, reason FROM actor.org_unit_closed WHERE org_unit IN ($$ ||
2040 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
2041 $$) TO '$$ || dir || $$/actor_org_unit_closed'$$;
2042 EXECUTE $$COPY (SELECT org_unit, name, value FROM actor.org_unit_setting WHERE org_unit IN ($$ ||
2043 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
2044 $$) TO '$$ || dir || $$/actor_org_unit_setting'$$;
2045 EXECUTE $$COPY (SELECT name, owning_lib, holdable, hold_verify, opac_visible, circulate FROM asset.copy_location WHERE owning_lib IN ($$ ||
2046 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
2047 $$) TO '$$ || dir || $$/asset_copy_location'$$;
2048 EXECUTE $$COPY (SELECT grp, org_unit, penalty, threshold FROM permission.grp_penalty_threshold WHERE org_unit IN ($$ ||
2049 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
2050 $$) TO '$$ || dir || $$/permission_grp_penalty_threshold'$$;
2051 EXECUTE $$COPY (SELECT owning_lib, label, label_sortkey FROM asset.call_number_prefix WHERE owning_lib IN ($$ ||
2052 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
2053 $$) TO '$$ || dir || $$/asset_call_number_prefix'$$;
2054 EXECUTE $$COPY (SELECT owning_lib, label, label_sortkey FROM asset.call_number_suffix WHERE owning_lib IN ($$ ||
2055 ARRAY_TO_STRING(orgs, ',') || $$)$$ ||
2056 $$) TO '$$ || dir || $$/asset_call_number_suffix'$$;
2057 EXECUTE $$COPY config.rule_circ_duration TO '$$ || dir || $$/config_rule_circ_duration'$$;
2058 EXECUTE $$COPY config.rule_age_hold_protect TO '$$ || dir || $$/config_rule_age_hold_protect'$$;
2059 EXECUTE $$COPY config.rule_max_fine TO '$$ || dir || $$/config_rule_max_fine'$$;
2060 EXECUTE $$COPY config.rule_recurring_fine TO '$$ || dir || $$/config_rule_recurring_fine'$$;
2061 EXECUTE $$COPY permission.grp_tree TO '$$ || dir || $$/permission_grp_tree'$$;
2063 $FUNC$ LANGUAGE PLPGSQL;
2065 CREATE OR REPLACE FUNCTION migration_tools.simple_import_library_config(dir TEXT) RETURNS VOID AS $FUNC$
2067 EXECUTE $$COPY actor.hours_of_operation FROM '$$ || dir || $$/actor_hours_of_operation'$$;
2068 EXECUTE $$COPY actor.org_unit_closed (org_unit, close_start, close_end, reason) FROM '$$ || dir || $$/actor_org_unit_closed'$$;
2069 EXECUTE $$COPY actor.org_unit_setting (org_unit, name, value) FROM '$$ || dir || $$/actor_org_unit_setting'$$;
2070 EXECUTE $$COPY asset.copy_location (name, owning_lib, holdable, hold_verify, opac_visible, circulate) FROM '$$ || dir || $$/asset_copy_location'$$;
2071 EXECUTE $$COPY permission.grp_penalty_threshold (grp, org_unit, penalty, threshold) FROM '$$ || dir || $$/permission_grp_penalty_threshold'$$;
2072 EXECUTE $$COPY asset.call_number_prefix (owning_lib, label, label_sortkey) FROM '$$ || dir || $$/asset_call_number_prefix'$$;
2073 EXECUTE $$COPY asset.call_number_suffix (owning_lib, label, label_sortkey) FROM '$$ || dir || $$/asset_call_number_suffix'$$;
2075 -- import any new circ rules
2076 PERFORM migration_tools.simple_import_new_rows_by_value(dir, 'config', 'rule_circ_duration', 'id', 'name');
2077 PERFORM migration_tools.simple_import_new_rows_by_value(dir, 'config', 'rule_age_hold_protect', 'id', 'name');
2078 PERFORM migration_tools.simple_import_new_rows_by_value(dir, 'config', 'rule_max_fine', 'id', 'name');
2079 PERFORM migration_tools.simple_import_new_rows_by_value(dir, 'config', 'rule_recurring_fine', 'id', 'name');
2081 -- and permission groups
2082 PERFORM migration_tools.simple_import_new_rows_by_value(dir, 'permission', 'grp_tree', 'id', 'name');
2085 $FUNC$ LANGUAGE PLPGSQL;
2088 CREATE OR REPLACE FUNCTION migration_tools.simple_import_new_rows_by_value(dir TEXT, schemaname TEXT, tablename TEXT, idcol TEXT, matchcol TEXT) RETURNS VOID AS $FUNC$
2097 EXECUTE $$DROP TABLE IF EXISTS tmp_$$ || tablename;
2098 EXECUTE $$CREATE TEMPORARY TABLE tmp_$$ || tablename || $$ AS SELECT * FROM $$ || schemaname || '.' || tablename || $$ LIMIT 0$$;
2099 EXECUTE $$COPY tmp_$$ || tablename || $$ FROM '$$ || dir || '/' || schemaname || '_' || tablename || $$'$$;
2100 loopq := 'SELECT ' || matchcol || ' FROM tmp_' || tablename || ' ORDER BY ' || idcol;
2101 existsq := 'SELECT COUNT(*) FROM ' || schemaname || '.' || tablename || ' WHERE ' || matchcol || ' = $1';
2102 SELECT ARRAY_AGG(column_name::TEXT) INTO cols FROM information_schema.columns WHERE table_schema = schemaname AND table_name = tablename AND column_name <> idcol;
2103 FOR name IN EXECUTE loopq LOOP
2104 EXECUTE existsq INTO ct USING name;
2106 RAISE NOTICE 'inserting %.% row for %', schemaname, tablename, name;
2107 copyst := 'INSERT INTO ' || schemaname || '.' || tablename || ' (' || ARRAY_TO_STRING(cols, ',') || ') SELECT ' || ARRAY_TO_STRING(cols, ',') ||
2108 ' FROM tmp_' || tablename || ' WHERE ' || matchcol || ' = $1';
2109 EXECUTE copyst USING name;
2113 $FUNC$ LANGUAGE PLPGSQL;
2115 CREATE OR REPLACE FUNCTION migration_tools.merge_marc_fields( TEXT, TEXT, TEXT[] ) RETURNS TEXT AS $func$
2121 use MARC::File::XML (BinaryEncoding => 'UTF-8');
2124 MARC::Charset->assume_unicode(1);
2126 my $target_xml = shift;
2127 my $source_xml = shift;
2133 eval { $target = MARC::Record->new_from_xml( $target_xml ); };
2137 eval { $source = MARC::Record->new_from_xml( $source_xml ); };
2142 my $source_id = $source->subfield('901', 'c');
2143 $source_id = $source->subfield('903', 'a') unless $source_id;
2144 my $target_id = $target->subfield('901', 'c');
2145 $target_id = $target->subfield('903', 'a') unless $target_id;
2147 my %existing_fields;
2148 foreach my $tag (@$tags) {
2149 my %existing_fields = map { $_->as_formatted() => 1 } $target->field($tag);
2150 my @to_add = grep { not exists $existing_fields{$_->as_formatted()} } $source->field($tag);
2151 $target->insert_fields_ordered(map { $_->clone() } @to_add);
2153 elog(NOTICE, "Merged $tag tag(s) from $source_id to $target_id");
2157 my $xml = $target->as_xml_record;
2158 $xml =~ s/^<\?.+?\?>$//mo;
2160 $xml =~ s/>\s+</></sgo;
2164 $func$ LANGUAGE PLPERLU;
2165 COMMENT ON FUNCTION migration_tools.merge_marc_fields( TEXT, TEXT, TEXT[] ) IS 'Given two MARCXML strings and an array of tags, returns MARCXML representing the merge of the specified fields from the second MARCXML record into the first.';
2167 CREATE OR REPLACE FUNCTION migration_tools.make_stub_bib (text[], text[]) RETURNS TEXT AS $func$
2173 use MARC::File::XML (BinaryEncoding => 'UTF-8');
2176 my $in_tags = shift;
2177 my $in_values = shift;
2179 # hack-and-slash parsing of array-passed-as-string;
2180 # this can go away once everybody is running Postgres 9.1+
2181 my $csv = Text::CSV->new({binary => 1});
2184 my $status = $csv->parse($in_tags);
2185 my $tags = [ $csv->fields() ];
2186 $in_values =~ s/^{//;
2187 $in_values =~ s/}$//;
2188 $status = $csv->parse($in_values);
2189 my $values = [ $csv->fields() ];
2191 my $marc = MARC::Record->new();
2193 $marc->leader('00000nam a22000007 4500');
2194 $marc->append_fields(MARC::Field->new('008', '000000s 000 eng d'));
2196 foreach my $i (0..$#$tags) {
2198 if ($tags->[$i] =~ /^(\d{3})([0-9a-z])$/) {
2201 $marc->append_fields(MARC::Field->new($tag, ' ', ' ', $sf => $values->[$i])) if $values->[$i] !~ /^\s*$/ and $values->[$i] ne 'NULL';
2202 } elsif ($tags->[$i] =~ /^(\d{3})$/) {
2204 $marc->append_fields(MARC::Field->new($tag, $values->[$i])) if $values->[$i] !~ /^\s*$/ and $values->[$i] ne 'NULL';
2208 my $xml = $marc->as_xml_record;
2209 $xml =~ s/^<\?.+?\?>$//mo;
2211 $xml =~ s/>\s+</></sgo;
2215 $func$ LANGUAGE PLPERLU;
2216 COMMENT ON FUNCTION migration_tools.make_stub_bib (text[], text[]) IS $$Simple function to create a stub MARCXML bib from a set of columns.
2217 The first argument is an array of tag/subfield specifiers, e.g., ARRAY['001', '245a', '500a'].
2218 The second argument is an array of text containing the values to plug into each field.
2219 If the value for a given field is NULL or the empty string, it is not inserted.
2222 CREATE OR REPLACE FUNCTION migration_tools.set_indicator (TEXT, TEXT, INTEGER, CHAR(1)) RETURNS TEXT AS $func$
2224 my ($marcxml, $tag, $pos, $value) = @_;
2227 use MARC::File::XML (BinaryEncoding => 'UTF-8');
2231 MARC::Charset->assume_unicode(1);
2233 elog(ERROR, 'indicator position must be either 1 or 2') unless $pos =~ /^[12]$/;
2234 elog(ERROR, 'MARC tag must be numeric') unless $tag =~ /^\d{3}$/;
2235 elog(ERROR, 'MARC tag must not be control field') if $tag =~ /^00/;
2236 elog(ERROR, 'Value must be exactly one character') unless $value =~ /^.$/;
2240 my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8');
2242 foreach my $field ($marc->field($tag)) {
2243 $field->update("ind$pos" => $value);
2245 $xml = $marc->as_xml_record;
2246 $xml =~ s/^<\?.+?\?>$//mo;
2248 $xml =~ s/>\s+</></sgo;
2252 $func$ LANGUAGE PLPERLU;
2254 COMMENT ON FUNCTION migration_tools.set_indicator(TEXT, TEXT, INTEGER, CHAR(1)) IS $$Set indicator value of a specified MARC field.
2255 The first argument is a MARCXML string.
2256 The second argument is a MARC tag.
2257 The third argument is the indicator position, either 1 or 2.
2258 The fourth argument is the character to set the indicator value to.
2259 All occurences of the specified field will be changed.
2260 The function returns the revised MARCXML string.$$;
2262 CREATE OR REPLACE FUNCTION migration_tools.create_staff_user(
2267 first_name TEXT DEFAULT '',
2268 last_name TEXT DEFAULT ''
2269 ) RETURNS VOID AS $func$
2271 RAISE NOTICE '%', org ;
2272 INSERT INTO actor.usr (usrname, passwd, ident_type, first_given_name, family_name, home_ou, profile)
2273 SELECT username, password, 1, first_name, last_name, aou.id, pgt.id
2274 FROM actor.org_unit aou, permission.grp_tree pgt
2275 WHERE aou.shortname = org
2276 AND pgt.name = perm_group;
2281 -- example: SELECT * FROM migration_tools.duplicate_template(5,'{3,4}');
2282 CREATE OR REPLACE FUNCTION migration_tools.duplicate_template (INTEGER, INTEGER[]) RETURNS VOID AS $$
2284 target_event_def ALIAS FOR $1;
2287 DROP TABLE IF EXISTS new_atevdefs;
2288 CREATE TEMP TABLE new_atevdefs (atevdef INTEGER);
2289 FOR i IN array_lower(orgs,1) .. array_upper(orgs,1) LOOP
2290 INSERT INTO action_trigger.event_definition (
2311 ,name || ' (clone of '||target_event_def||')'
2327 action_trigger.event_definition
2329 id = target_event_def
2331 RAISE INFO 'created atevdef with id = %', currval('action_trigger.event_definition_id_seq');
2332 INSERT INTO new_atevdefs SELECT currval('action_trigger.event_definition_id_seq');
2333 INSERT INTO action_trigger.environment (
2339 currval('action_trigger.event_definition_id_seq')
2344 action_trigger.environment
2346 event_def = target_event_def
2348 INSERT INTO action_trigger.event_params (
2353 currval('action_trigger.event_definition_id_seq')
2357 action_trigger.event_params
2359 event_def = target_event_def
2362 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);
2364 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
2366 -- example: SELECT * FROM migration_tools.duplicate_template_but_change_delay(5,'{3,4}','00:30:00'::INTERVAL);
2367 CREATE OR REPLACE FUNCTION migration_tools.duplicate_template_but_change_delay (INTEGER, INTEGER[], INTERVAL) RETURNS VOID AS $$
2369 target_event_def ALIAS FOR $1;
2371 new_interval ALIAS FOR $3;
2373 DROP TABLE IF EXISTS new_atevdefs;
2374 CREATE TEMP TABLE new_atevdefs (atevdef INTEGER);
2375 FOR i IN array_lower(orgs,1) .. array_upper(orgs,1) LOOP
2376 INSERT INTO action_trigger.event_definition (
2397 ,name || ' (clone of '||target_event_def||')'
2413 action_trigger.event_definition
2415 id = target_event_def
2417 RAISE INFO 'created atevdef with id = %', currval('action_trigger.event_definition_id_seq');
2418 INSERT INTO new_atevdefs SELECT currval('action_trigger.event_definition_id_seq');
2419 INSERT INTO action_trigger.environment (
2425 currval('action_trigger.event_definition_id_seq')
2430 action_trigger.environment
2432 event_def = target_event_def
2434 INSERT INTO action_trigger.event_params (
2439 currval('action_trigger.event_definition_id_seq')
2443 action_trigger.event_params
2445 event_def = target_event_def
2448 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);
2450 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
2452 CREATE OR REPLACE FUNCTION migration_tools.get_marc_tag (TEXT, TEXT, TEXT, TEXT) RETURNS TEXT AS $$
2453 my ($marcxml, $tag, $subfield, $delimiter) = @_;
2456 use MARC::File::XML;
2461 my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8');
2462 $field = $marc->field($tag);
2464 return $field->as_string($subfield,$delimiter);
2465 $$ LANGUAGE PLPERLU STABLE;
2467 CREATE OR REPLACE FUNCTION migration_tools.get_marc_tags (TEXT, TEXT, TEXT, TEXT) RETURNS TEXT[] AS $$
2468 my ($marcxml, $tag, $subfield, $delimiter) = @_;
2471 use MARC::File::XML;
2476 my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8');
2477 @fields = $marc->field($tag);
2480 foreach my $field (@fields) {
2481 push @texts, $field->as_string($subfield,$delimiter);
2484 $$ LANGUAGE PLPERLU STABLE;
2486 CREATE OR REPLACE FUNCTION migration_tools.find_hold_matrix_matchpoint (INTEGER) RETURNS INTEGER AS $$
2487 SELECT action.find_hold_matrix_matchpoint(
2488 (SELECT pickup_lib FROM action.hold_request WHERE id = $1),
2489 (SELECT request_lib FROM action.hold_request WHERE id = $1),
2490 (SELECT current_copy FROM action.hold_request WHERE id = $1),
2491 (SELECT usr FROM action.hold_request WHERE id = $1),
2492 (SELECT requestor FROM action.hold_request WHERE id = $1)
2496 CREATE OR REPLACE FUNCTION migration_tools.find_hold_matrix_matchpoint2 (INTEGER) RETURNS SETOF action.matrix_test_result AS $$
2497 SELECT action.hold_request_permit_test(
2498 (SELECT pickup_lib FROM action.hold_request WHERE id = $1),
2499 (SELECT request_lib FROM action.hold_request WHERE id = $1),
2500 (SELECT current_copy FROM action.hold_request WHERE id = $1),
2501 (SELECT usr FROM action.hold_request WHERE id = $1),
2502 (SELECT requestor FROM action.hold_request WHERE id = $1)
2506 CREATE OR REPLACE FUNCTION migration_tools.find_circ_matrix_matchpoint (INTEGER) RETURNS SETOF action.found_circ_matrix_matchpoint AS $$
2507 SELECT action.find_circ_matrix_matchpoint(
2508 (SELECT circ_lib FROM action.circulation WHERE id = $1),
2509 (SELECT target_copy FROM action.circulation WHERE id = $1),
2510 (SELECT usr FROM action.circulation WHERE id = $1),
2512 NULLIF(phone_renewal,false),
2513 NULLIF(desk_renewal,false),
2514 NULLIF(opac_renewal,false),
2516 ) FROM action.circulation WHERE id = $1
2521 CREATE OR REPLACE FUNCTION migration_tools.assert (BOOLEAN) RETURNS VOID AS $$
2526 RAISE EXCEPTION 'assertion';
2529 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
2531 CREATE OR REPLACE FUNCTION migration_tools.assert (BOOLEAN,TEXT) RETURNS VOID AS $$
2537 RAISE EXCEPTION '%', msg;
2540 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
2542 CREATE OR REPLACE FUNCTION migration_tools.assert (BOOLEAN,TEXT,TEXT) RETURNS TEXT AS $$
2545 fail_msg ALIAS FOR $2;
2546 success_msg ALIAS FOR $3;
2549 RAISE EXCEPTION '%', fail_msg;
2553 $$ LANGUAGE PLPGSQL STRICT VOLATILE;