1 --------------------------------------------------------------------------
2 -- An example of how to use:
4 -- DROP SCHEMA foo CASCADE; CREATE SCHEMA foo;
6 -- SELECT migration_tools.init('foo');
7 -- SELECT migration_tools.build('foo');
8 -- SELECT * FROM foo.fields_requiring_mapping;
10 -- create some incoming ILS specific staging tables, like CREATE foo.legacy_items ( l_barcode TEXT, .. ) INHERITS foo.asset_copy;
11 -- Do some mapping, like UPDATE foo.legacy_items SET barcode = TRIM(BOTH ' ' FROM l_barcode);
12 -- Then, to move into production, do: select migration_tools.insert_base_into_production('foo')
14 CREATE SCHEMA migration_tools;
16 CREATE OR REPLACE FUNCTION migration_tools.production_tables (TEXT) RETURNS TEXT[] AS $$
18 migration_schema ALIAS FOR $1;
22 EXECUTE 'SELECT string_to_array(value,'','') AS tables FROM ' || migration_schema || '.config WHERE key = ''production_tables'';'
27 $$ LANGUAGE PLPGSQL STRICT STABLE;
29 CREATE OR REPLACE FUNCTION migration_tools.country_code (TEXT) RETURNS TEXT AS $$
31 migration_schema ALIAS FOR $1;
35 EXECUTE 'SELECT value FROM ' || migration_schema || '.config WHERE key = ''country_code'';'
40 $$ LANGUAGE PLPGSQL STRICT STABLE;
43 CREATE OR REPLACE FUNCTION migration_tools.log (TEXT,TEXT,INTEGER) RETURNS VOID AS $$
45 migration_schema ALIAS FOR $1;
49 EXECUTE 'INSERT INTO ' || migration_schema || '.sql_log ( sql, row_count ) VALUES ( ' || quote_literal(sql) || ', ' || nrows || ' );';
51 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
53 CREATE OR REPLACE FUNCTION migration_tools.exec (TEXT,TEXT) RETURNS VOID AS $$
55 migration_schema ALIAS FOR $1;
59 EXECUTE 'UPDATE ' || migration_schema || '.sql_current SET sql = ' || quote_literal(sql) || ';';
60 --RAISE INFO '%', sql;
62 GET DIAGNOSTICS nrows = ROW_COUNT;
63 PERFORM migration_tools.log(migration_schema,sql,nrows);
66 RAISE EXCEPTION '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
68 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
70 CREATE OR REPLACE FUNCTION migration_tools.debug_exec (TEXT,TEXT) RETURNS VOID AS $$
72 migration_schema ALIAS FOR $1;
76 EXECUTE 'UPDATE ' || migration_schema || '.sql_current SET sql = ' || quote_literal(sql) || ';';
77 RAISE INFO 'debug_exec sql = %', sql;
79 GET DIAGNOSTICS nrows = ROW_COUNT;
80 PERFORM migration_tools.log(migration_schema,sql,nrows);
83 RAISE EXCEPTION '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
85 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
87 CREATE OR REPLACE FUNCTION migration_tools.init (TEXT) RETURNS VOID AS $$
89 migration_schema ALIAS FOR $1;
92 EXECUTE 'DROP TABLE IF EXISTS ' || migration_schema || '.sql_current;';
93 EXECUTE 'CREATE TABLE ' || migration_schema || '.sql_current ( sql TEXT);';
94 EXECUTE 'INSERT INTO ' || migration_schema || '.sql_current ( sql ) VALUES ( '''' );';
96 SELECT 'CREATE TABLE ' || migration_schema || '.sql_log ( time TIMESTAMP NOT NULL DEFAULT NOW(), row_count INTEGER, sql TEXT );' INTO STRICT sql;
100 RAISE INFO '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
102 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.config;' );
103 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.config ( key TEXT UNIQUE, value TEXT);' );
104 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''production_tables'', ''asset.call_number,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,action.circulation,action.hold_request,action.hold_notification,money.grocery,money.billing,money.cash_payment,money.forgive_payment'' );' );
105 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''country_code'', ''USA'' );' );
106 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.fields_requiring_mapping;' );
107 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.fields_requiring_mapping( table_schema TEXT, table_name TEXT, column_name TEXT, data_type TEXT);' );
108 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.base_profile_map;' );
109 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || E'.base_profile_map (
112 transcribed_perm_group TEXT,
120 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''base_profile_map'', ''base_profile_map'' );' );
121 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.base_item_dynamic_field_map;' );
122 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || E'.base_item_dynamic_field_map (
124 evergreen_field TEXT,
125 evergreen_value TEXT,
126 evergreen_datatype TEXT,
134 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);' );
135 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);' );
136 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);' );
137 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''base_item_dynamic_field_map'', ''base_item_dynamic_field_map'' );' );
138 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.base_copy_location_map;' );
139 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || E'.base_copy_location_map (
142 holdable BOOLEAN NOT NULL DEFAULT TRUE,
143 hold_verify BOOLEAN NOT NULL DEFAULT FALSE,
144 opac_visible BOOLEAN NOT NULL DEFAULT TRUE,
145 circulate BOOLEAN NOT NULL DEFAULT TRUE,
146 transcribed_location TEXT,
154 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);' );
155 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);' );
156 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);' );
157 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_base_copy_location_loc_idx ON ' || migration_schema || '.base_copy_location_map (transcribed_location);' );
158 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''base_copy_location_map'', ''base_copy_location_map'' );' );
159 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.base_circ_field_map;' );
160 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || E'.base_circ_field_map (
178 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_circ_dynamic_lf1_idx ON ' || migration_schema || '.base_circ_field_map (item_field1,item_value1);' );
179 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_circ_dynamic_lf2_idx ON ' || migration_schema || '.base_circ_field_map (item_field2,item_value2);' );
180 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_circ_dynamic_lf3_idx ON ' || migration_schema || '.base_circ_field_map (patron_field1,patron_value1);' );
181 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_circ_dynamic_lf4_idx ON ' || migration_schema || '.base_circ_field_map (patron_field2,patron_value2);' );
182 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''base_circ_field_map'', ''base_circ_field_map'' );' );
185 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_init'', now() );' );
187 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_init'';' );
190 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
192 CREATE OR REPLACE FUNCTION migration_tools.build (TEXT) RETURNS VOID AS $$
194 migration_schema ALIAS FOR $1;
195 production_tables TEXT[];
197 --RAISE INFO 'In migration_tools.build(%)', migration_schema;
198 SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
199 PERFORM migration_tools.build_base_staging_tables(migration_schema,production_tables);
200 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_barcode_key ON ' || migration_schema || '.actor_card ( barcode );' );
201 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_usrname_key ON ' || migration_schema || '.actor_usr ( usrname );' );
202 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_copy_barcode_key ON ' || migration_schema || '.asset_copy ( barcode );' );
203 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_copy_id_key ON ' || migration_schema || '.asset_copy ( id );' );
204 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_callnum_record_idx ON ' || migration_schema || '.asset_call_number ( record );' );
205 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 );' );
206 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 );' );
208 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
210 CREATE OR REPLACE FUNCTION migration_tools.build_base_staging_tables (TEXT,TEXT[]) RETURNS VOID AS $$
212 migration_schema ALIAS FOR $1;
213 production_tables ALIAS FOR $2;
215 --RAISE INFO 'In migration_tools.build_base_staging_tables(%,%)', migration_schema, production_tables;
216 FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
217 PERFORM migration_tools.build_specific_base_staging_table(migration_schema,production_tables[i]);
220 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
222 CREATE OR REPLACE FUNCTION migration_tools.build_specific_base_staging_table (TEXT,TEXT) RETURNS VOID AS $$
224 migration_schema ALIAS FOR $1;
225 production_table ALIAS FOR $2;
226 base_staging_table TEXT;
229 base_staging_table = REPLACE( production_table, '.', '_' );
230 --RAISE INFO 'In migration_tools.build_specific_base_staging_table(%,%) -> %', migration_schema, production_table, base_staging_table;
231 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.' || base_staging_table || ' ( LIKE ' || production_table || ' INCLUDING DEFAULTS EXCLUDING CONSTRAINTS );' );
232 PERFORM migration_tools.exec( $1, '
233 INSERT INTO ' || migration_schema || '.fields_requiring_mapping
234 SELECT table_schema, table_name, column_name, data_type
235 FROM information_schema.columns
236 WHERE table_schema = ''' || migration_schema || ''' AND table_name = ''' || base_staging_table || ''' AND is_nullable = ''NO'' AND column_default IS NULL;
239 SELECT table_schema, table_name, column_name, data_type
240 FROM information_schema.columns
241 WHERE table_schema = migration_schema AND table_name = base_staging_table AND is_nullable = 'NO' AND column_default IS NULL
243 PERFORM migration_tools.exec( $1, 'ALTER TABLE ' || columns.table_schema || '.' || columns.table_name || ' ALTER COLUMN ' || columns.column_name || ' DROP NOT NULL;' );
246 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
248 CREATE OR REPLACE FUNCTION migration_tools.insert_base_into_production (TEXT) RETURNS VOID AS $$
250 migration_schema ALIAS FOR $1;
251 production_tables TEXT[];
253 --RAISE INFO 'In migration_tools.insert_into_production(%)', migration_schema;
254 SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
255 FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
256 PERFORM migration_tools.insert_into_production(migration_schema,production_tables[i]);
259 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
261 CREATE OR REPLACE FUNCTION migration_tools.insert_into_production (TEXT,TEXT) RETURNS VOID AS $$
263 migration_schema ALIAS FOR $1;
264 production_table ALIAS FOR $2;
265 base_staging_table TEXT;
268 base_staging_table = REPLACE( production_table, '.', '_' );
269 --RAISE INFO 'In migration_tools.insert_into_production(%,%) -> %', migration_schema, production_table, base_staging_table;
270 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || production_table || ' SELECT * FROM ' || migration_schema || '.' || base_staging_table || ';' );
272 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
274 CREATE OR REPLACE FUNCTION migration_tools.name_parse_out_last_comma_prefix_first_middle_suffix (TEXT) RETURNS TEXT[] AS $$
276 full_name TEXT := $1;
278 family_name TEXT := '';
279 first_given_name TEXT := '';
280 second_given_name TEXT := '';
285 -- Use values, not structure, for prefix/suffix, unless we come up with a better idea
286 IF temp ilike '%MR.%' THEN
288 temp := REGEXP_REPLACE( temp, E'MR\.\\s*', '', 'i' );
290 IF temp ilike '%MRS.%' THEN
292 temp := REGEXP_REPLACE( temp, E'MRS\.\\s*', '', 'i' );
294 IF temp ilike '%MS.%' THEN
296 temp := REGEXP_REPLACE( temp, E'MS\.\\s*', '', 'i' );
298 IF temp ilike '%DR.%' THEN
300 temp := REGEXP_REPLACE( temp, E'DR\.\\s*', '', 'i' );
302 IF temp ilike '%JR%' THEN
304 temp := REGEXP_REPLACE( temp, E'JR\.?\\s*', '', 'i' );
306 IF temp ilike '%JR,%' THEN
308 temp := REGEXP_REPLACE( temp, E'JR,\\s*', ',', 'i' );
310 IF temp ilike '%SR%' THEN
312 temp := REGEXP_REPLACE( temp, E'SR\.?\\s*', '', 'i' );
314 IF temp ilike '%SR,%' THEN
316 temp := REGEXP_REPLACE( temp, E'SR,\\s*', ',', 'i' );
318 IF temp ~ E'\\sII$' THEN
320 temp := REGEXP_REPLACE( temp, E'II$', '', 'i' );
322 IF temp ~ E'\\sIII$' THEN
324 temp := REGEXP_REPLACE( temp, E'III$', '', 'i' );
326 IF temp ~ E'\\sIV$' THEN
328 temp := REGEXP_REPLACE( temp, E'IV$', '', 'i' );
331 family_name := BTRIM( REGEXP_REPLACE(temp,E'^([^,]*)\\s*,.*$',E'\\1') );
332 first_given_name := BTRIM( CASE WHEN temp ~ ',' THEN REGEXP_REPLACE(temp,E'^[^,]*\\s*,\\s*([^,\\s]*)\\s*.*$',E'\\1') ELSE 'N/A' END );
333 second_given_name := BTRIM( CASE WHEN temp ~ ',' THEN REGEXP_REPLACE(temp,E'^[^,]*\\s*,\\s*[^,\\s]*\\s*(.*)$',E'\\1') ELSE '' END );
335 RETURN ARRAY[ family_name, prefix, first_given_name, second_given_name, suffix ];
337 $$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
339 CREATE OR REPLACE FUNCTION migration_tools.address_parse_out_citystatezip (TEXT) RETURNS TEXT[] AS $$
341 city_state_zip TEXT := $1;
346 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;
347 city_state_zip := REGEXP_REPLACE( city_state_zip, E'^(.*)\\d\\d\\d\\d\\d-?\\d*(.*)$', E'\\1\\2');
348 IF city_state_zip ~ ',' THEN
349 state := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\2');
350 city := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\1');
352 IF city_state_zip ~ E'\\s+[A-Z][A-Z]\\s*' THEN
353 state := REGEXP_REPLACE( city_state_zip, E'^.*,?\\s+([A-Z][A-Z])\\s*.*$', E'\\1' );
354 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s+[A-Z][A-Z](\\s*.*)$', E'\\1\\2' );
356 IF city_state_zip ~ E'^\\S+$' THEN
357 city := city_state_zip;
360 state := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\2');
361 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\1');
365 RETURN ARRAY[ TRIM(BOTH ' ' FROM city), TRIM(BOTH ' ' FROM state), TRIM(BOTH ' ' FROM zip) ];
367 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
369 CREATE OR REPLACE FUNCTION migration_tools.rebarcode (o TEXT, t BIGINT) RETURNS TEXT AS $$
373 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
374 IF o::BIGINT < t THEN
381 $$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
383 CREATE OR REPLACE FUNCTION migration_tools.base_profile_map (TEXT) RETURNS TEXT AS $$
385 migration_schema ALIAS FOR $1;
389 EXECUTE 'SELECT ''' || migration_schema || '.'' || value FROM ' || migration_schema || '.config WHERE key = ''base_profile_map'';'
394 $$ LANGUAGE PLPGSQL STRICT STABLE;
396 CREATE OR REPLACE FUNCTION migration_tools.base_item_dynamic_field_map (TEXT) RETURNS TEXT AS $$
398 migration_schema ALIAS FOR $1;
402 EXECUTE 'SELECT ''' || migration_schema || '.'' || value FROM ' || migration_schema || '.config WHERE key = ''base_item_dynamic_field_map'';'
407 $$ LANGUAGE PLPGSQL STRICT STABLE;
409 CREATE OR REPLACE FUNCTION migration_tools.base_copy_location_map (TEXT) RETURNS TEXT AS $$
411 migration_schema ALIAS FOR $1;
415 EXECUTE 'SELECT ''' || migration_schema || '.'' || value FROM ' || migration_schema || '.config WHERE key = ''base_copy_location_map'';'
420 $$ LANGUAGE PLPGSQL STRICT STABLE;
422 CREATE OR REPLACE FUNCTION migration_tools.base_circ_field_map (TEXT) RETURNS TEXT AS $$
424 migration_schema ALIAS FOR $1;
428 EXECUTE 'SELECT ''' || migration_schema || '.'' || value FROM ' || migration_schema || '.config WHERE key = ''base_circ_field_map'';'
433 $$ LANGUAGE PLPGSQL STRICT STABLE;
435 CREATE OR REPLACE FUNCTION migration_tools.map_base_patron_profile (TEXT,TEXT,INTEGER) RETURNS VOID AS $$
437 migration_schema ALIAS FOR $1;
439 patron_table ALIAS FOR $2;
440 default_patron_profile ALIAS FOR $3;
443 sql_where1 TEXT := '';
444 sql_where2 TEXT := '';
445 sql_where3 TEXT := '';
448 SELECT migration_tools.base_profile_map(migration_schema) INTO STRICT profile_map;
450 EXECUTE 'SELECT * FROM ' || profile_map || E' ORDER BY id;'
452 sql_update := 'UPDATE ' || patron_table || ' AS u SET profile = perm_grp_id FROM ' || profile_map || ' AS m WHERE ';
453 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);
454 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);
455 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);
456 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,'') || ';';
457 --RAISE INFO 'sql = %', sql;
458 PERFORM migration_tools.exec( $1, sql );
460 PERFORM migration_tools.exec( $1, 'UPDATE ' || patron_table || ' AS u SET profile = ' || quote_literal(default_patron_profile) || ' WHERE profile IS NULL;' );
462 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_base_patron_mapping_profile'', now() );' );
464 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_base_patron_mapping_profile'';' );
467 $$ LANGUAGE PLPGSQL STRICT STABLE;
469 CREATE OR REPLACE FUNCTION migration_tools.map_base_item_table_dynamic (TEXT,TEXT) RETURNS VOID AS $$
471 migration_schema ALIAS FOR $1;
473 item_table ALIAS FOR $2;
476 sql_where1 TEXT := '';
477 sql_where2 TEXT := '';
478 sql_where3 TEXT := '';
481 SELECT migration_tools.base_item_dynamic_field_map(migration_schema) INTO STRICT field_map;
483 EXECUTE 'SELECT * FROM ' || field_map || E' ORDER BY id;'
485 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 ';
486 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);
487 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);
488 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);
489 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,'') || ';';
490 --RAISE INFO 'sql = %', sql;
491 PERFORM migration_tools.exec( $1, sql );
494 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_base_item_mapping_dynamic'', now() );' );
496 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_base_item_mapping_dynamic'';' );
499 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
501 CREATE OR REPLACE FUNCTION migration_tools.map_base_item_table_locations (TEXT,TEXT) RETURNS VOID AS $$
503 migration_schema ALIAS FOR $1;
504 base_copy_location_map TEXT;
505 item_table ALIAS FOR $2;
508 sql_where1 TEXT := '';
509 sql_where2 TEXT := '';
510 sql_where3 TEXT := '';
513 SELECT migration_tools.base_copy_location_map(migration_schema) INTO STRICT base_copy_location_map;
515 EXECUTE 'SELECT * FROM ' || base_copy_location_map || E' ORDER BY id;'
517 sql_update := 'UPDATE ' || item_table || ' AS i SET location = m.location FROM ' || base_copy_location_map || ' AS m WHERE ';
518 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);
519 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);
520 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);
521 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,'') || ';';
522 --RAISE INFO 'sql = %', sql;
523 PERFORM migration_tools.exec( $1, sql );
526 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_base_item_mapping_locations'', now() );' );
528 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_base_item_mapping_locations'';' );
531 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
533 -- 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
534 CREATE OR REPLACE FUNCTION migration_tools.map_base_circ_table_dynamic (TEXT,TEXT,TEXT,TEXT) RETURNS VOID AS $$
536 migration_schema ALIAS FOR $1;
538 circ_table ALIAS FOR $2;
539 item_table ALIAS FOR $3;
540 patron_table ALIAS FOR $4;
543 sql_where1 TEXT := '';
544 sql_where2 TEXT := '';
545 sql_where3 TEXT := '';
546 sql_where4 TEXT := '';
549 SELECT migration_tools.base_circ_field_map(migration_schema) INTO STRICT field_map;
551 EXECUTE 'SELECT * FROM ' || field_map || E' ORDER BY id;'
553 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 ';
554 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);
555 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);
556 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);
557 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);
558 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,'') || ';';
559 --RAISE INFO 'sql = %', sql;
560 PERFORM migration_tools.exec( $1, sql );
563 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_base_circ_field_mapping'', now() );' );
565 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_base_circ_field_mapping'';' );
568 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
571 -- $barcode source barcode
572 -- $prefix prefix to add to barcode, NULL = add no prefix
573 -- $maxlen maximum length of barcode; default to 14 if left NULL
574 -- $pad padding string to apply to left of source barcode before adding
575 -- prefix and suffix; set to NULL or '' if no padding is desired
576 -- $suffix suffix to add to barcode, NULL = add no suffix
578 -- Returns a new string consisting of prefix concatenated with padded barcode and suffix.
579 -- If new barcode would be longer than $maxlen, the original barcode is returned instead.
581 CREATE OR REPLACE FUNCTION migration_tools.expand_barcode (TEXT, TEXT, INTEGER, TEXT, TEXT) RETURNS TEXT AS $$
582 my ($barcode, $prefix, $maxlen, $pad, $suffix) = @_;
585 return unless defined $barcode;
587 $prefix = '' unless defined $prefix;
589 $pad = '0' unless defined $pad;
590 $suffix = '' unless defined $suffix;
592 # bail out if adding prefix and suffix would bring new barcode over max length
593 return $barcode if (length($prefix) + length($barcode) + length($suffix)) > $maxlen;
595 my $new_barcode = $barcode;
597 my $pad_length = $maxlen - length($prefix) - length($suffix);
598 if (length($barcode) < $pad_length) {
599 # assuming we always want padding on the left
600 # also assuming that it is possible to have the pad string be longer than 1 character
601 $new_barcode = substr($pad x ($pad_length - length($barcode)), 0, $pad_length - length($barcode)) . $new_barcode;
605 # bail out if adding prefix and suffix would bring new barcode over max length
606 return $barcode if (length($prefix) + length($new_barcode) + length($suffix)) > $maxlen;
608 return "$prefix$new_barcode$suffix";
609 $$ LANGUAGE PLPERL STABLE;
611 CREATE OR REPLACE FUNCTION migration_tools.attempt_cast (TEXT,TEXT,TEXT) RETURNS RECORD AS $$
613 attempt_value ALIAS FOR $1;
614 datatype ALIAS FOR $2;
615 fail_value ALIAS FOR $3;
619 EXECUTE 'SELECT ' || quote_literal(attempt_value) || '::' || datatype || ' AS a;'
626 EXECUTE 'SELECT ' || quote_literal(fail_value) || '::' || datatype || ' AS a;'
631 $$ LANGUAGE PLPGSQL STRICT STABLE;
633 CREATE OR REPLACE FUNCTION migration_tools.attempt_date (TEXT,TEXT) RETURNS DATE AS $$
635 attempt_value ALIAS FOR $1;
636 fail_value ALIAS FOR $2;
640 EXECUTE 'SELECT ' || quote_literal(attempt_value) || '::date AS a;'
647 EXECUTE 'SELECT ' || quote_literal(fail_value) || '::date AS a;'
652 $$ LANGUAGE PLPGSQL STRICT STABLE;
654 CREATE OR REPLACE FUNCTION migration_tools.attempt_timestamptz (TEXT,TEXT) RETURNS TIMESTAMPTZ AS $$
656 attempt_value ALIAS FOR $1;
657 fail_value ALIAS FOR $2;
661 EXECUTE 'SELECT ' || quote_literal(attempt_value) || '::TIMESTAMPTZ AS a;'
668 EXECUTE 'SELECT ' || quote_literal(fail_value) || '::TIMESTAMPTZ AS a;'
673 $$ LANGUAGE PLPGSQL STRICT STABLE;
675 CREATE OR REPLACE FUNCTION migration_tools.attempt_money (TEXT,TEXT) RETURNS NUMERIC(8,2) AS $$
677 attempt_value ALIAS FOR $1;
678 fail_value ALIAS FOR $2;
682 EXECUTE 'SELECT ' || quote_literal(attempt_value) || '::NUMERIC(8,2) AS a;'
689 EXECUTE 'SELECT ' || quote_literal(fail_value) || '::NUMERIC(8,2) AS a;'
694 $$ LANGUAGE PLPGSQL STRICT STABLE;
696 -- add_codabar_checkdigit
697 -- $barcode source barcode
699 -- If the source string is 13 or 14 characters long and contains only digits, adds or replaces the 14
700 -- character with a checkdigit computed according to the usual algorithm for library barcodes
701 -- using the Codabar symbology - see <http://www.makebarcode.com/specs/codabar.html>. If the
702 -- input string does not meet those requirements, it is returned unchanged.
704 CREATE OR REPLACE FUNCTION migration_tools.add_codabar_checkdigit (TEXT) RETURNS TEXT AS $$
707 return $barcode if $barcode !~ /^\d{13,14}$/;
708 $barcode = substr($barcode, 0, 13); # ignore 14th digit
709 my @digits = split //, $barcode;
711 $total += $digits[$_] foreach (1, 3, 5, 7, 9, 11);
712 $total += (2 * $digits[$_] >= 10) ? (2 * $digits[$_] - 9) : (2 * $digits[$_]) foreach (0, 2, 4, 6, 8, 10, 12);
713 my $remainder = $total % 10;
714 my $checkdigit = ($remainder == 0) ? $remainder : 10 - $remainder;
715 return $barcode . $checkdigit;
716 $$ LANGUAGE PLPERL STRICT STABLE;
718 CREATE OR REPLACE FUNCTION migration_tools.attempt_phone (TEXT,TEXT) RETURNS TEXT AS $$
724 n_digits INTEGER := 0;
727 temp := REGEXP_REPLACE(temp, '^1*[^0-9]*(?=[0-9])', '');
728 temp := REGEXP_REPLACE(temp, '[^0-9]*([0-9]{3})[^0-9]*([0-9]{3})[^0-9]*([0-9]{4})', E'\\1-\\2-\\3');
729 n_digits := LENGTH(REGEXP_REPLACE(REGEXP_REPLACE(temp, '(.*)?[a-zA-Z].*', E'\\1') , '[^0-9]', '', 'g'));
730 IF n_digits = 7 AND areacode <> '' THEN
731 temp := REGEXP_REPLACE(temp, '[^0-9]*([0-9]{3})[^0-9]*([0-9]{4})', E'\\1-\\2');
732 output := (areacode || '-' || temp);
739 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
741 CREATE OR REPLACE FUNCTION migration_tools.set_leader (TEXT, INT, TEXT) RETURNS TEXT AS $$
742 my ($marcxml, $pos, $value) = @_;
749 my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8');
750 my $leader = $marc->leader();
751 substr($leader, $pos, 1) = $value;
752 $marc->leader($leader);
753 $xml = $marc->as_xml_record;
754 $xml =~ s/^<\?.+?\?>$//mo;
756 $xml =~ s/>\s+</></sgo;
759 $$ LANGUAGE PLPERLU STABLE;
761 CREATE OR REPLACE FUNCTION migration_tools.set_008 (TEXT, INT, TEXT) RETURNS TEXT AS $$
762 my ($marcxml, $pos, $value) = @_;
769 my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8');
770 my $f008 = $marc->field('008');
773 my $field = $f008->data();
774 substr($field, $pos, 1) = $value;
775 $f008->update($field);
776 $xml = $marc->as_xml_record;
777 $xml =~ s/^<\?.+?\?>$//mo;
779 $xml =~ s/>\s+</></sgo;
783 $$ LANGUAGE PLPERLU STABLE;
786 CREATE OR REPLACE FUNCTION migration_tools.is_staff_profile (INT) RETURNS BOOLEAN AS $$
788 profile ALIAS FOR $1;
790 RETURN CASE WHEN 'Staff' IN (select (permission.grp_ancestors(profile)).name) THEN TRUE ELSE FALSE END;
792 $$ LANGUAGE PLPGSQL STRICT STABLE;
795 CREATE OR REPLACE FUNCTION migration_tools.is_blank (TEXT) RETURNS BOOLEAN AS $$
797 RETURN CASE WHEN $1 = '' THEN TRUE ELSE FALSE END;
799 $$ LANGUAGE PLPGSQL STRICT STABLE;
802 CREATE OR REPLACE FUNCTION migration_tools.insert_tags (TEXT, TEXT) RETURNS TEXT AS $$
804 my ($marcxml, $tags) = @_;
812 my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8');
813 my $to_insert = MARC::Record->new_from_xml("<record>$tags</record>", 'UTF-8');
817 foreach my $field ( $marc->fields() ) {
818 push @incumbents, $field->as_formatted();
821 foreach $field ( $to_insert->fields() ) {
822 if (!grep {$_ eq $field->as_formatted()} @incumbents) {
823 $marc->insert_fields_ordered( ($field) );
827 $xml = $marc->as_xml_record;
828 $xml =~ s/^<\?.+?\?>$//mo;
830 $xml =~ s/>\s+</></sgo;
835 $$ LANGUAGE PLPERLU STABLE;
837 CREATE OR REPLACE FUNCTION migration_tools.apply_circ_matrix( tablename TEXT ) RETURNS VOID AS $$
841 -- First make sure the circ matrix is loaded and the circulations
842 -- have been staged to the extent possible (but at the very least
843 -- circ_lib, target_copy, usr, and *_renewal). User profiles and
844 -- circ modifiers must also be in place.
846 -- SELECT migration_tools.apply_circ_matrix('m_pioneer.action_circulation');
854 this_duration_rule INT;
856 this_max_fine_rule INT;
857 rcd config.rule_circ_duration%ROWTYPE;
858 rrf config.rule_recurring_fine%ROWTYPE;
859 rmf config.rule_max_fine%ROWTYPE;
866 EXECUTE 'SELECT COUNT(*) FROM ' || tablename || ';' INTO n_circs;
868 FOR circ IN EXECUTE ('SELECT id FROM ' || tablename) LOOP
870 -- Fetch the correct rules for this circulation
877 WHEN phone_renewal OR desk_renewal OR opac_renewal THEN TRUE
880 FROM ' || tablename || ' WHERE id = ' || circ || ';')
881 INTO circ_lib, target_copy, usr, is_renewal ;
883 INTO this_duration_rule,
889 FROM action.find_circ_matrix_matchpoint(
895 SELECT INTO rcd * FROM config.rule_circ_duration
896 WHERE id = this_duration_rule;
897 SELECT INTO rrf * FROM config.rule_recurring_fine
898 WHERE id = this_fine_rule;
899 SELECT INTO rmf * FROM config.rule_max_fine
900 WHERE id = this_max_fine_rule;
902 -- Apply the rules to this circulation
903 EXECUTE ('UPDATE ' || tablename || ' c
905 duration_rule = rcd.name,
906 recurring_fine_rule = rrf.name,
907 max_fine_rule = rmf.name,
908 duration = rcd.normal,
909 recurring_fine = rrf.normal,
912 WHEN TRUE THEN (rmf.amount / 100.0) * ac.price
915 renewal_remaining = rcd.max_renewals
917 config.rule_circ_duration rcd,
918 config.rule_recurring_fine rrf,
919 config.rule_max_fine rmf,
922 rcd.id = ' || this_duration_rule || ' AND
923 rrf.id = ' || this_fine_rule || ' AND
924 rmf.id = ' || this_max_fine_rule || ' AND
925 ac.id = c.target_copy AND
926 c.id = ' || circ || ';');
928 -- Keep track of where we are in the process
930 IF (n % 100 = 0) THEN
931 RAISE INFO '%', n || ' of ' || n_circs
932 || ' (' || (100*n/n_circs) || '%) circs updated.';
942 CREATE OR REPLACE FUNCTION migration_tools.stage_not_applicable_asset_stat_cats( schemaname TEXT ) RETURNS VOID AS $$
944 -- USAGE: Make sure the stat_cat and stat_cat_entry tables are populated, including exactly one 'Not Applicable' entry per stat cat.
945 -- Then SELECT migration_tools.stage_not_applicable_asset_stat_cats('m_foo');
947 -- TODO: Make a variant that will go directly to production tables -- which would be useful for retrofixing the absence of N/A cats.
948 -- TODO: Add a similar tool for actor stat cats, which behave differently.
951 c TEXT := schemaname || '.asset_copy_legacy';
952 sc TEXT := schemaname || '.asset_stat_cat';
953 sce TEXT := schemaname || '.asset_stat_cat_entry';
954 scecm TEXT := schemaname || '.asset_stat_cat_entry_copy_map';
960 FOR stat_cat IN EXECUTE ('SELECT id FROM ' || sc) LOOP
962 EXECUTE ('SELECT id FROM ' || sce || ' WHERE stat_cat = ' || stat_cat || E' AND value = \'Not Applicable\';') INTO stat_cat_entry;
964 EXECUTE ('INSERT INTO ' || scecm || ' (owning_copy, stat_cat, stat_cat_entry)
965 SELECT c.id, ' || stat_cat || ', ' || stat_cat_entry || ' FROM ' || c || ' c WHERE c.id NOT IN
966 (SELECT owning_copy FROM ' || scecm || ' WHERE stat_cat = ' || stat_cat || ');');
975 CREATE OR REPLACE FUNCTION migration_tools.assign_standing_penalties ( ) RETURNS VOID AS $$
977 -- USAGE: Once circulation data has been loaded, and group penalty thresholds have been set up, run this.
978 -- This will assign standing penalties as needed.
986 FOR org_unit IN EXECUTE ('SELECT DISTINCT org_unit FROM permission.grp_penalty_threshold;') LOOP
988 FOR usr IN EXECUTE ('SELECT id FROM actor.usr WHERE NOT deleted;') LOOP
990 EXECUTE('SELECT actor.calculate_system_penalties(' || usr || ', ' || org_unit || ');');
1000 $$ LANGUAGE plpgsql;
1003 CREATE OR REPLACE FUNCTION migration_tools.insert_metarecords_for_pristine_database () RETURNS VOID AS $$
1006 INSERT INTO metabib.metarecord (fingerprint, master_record)
1007 SELECT DISTINCT ON (b.fingerprint) b.fingerprint, b.id
1008 FROM biblio.record_entry b
1010 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)
1011 AND NOT EXISTS ( SELECT 1 FROM metabib.metarecord WHERE fingerprint = b.fingerprint )
1012 ORDER BY b.fingerprint, b.quality DESC;
1013 INSERT INTO metabib.metarecord_source_map (metarecord, source)
1015 FROM biblio.record_entry r
1016 JOIN metabib.metarecord m USING (fingerprint)
1017 WHERE NOT r.deleted;
1020 $$ LANGUAGE plpgsql;
1023 CREATE OR REPLACE FUNCTION migration_tools.insert_metarecords_for_incumbent_database () RETURNS VOID AS $$
1026 INSERT INTO metabib.metarecord (fingerprint, master_record)
1027 SELECT DISTINCT ON (b.fingerprint) b.fingerprint, b.id
1028 FROM biblio.record_entry b
1030 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)
1031 AND NOT EXISTS ( SELECT 1 FROM metabib.metarecord WHERE fingerprint = b.fingerprint )
1032 ORDER BY b.fingerprint, b.quality DESC;
1033 INSERT INTO metabib.metarecord_source_map (metarecord, source)
1035 FROM biblio.record_entry r
1036 JOIN metabib.metarecord m USING (fingerprint)
1038 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);
1041 $$ LANGUAGE plpgsql;
1044 CREATE OR REPLACE FUNCTION migration_tools.create_cards( schemaname TEXT ) RETURNS VOID AS $$
1046 -- USAGE: Make sure the patrons are staged in schemaname.actor_usr_legacy and have 'usrname' assigned.
1047 -- Then SELECT migration_tools.create_cards('m_foo');
1050 u TEXT := schemaname || '.actor_usr_legacy';
1051 c TEXT := schemaname || '.actor_card';
1055 EXECUTE ('TRUNCATE ' || c || ';');
1056 EXECUTE ('INSERT INTO ' || c || ' (usr, barcode) SELECT id, usrname FROM ' || u || ';');
1057 EXECUTE ('UPDATE ' || u || ' u SET card = c.id FROM ' || c || ' c WHERE c.usr = u.id;');
1063 $$ LANGUAGE plpgsql;
1066 CREATE OR REPLACE FUNCTION migration_tools.insert_856_9 (TEXT, TEXT) RETURNS TEXT AS $$
1068 ## USAGE: UPDATE biblio.record_entry SET marc = migration_tools.insert_856_9(marc, 'ABC') WHERE [...];
1070 my ($marcxml, $shortname) = @_;
1073 use MARC::File::XML;
1078 my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8');
1080 foreach my $field ( $marc->field('856') ) {
1081 if ( scalar(grep( /(netlibrary|overdrive)\.com/i, $field->subfield('u'))) > 0 &&
1082 ! ( $field->as_string('9') =~ m/$shortname/ ) ) {
1083 $field->add_subfields( '9' => $shortname );
1084 $field->update( ind2 => '0');
1088 $xml = $marc->as_xml_record;
1089 $xml =~ s/^<\?.+?\?>$//mo;
1091 $xml =~ s/>\s+</></sgo;
1096 $$ LANGUAGE PLPERLU STABLE;
1098 CREATE OR REPLACE FUNCTION migration_tools.change_call_number(copy_id BIGINT, new_label TEXT) RETURNS VOID AS $$
1110 -- Bail out if asked to change the label to ##URI##
1111 IF new_label = '##URI##' THEN
1115 -- Gather information
1116 SELECT call_number INTO old_volume FROM asset.copy WHERE id = copy_id;
1117 SELECT record INTO bib FROM asset.call_number WHERE id = old_volume;
1118 SELECT owning_lib, label INTO owner, old_label FROM asset.call_number WHERE id = old_volume;
1120 -- Bail out if the label already is ##URI##
1121 IF old_label = '##URI##' THEN
1125 -- Bail out if the call number label is already correct
1126 IF new_volume = old_volume THEN
1130 -- Check whether we already have a destination volume available
1131 SELECT id INTO new_volume FROM asset.call_number
1134 owning_lib = owner AND
1135 label = new_label AND
1138 -- Create destination volume if needed
1140 INSERT INTO asset.call_number (creator, editor, record, owning_lib, label)
1141 VALUES (1, 1, bib, owner, new_label);
1142 SELECT id INTO new_volume FROM asset.call_number
1145 owning_lib = owner AND
1146 label = new_label AND
1150 -- Move copy to destination
1151 UPDATE asset.copy SET call_number = new_volume WHERE id = copy_id;
1153 -- Delete source volume if it is now empty
1154 SELECT id INTO remainder FROM asset.copy WHERE call_number = old_volume AND NOT deleted;
1156 DELETE FROM asset.call_number WHERE id = old_volume;
1161 $$ LANGUAGE plpgsql;
1163 CREATE OR REPLACE FUNCTION migration_tools.zip_to_city_state_county (TEXT) RETURNS TEXT[] AS $$
1168 open (FH, '<', '/openils/var/data/zips.txt') or return ('No File Found', 'No File Found', 'No File Found');
1172 my ($junk, $state, $city, $zip, $foo, $bar, $county, $baz, $morejunk) = split(/\|/);
1173 $zipdata{$zip} = [$city, $state, $county];
1176 if (defined $zipdata{$input}) {
1177 my ($city, $state, $county) = @{$zipdata{$input}};
1178 return [$city, $state, $county];
1179 } elsif (defined $zipdata{substr $input, 0, 5}) {
1180 my ($city, $state, $county) = @{$zipdata{substr $input, 0, 5}};
1181 return [$city, $state, $county];
1183 return ['ZIP not found', 'ZIP not found', 'ZIP not found'];
1186 $$ LANGUAGE PLPERLU STABLE;
1189 CREATE OR REPLACE FUNCTION migration_tools.check_ou_depth ( ) RETURNS VOID AS $$
1196 errors_found BOOLEAN;
1198 parent_shortname TEXT;
1204 errors_found := FALSE;
1206 FOR ou IN EXECUTE ('SELECT DISTINCT id FROM actor.org_unit ORDER BY 1;') LOOP
1208 SELECT parent_ou FROM actor.org_unit WHERE id = ou INTO ou_parent;
1209 SELECT depth FROM actor.org_unit_type WHERE id = ou INTO org_unit_depth;
1210 SELECT depth FROM actor.org_unit_type WHERE id = ou_parent INTO parent_depth;
1212 IF ou_parent IS NOT NULL AND org_unit_depth - parent_depth <> 1 THEN
1213 SELECT shortname FROM actor.org_unit WHERE id = ou INTO ou_shortname;
1214 SELECT shortname FROM actor.org_unit WHERE id = ou_parent INTO parent_shortname;
1215 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;
1216 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;
1217 RAISE INFO '% (org unit %) is a % (depth %) but its parent, % (org unit %), is a % (depth %).',
1218 ou_shortname, ou, ou_type_name, org_unit_depth, parent_shortname, ou_parent, parent_type, parent_depth;
1219 errors_found := TRUE;
1224 IF NOT errors_found THEN
1225 RAISE INFO 'No errors found.';
1232 $$ LANGUAGE plpgsql;