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.address_parse_out_citystatezip (TEXT) RETURNS TEXT[] AS $$
212 city_state_zip TEXT := $1;
217 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;
218 city_state_zip := REGEXP_REPLACE( city_state_zip, E'^(.*)\\d\\d\\d\\d\\d-?\\d*(.*)$', E'\\1\\2');
219 IF city_state_zip ~ ',' THEN
220 state := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\2');
221 city := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\1');
223 IF city_state_zip ~ E'\\s+[A-Z][A-Z]\\s*' THEN
224 state := REGEXP_REPLACE( city_state_zip, E'^.*,?\\s+([A-Z][A-Z])\\s*.*$', E'\\1' );
225 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s+[A-Z][A-Z](\\s*.*)$', E'\\1\\2' );
227 IF city_state_zip ~ E'^\\S+$' THEN
228 city := city_state_zip;
231 state := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\2');
232 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\1');
236 RETURN ARRAY[ TRIM(BOTH ' ' FROM city), TRIM(BOTH ' ' FROM state), TRIM(BOTH ' ' FROM zip) ];
238 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
240 CREATE OR REPLACE FUNCTION migration_tools.rebarcode (o TEXT, t BIGINT) RETURNS TEXT AS $$
244 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
245 IF o::BIGINT < t THEN
252 $$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
254 CREATE OR REPLACE FUNCTION migration_tools.base_profile_map (TEXT) RETURNS TEXT AS $$
256 migration_schema ALIAS FOR $1;
260 EXECUTE 'SELECT ''' || migration_schema || '.'' || value FROM ' || migration_schema || '.config WHERE key = ''base_profile_map'';'
265 $$ LANGUAGE PLPGSQL STRICT STABLE;
267 CREATE OR REPLACE FUNCTION migration_tools.map_base_patron_profile (TEXT,TEXT,TEXT) RETURNS VOID AS $$
269 migration_schema ALIAS FOR $1;
271 patron_table ALIAS FOR $2;
272 default_patron_profile ALIAS FOR $3;
275 sql_where1 TEXT := '';
276 sql_where2 TEXT := '';
277 sql_where3 TEXT := '';
280 SELECT migration_tools.base_profile_map(migration_schema) INTO STRICT profile_map;
282 EXECUTE 'SELECT * FROM ' || profile_map || E' ORDER BY id;'
284 sql_update := 'UPDATE ' || patron_table || ' AS u SET profile = perm_grp_id FROM ' || profile_map || ' AS m WHERE ';
285 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);
286 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);
287 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);
288 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,'') || ';';
289 --RAISE INFO 'sql = %', sql;
290 PERFORM migration_tools.exec( $1, sql );
292 PERFORM migration_tools.exec( $1, 'UPDATE ' || patron_table || ' AS u SET profile = ' || quote_literal(default_patron_profile) || ' WHERE profile IS NULL;' );
294 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_base_patron_mapping_profile'', now() );' );
296 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_base_patron_mapping_profile'';' );
299 $$ LANGUAGE PLPGSQL STRICT STABLE;