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,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'' );' );
122 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_init'', now() );' );
124 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_init'';' );
127 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
129 CREATE OR REPLACE FUNCTION migration_tools.build (TEXT) RETURNS VOID AS $$
131 migration_schema ALIAS FOR $1;
132 production_tables TEXT[];
134 --RAISE INFO 'In migration_tools.build(%)', migration_schema;
135 SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
136 PERFORM migration_tools.build_base_staging_tables(migration_schema,production_tables);
137 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_barcode_key ON ' || migration_schema || '.actor_card ( barcode );' );
138 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_usrname_key ON ' || migration_schema || '.actor_usr ( usrname );' );
139 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_copy_barcode_key ON ' || migration_schema || '.asset_copy ( barcode );' );
140 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_callnum_record_idx ON ' || migration_schema || '.asset_call_number ( record );' );
141 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 );' );
142 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 );' );
144 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
146 CREATE OR REPLACE FUNCTION migration_tools.build_base_staging_tables (TEXT,TEXT[]) RETURNS VOID AS $$
148 migration_schema ALIAS FOR $1;
149 production_tables ALIAS FOR $2;
151 --RAISE INFO 'In migration_tools.build_base_staging_tables(%,%)', migration_schema, production_tables;
152 FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
153 PERFORM migration_tools.build_specific_base_staging_table(migration_schema,production_tables[i]);
156 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
158 CREATE OR REPLACE FUNCTION migration_tools.build_specific_base_staging_table (TEXT,TEXT) RETURNS VOID AS $$
160 migration_schema ALIAS FOR $1;
161 production_table ALIAS FOR $2;
162 base_staging_table TEXT;
165 base_staging_table = REPLACE( production_table, '.', '_' );
166 --RAISE INFO 'In migration_tools.build_specific_base_staging_table(%,%) -> %', migration_schema, production_table, base_staging_table;
167 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.' || base_staging_table || ' ( LIKE ' || production_table || ' INCLUDING DEFAULTS EXCLUDING CONSTRAINTS );' );
168 PERFORM migration_tools.exec( $1, '
169 INSERT INTO ' || migration_schema || '.fields_requiring_mapping
170 SELECT table_schema, table_name, column_name, data_type
171 FROM information_schema.columns
172 WHERE table_schema = ''' || migration_schema || ''' AND table_name = ''' || base_staging_table || ''' AND is_nullable = ''NO'' AND column_default IS NULL;
175 SELECT table_schema, table_name, column_name, data_type
176 FROM information_schema.columns
177 WHERE table_schema = migration_schema AND table_name = base_staging_table AND is_nullable = 'NO' AND column_default IS NULL
179 PERFORM migration_tools.exec( $1, 'ALTER TABLE ' || columns.table_schema || '.' || columns.table_name || ' ALTER COLUMN ' || columns.column_name || ' DROP NOT NULL;' );
182 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
184 CREATE OR REPLACE FUNCTION migration_tools.insert_base_into_production (TEXT) RETURNS VOID AS $$
186 migration_schema ALIAS FOR $1;
187 production_tables TEXT[];
189 --RAISE INFO 'In migration_tools.insert_into_production(%)', migration_schema;
190 SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
191 FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
192 PERFORM migration_tools.insert_into_production(migration_schema,production_tables[i]);
195 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
197 CREATE OR REPLACE FUNCTION migration_tools.insert_into_production (TEXT,TEXT) RETURNS VOID AS $$
199 migration_schema ALIAS FOR $1;
200 production_table ALIAS FOR $2;
201 base_staging_table TEXT;
204 base_staging_table = REPLACE( production_table, '.', '_' );
205 --RAISE INFO 'In migration_tools.insert_into_production(%,%) -> %', migration_schema, production_table, base_staging_table;
206 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || production_table || ' SELECT * FROM ' || migration_schema || '.' || base_staging_table || ';' );
208 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
210 CREATE OR REPLACE FUNCTION migration_tools.name_parse_out_last_comma_prefix_first_middle_suffix (TEXT) RETURNS TEXT[] AS $$
212 full_name TEXT := $1;
214 family_name TEXT := '';
215 first_given_name TEXT := '';
216 second_given_name TEXT := '';
221 -- Use values, not structure, for prefix/suffix, unless we come up with a better idea
222 IF temp ilike '%MR.%' THEN
224 temp := REGEXP_REPLACE( temp, E'MR\.\\s*', '', 'i' );
226 IF temp ilike '%MRS.%' THEN
228 temp := REGEXP_REPLACE( temp, E'MRS\.\\s*', '', 'i' );
230 IF temp ilike '%MS.%' THEN
232 temp := REGEXP_REPLACE( temp, E'MS\.\\s*', '', 'i' );
234 IF temp ilike '%DR.%' THEN
236 temp := REGEXP_REPLACE( temp, E'DR\.\\s*', '', 'i' );
238 IF temp ilike '%JR%' THEN
240 temp := REGEXP_REPLACE( temp, E'JR\.?\\s*', '', 'i' );
242 IF temp ilike '%SR%' THEN
244 temp := REGEXP_REPLACE( temp, E'SR\.?\\s*', '', 'i' );
246 IF temp ~ E'\\sII$' THEN
248 temp := REGEXP_REPLACE( temp, E'II$', '', 'i' );
250 IF temp ~ E'\\sIII$' THEN
252 temp := REGEXP_REPLACE( temp, E'III$', '', 'i' );
255 family_name := BTRIM( REGEXP_REPLACE(temp,E'^([^,]*)\\s*,.*$',E'\\1') );
256 first_given_name := BTRIM( CASE WHEN temp ~ ',' THEN REGEXP_REPLACE(temp,E'^[^,]*\\s*,\\s*([^,\\s]*)\\s*.*$',E'\\1') ELSE 'N/A' END );
257 second_given_name := BTRIM( CASE WHEN temp ~ ',' THEN REGEXP_REPLACE(temp,E'^[^,]*\\s*,\\s*[^,\\s]*\\s*(.*)$',E'\\1') ELSE '' END );
259 RETURN ARRAY[ family_name, prefix, first_given_name, second_given_name, suffix ];
261 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
263 CREATE OR REPLACE FUNCTION migration_tools.address_parse_out_citystatezip (TEXT) RETURNS TEXT[] AS $$
265 city_state_zip TEXT := $1;
270 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;
271 city_state_zip := REGEXP_REPLACE( city_state_zip, E'^(.*)\\d\\d\\d\\d\\d-?\\d*(.*)$', E'\\1\\2');
272 IF city_state_zip ~ ',' THEN
273 state := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\2');
274 city := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\1');
276 IF city_state_zip ~ E'\\s+[A-Z][A-Z]\\s*' THEN
277 state := REGEXP_REPLACE( city_state_zip, E'^.*,?\\s+([A-Z][A-Z])\\s*.*$', E'\\1' );
278 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s+[A-Z][A-Z](\\s*.*)$', E'\\1\\2' );
280 IF city_state_zip ~ E'^\\S+$' THEN
281 city := city_state_zip;
284 state := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\2');
285 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\1');
289 RETURN ARRAY[ TRIM(BOTH ' ' FROM city), TRIM(BOTH ' ' FROM state), TRIM(BOTH ' ' FROM zip) ];
291 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
293 CREATE OR REPLACE FUNCTION migration_tools.rebarcode (o TEXT, t BIGINT) RETURNS TEXT AS $$
297 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
298 IF o::BIGINT < t THEN
305 $$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
307 CREATE OR REPLACE FUNCTION migration_tools.base_profile_map (TEXT) RETURNS TEXT AS $$
309 migration_schema ALIAS FOR $1;
313 EXECUTE 'SELECT ''' || migration_schema || '.'' || value FROM ' || migration_schema || '.config WHERE key = ''base_profile_map'';'
318 $$ LANGUAGE PLPGSQL STRICT STABLE;
320 CREATE OR REPLACE FUNCTION migration_tools.map_base_patron_profile (TEXT,TEXT,INTEGER) RETURNS VOID AS $$
322 migration_schema ALIAS FOR $1;
324 patron_table ALIAS FOR $2;
325 default_patron_profile ALIAS FOR $3;
328 sql_where1 TEXT := '';
329 sql_where2 TEXT := '';
330 sql_where3 TEXT := '';
333 SELECT migration_tools.base_profile_map(migration_schema) INTO STRICT profile_map;
335 EXECUTE 'SELECT * FROM ' || profile_map || E' ORDER BY id;'
337 sql_update := 'UPDATE ' || patron_table || ' AS u SET profile = perm_grp_id FROM ' || profile_map || ' AS m WHERE ';
338 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);
339 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);
340 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);
341 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,'') || ';';
342 --RAISE INFO 'sql = %', sql;
343 PERFORM migration_tools.exec( $1, sql );
345 PERFORM migration_tools.exec( $1, 'UPDATE ' || patron_table || ' AS u SET profile = ' || quote_literal(default_patron_profile) || ' WHERE profile IS NULL;' );
347 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_base_patron_mapping_profile'', now() );' );
349 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_base_patron_mapping_profile'';' );
352 $$ LANGUAGE PLPGSQL STRICT STABLE;