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'' );' );
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'' );' );
161 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_init'', now() );' );
163 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_init'';' );
166 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
168 CREATE OR REPLACE FUNCTION migration_tools.build (TEXT) RETURNS VOID AS $$
170 migration_schema ALIAS FOR $1;
171 production_tables TEXT[];
173 --RAISE INFO 'In migration_tools.build(%)', migration_schema;
174 SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
175 PERFORM migration_tools.build_base_staging_tables(migration_schema,production_tables);
176 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_barcode_key ON ' || migration_schema || '.actor_card ( barcode );' );
177 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_usrname_key ON ' || migration_schema || '.actor_usr ( usrname );' );
178 PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_copy_barcode_key ON ' || migration_schema || '.asset_copy ( barcode );' );
179 PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_callnum_record_idx ON ' || migration_schema || '.asset_call_number ( record );' );
180 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 );' );
181 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 );' );
183 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
185 CREATE OR REPLACE FUNCTION migration_tools.build_base_staging_tables (TEXT,TEXT[]) RETURNS VOID AS $$
187 migration_schema ALIAS FOR $1;
188 production_tables ALIAS FOR $2;
190 --RAISE INFO 'In migration_tools.build_base_staging_tables(%,%)', migration_schema, production_tables;
191 FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
192 PERFORM migration_tools.build_specific_base_staging_table(migration_schema,production_tables[i]);
195 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
197 CREATE OR REPLACE FUNCTION migration_tools.build_specific_base_staging_table (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.build_specific_base_staging_table(%,%) -> %', migration_schema, production_table, base_staging_table;
206 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.' || base_staging_table || ' ( LIKE ' || production_table || ' INCLUDING DEFAULTS EXCLUDING CONSTRAINTS );' );
207 PERFORM migration_tools.exec( $1, '
208 INSERT INTO ' || migration_schema || '.fields_requiring_mapping
209 SELECT table_schema, table_name, column_name, data_type
210 FROM information_schema.columns
211 WHERE table_schema = ''' || migration_schema || ''' AND table_name = ''' || base_staging_table || ''' AND is_nullable = ''NO'' AND column_default IS NULL;
214 SELECT table_schema, table_name, column_name, data_type
215 FROM information_schema.columns
216 WHERE table_schema = migration_schema AND table_name = base_staging_table AND is_nullable = 'NO' AND column_default IS NULL
218 PERFORM migration_tools.exec( $1, 'ALTER TABLE ' || columns.table_schema || '.' || columns.table_name || ' ALTER COLUMN ' || columns.column_name || ' DROP NOT NULL;' );
221 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
223 CREATE OR REPLACE FUNCTION migration_tools.insert_base_into_production (TEXT) RETURNS VOID AS $$
225 migration_schema ALIAS FOR $1;
226 production_tables TEXT[];
228 --RAISE INFO 'In migration_tools.insert_into_production(%)', migration_schema;
229 SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
230 FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
231 PERFORM migration_tools.insert_into_production(migration_schema,production_tables[i]);
234 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
236 CREATE OR REPLACE FUNCTION migration_tools.insert_into_production (TEXT,TEXT) RETURNS VOID AS $$
238 migration_schema ALIAS FOR $1;
239 production_table ALIAS FOR $2;
240 base_staging_table TEXT;
243 base_staging_table = REPLACE( production_table, '.', '_' );
244 --RAISE INFO 'In migration_tools.insert_into_production(%,%) -> %', migration_schema, production_table, base_staging_table;
245 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || production_table || ' SELECT * FROM ' || migration_schema || '.' || base_staging_table || ';' );
247 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
249 CREATE OR REPLACE FUNCTION migration_tools.name_parse_out_last_comma_prefix_first_middle_suffix (TEXT) RETURNS TEXT[] AS $$
251 full_name TEXT := $1;
253 family_name TEXT := '';
254 first_given_name TEXT := '';
255 second_given_name TEXT := '';
260 -- Use values, not structure, for prefix/suffix, unless we come up with a better idea
261 IF temp ilike '%MR.%' THEN
263 temp := REGEXP_REPLACE( temp, E'MR\.\\s*', '', 'i' );
265 IF temp ilike '%MRS.%' THEN
267 temp := REGEXP_REPLACE( temp, E'MRS\.\\s*', '', 'i' );
269 IF temp ilike '%MS.%' THEN
271 temp := REGEXP_REPLACE( temp, E'MS\.\\s*', '', 'i' );
273 IF temp ilike '%DR.%' THEN
275 temp := REGEXP_REPLACE( temp, E'DR\.\\s*', '', 'i' );
277 IF temp ilike '%JR%' THEN
279 temp := REGEXP_REPLACE( temp, E'JR\.?\\s*', '', 'i' );
281 IF temp ilike '%SR%' THEN
283 temp := REGEXP_REPLACE( temp, E'SR\.?\\s*', '', 'i' );
285 IF temp ~ E'\\sII$' THEN
287 temp := REGEXP_REPLACE( temp, E'II$', '', 'i' );
289 IF temp ~ E'\\sIII$' THEN
291 temp := REGEXP_REPLACE( temp, E'III$', '', 'i' );
294 family_name := BTRIM( REGEXP_REPLACE(temp,E'^([^,]*)\\s*,.*$',E'\\1') );
295 first_given_name := BTRIM( CASE WHEN temp ~ ',' THEN REGEXP_REPLACE(temp,E'^[^,]*\\s*,\\s*([^,\\s]*)\\s*.*$',E'\\1') ELSE 'N/A' END );
296 second_given_name := BTRIM( CASE WHEN temp ~ ',' THEN REGEXP_REPLACE(temp,E'^[^,]*\\s*,\\s*[^,\\s]*\\s*(.*)$',E'\\1') ELSE '' END );
298 RETURN ARRAY[ family_name, prefix, first_given_name, second_given_name, suffix ];
300 $$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
302 CREATE OR REPLACE FUNCTION migration_tools.address_parse_out_citystatezip (TEXT) RETURNS TEXT[] AS $$
304 city_state_zip TEXT := $1;
309 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;
310 city_state_zip := REGEXP_REPLACE( city_state_zip, E'^(.*)\\d\\d\\d\\d\\d-?\\d*(.*)$', E'\\1\\2');
311 IF city_state_zip ~ ',' THEN
312 state := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\2');
313 city := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\1');
315 IF city_state_zip ~ E'\\s+[A-Z][A-Z]\\s*' THEN
316 state := REGEXP_REPLACE( city_state_zip, E'^.*,?\\s+([A-Z][A-Z])\\s*.*$', E'\\1' );
317 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s+[A-Z][A-Z](\\s*.*)$', E'\\1\\2' );
319 IF city_state_zip ~ E'^\\S+$' THEN
320 city := city_state_zip;
323 state := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\2');
324 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\1');
328 RETURN ARRAY[ TRIM(BOTH ' ' FROM city), TRIM(BOTH ' ' FROM state), TRIM(BOTH ' ' FROM zip) ];
330 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
332 CREATE OR REPLACE FUNCTION migration_tools.rebarcode (o TEXT, t BIGINT) RETURNS TEXT AS $$
336 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
337 IF o::BIGINT < t THEN
344 $$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
346 CREATE OR REPLACE FUNCTION migration_tools.base_profile_map (TEXT) RETURNS TEXT AS $$
348 migration_schema ALIAS FOR $1;
352 EXECUTE 'SELECT ''' || migration_schema || '.'' || value FROM ' || migration_schema || '.config WHERE key = ''base_profile_map'';'
357 $$ LANGUAGE PLPGSQL STRICT STABLE;
359 CREATE OR REPLACE FUNCTION migration_tools.base_item_dynamic_field_map (TEXT) RETURNS TEXT AS $$
361 migration_schema ALIAS FOR $1;
365 EXECUTE 'SELECT ''' || migration_schema || '.'' || value FROM ' || migration_schema || '.config WHERE key = ''base_item_dynamic_field_map'';'
370 $$ LANGUAGE PLPGSQL STRICT STABLE;
372 CREATE OR REPLACE FUNCTION migration_tools.base_copy_location_map (TEXT) RETURNS TEXT AS $$
374 migration_schema ALIAS FOR $1;
378 EXECUTE 'SELECT ''' || migration_schema || '.'' || value FROM ' || migration_schema || '.config WHERE key = ''base_copy_location_map'';'
383 $$ LANGUAGE PLPGSQL STRICT STABLE;
385 CREATE OR REPLACE FUNCTION migration_tools.map_base_patron_profile (TEXT,TEXT,INTEGER) RETURNS VOID AS $$
387 migration_schema ALIAS FOR $1;
389 patron_table ALIAS FOR $2;
390 default_patron_profile ALIAS FOR $3;
393 sql_where1 TEXT := '';
394 sql_where2 TEXT := '';
395 sql_where3 TEXT := '';
398 SELECT migration_tools.base_profile_map(migration_schema) INTO STRICT profile_map;
400 EXECUTE 'SELECT * FROM ' || profile_map || E' ORDER BY id;'
402 sql_update := 'UPDATE ' || patron_table || ' AS u SET profile = perm_grp_id FROM ' || profile_map || ' AS m WHERE ';
403 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);
404 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);
405 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);
406 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,'') || ';';
407 --RAISE INFO 'sql = %', sql;
408 PERFORM migration_tools.exec( $1, sql );
410 PERFORM migration_tools.exec( $1, 'UPDATE ' || patron_table || ' AS u SET profile = ' || quote_literal(default_patron_profile) || ' WHERE profile IS NULL;' );
412 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_base_patron_mapping_profile'', now() );' );
414 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_base_patron_mapping_profile'';' );
417 $$ LANGUAGE PLPGSQL STRICT STABLE;
419 CREATE OR REPLACE FUNCTION migration_tools.map_base_item_table_dynamic (TEXT,TEXT) RETURNS VOID AS $$
421 migration_schema ALIAS FOR $1;
423 item_table ALIAS FOR $2;
426 sql_where1 TEXT := '';
427 sql_where2 TEXT := '';
428 sql_where3 TEXT := '';
431 SELECT migration_tools.base_item_dynamic_field_map(migration_schema) INTO STRICT field_map;
433 EXECUTE 'SELECT * FROM ' || field_map || E' ORDER BY id;'
435 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 ';
436 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);
437 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);
438 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);
439 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,'') || ';';
440 --RAISE INFO 'sql = %', sql;
441 PERFORM migration_tools.exec( $1, sql );
444 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_base_item_mapping_dynamic'', now() );' );
446 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_base_item_mapping_dynamic'';' );
449 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
451 CREATE OR REPLACE FUNCTION migration_tools.map_base_item_table_locations (TEXT,TEXT) RETURNS VOID AS $$
453 migration_schema ALIAS FOR $1;
454 base_copy_location_map TEXT;
455 item_table ALIAS FOR $2;
458 sql_where1 TEXT := '';
459 sql_where2 TEXT := '';
460 sql_where3 TEXT := '';
463 SELECT migration_tools.base_copy_location_map(migration_schema) INTO STRICT base_copy_location_map;
465 EXECUTE 'SELECT * FROM ' || base_copy_location_map || E' ORDER BY id;'
467 sql_update := 'UPDATE ' || item_table || ' AS i SET location = m.location FROM ' || base_copy_location_map || ' AS m WHERE ';
468 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);
469 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);
470 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);
471 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,'') || ';';
472 --RAISE INFO 'sql = %', sql;
473 PERFORM migration_tools.exec( $1, sql );
476 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_base_item_mapping_locations'', now() );' );
478 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_base_item_mapping_locations'';' );
481 $$ LANGUAGE PLPGSQL STRICT VOLATILE;