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) || ';';
61 GET DIAGNOSTICS nrows = ROW_COUNT;
62 PERFORM migration_tools.log(migration_schema,sql,nrows);
65 RAISE EXCEPTION '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
67 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
69 CREATE OR REPLACE FUNCTION migration_tools.init (TEXT) RETURNS VOID AS $$
71 migration_schema ALIAS FOR $1;
74 EXECUTE 'DROP TABLE IF EXISTS ' || migration_schema || '.sql_current;';
75 EXECUTE 'CREATE TABLE ' || migration_schema || '.sql_current ( sql TEXT);';
76 EXECUTE 'INSERT INTO ' || migration_schema || '.sql_current ( sql ) VALUES ( '''' );';
78 SELECT 'CREATE TABLE ' || migration_schema || '.sql_log ( time TIMESTAMP NOT NULL DEFAULT NOW(), row_count INTEGER, sql TEXT );' INTO STRICT sql;
82 RAISE INFO '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
84 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.config;' );
85 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.config ( key TEXT UNIQUE, value TEXT);' );
86 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'' );' );
87 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''country_code'', ''USA'' );' );
88 PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.fields_requiring_mapping;' );
89 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.fields_requiring_mapping( table_schema TEXT, table_name TEXT, column_name TEXT, data_type TEXT);' );
91 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_init'', now() );' );
93 WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_init'';' );
96 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
98 CREATE OR REPLACE FUNCTION migration_tools.build (TEXT) RETURNS VOID AS $$
100 migration_schema ALIAS FOR $1;
101 production_tables TEXT[];
103 --RAISE INFO 'In migration_tools.build(%)', migration_schema;
104 SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
105 PERFORM migration_tools.build_base_staging_tables(migration_schema,production_tables);
107 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
109 CREATE OR REPLACE FUNCTION migration_tools.build_base_staging_tables (TEXT,TEXT[]) RETURNS VOID AS $$
111 migration_schema ALIAS FOR $1;
112 production_tables ALIAS FOR $2;
114 --RAISE INFO 'In migration_tools.build_base_staging_tables(%,%)', migration_schema, production_tables;
115 FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
116 PERFORM migration_tools.build_specific_base_staging_table(migration_schema,production_tables[i]);
119 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
121 CREATE OR REPLACE FUNCTION migration_tools.build_specific_base_staging_table (TEXT,TEXT) RETURNS VOID AS $$
123 migration_schema ALIAS FOR $1;
124 production_table ALIAS FOR $2;
125 base_staging_table TEXT;
128 base_staging_table = REPLACE( production_table, '.', '_' );
129 --RAISE INFO 'In migration_tools.build_specific_base_staging_table(%,%) -> %', migration_schema, production_table, base_staging_table;
130 PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.' || base_staging_table || ' ( LIKE ' || production_table || ' INCLUDING DEFAULTS EXCLUDING CONSTRAINTS );' );
131 PERFORM migration_tools.exec( $1, '
132 INSERT INTO ' || migration_schema || '.fields_requiring_mapping
133 SELECT table_schema, table_name, column_name, data_type
134 FROM information_schema.columns
135 WHERE table_schema = ''' || migration_schema || ''' AND table_name = ''' || base_staging_table || ''' AND is_nullable = ''NO'' AND column_default IS NULL;
138 SELECT table_schema, table_name, column_name, data_type
139 FROM information_schema.columns
140 WHERE table_schema = migration_schema AND table_name = base_staging_table AND is_nullable = 'NO' AND column_default IS NULL
142 PERFORM migration_tools.exec( $1, 'ALTER TABLE ' || columns.table_schema || '.' || columns.table_name || ' ALTER COLUMN ' || columns.column_name || ' DROP NOT NULL;' );
145 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
147 CREATE OR REPLACE FUNCTION migration_tools.insert_base_into_production (TEXT) RETURNS VOID AS $$
149 migration_schema ALIAS FOR $1;
150 production_tables TEXT[];
152 --RAISE INFO 'In migration_tools.insert_into_production(%)', migration_schema;
153 SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
154 FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
155 PERFORM migration_tools.insert_into_production(migration_schema,production_tables[i]);
158 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
160 CREATE OR REPLACE FUNCTION migration_tools.insert_into_production (TEXT,TEXT) RETURNS VOID AS $$
162 migration_schema ALIAS FOR $1;
163 production_table ALIAS FOR $2;
164 base_staging_table TEXT;
167 base_staging_table = REPLACE( production_table, '.', '_' );
168 --RAISE INFO 'In migration_tools.insert_into_production(%,%) -> %', migration_schema, production_table, base_staging_table;
169 PERFORM migration_tools.exec( $1, 'INSERT INTO ' || production_table || ' SELECT * FROM ' || migration_schema || '.' || base_staging_table || ';' );
171 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
173 CREATE OR REPLACE FUNCTION migration_tools.address_parse_out_citystatezip (TEXT) RETURNS TEXT[] AS $$
175 city_state_zip TEXT := $1;
180 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;
181 city_state_zip := REGEXP_REPLACE( city_state_zip, E'^(.*)\\d\\d\\d\\d\\d-?\\d*(.*)$', E'\\1\\2');
182 IF city_state_zip ~ ',' THEN
183 state := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\2');
184 city := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\1');
186 IF city_state_zip ~ E'\\s+[A-Z][A-Z]\\s*' THEN
187 state := REGEXP_REPLACE( city_state_zip, E'^.*,?\\s+([A-Z][A-Z])\\s*.*$', E'\\1' );
188 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s+[A-Z][A-Z](\\s*.*)$', E'\\1\\2' );
190 IF city_state_zip ~ E'^\\S+$' THEN
191 city := city_state_zip;
194 state := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\2');
195 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\1');
199 RETURN ARRAY[ TRIM(BOTH ' ' FROM city), TRIM(BOTH ' ' FROM state), TRIM(BOTH ' ' FROM zip) ];
201 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
203 CREATE OR REPLACE FUNCTION migration_tools.rebarcode (o TEXT, t BIGINT) RETURNS TEXT AS $$
207 IF o ~ E'^\\d+$' AND o !~ E'^0' THEN
208 IF o::BIGINT < t THEN
215 $$ LANGUAGE PLPGSQL STRICT IMMUTABLE;