sql_log table
[migration-tools.git] / sql / base / base.sql
1 --------------------------------------------------------------------------
2 -- An example of how to use:
3 -- 
4 -- DROP SCHEMA foo CASCADE; CREATE SCHEMA foo; 
5 -- \i base.sql
6 -- SELECT migration_tools.init('foo');
7 -- SELECT migration_tools.build('foo');
8 -- SELECT * FROM foo.fields_requiring_mapping;
9 -- \d foo.actor_usr
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')
13
14 CREATE SCHEMA migration_tools;
15
16 CREATE OR REPLACE FUNCTION migration_tools.production_tables (TEXT) RETURNS TEXT[] AS $$
17     DECLARE
18         migration_schema ALIAS FOR $1;
19         output  RECORD;
20     BEGIN
21         FOR output IN
22             EXECUTE 'SELECT string_to_array(value,'','') AS tables FROM ' || migration_schema || '.config WHERE key = ''production_tables'';'
23         LOOP
24             RETURN output.tables;
25         END LOOP;
26     END;
27 $$ LANGUAGE PLPGSQL STRICT STABLE;
28
29 CREATE OR REPLACE FUNCTION migration_tools.log (TEXT,TEXT,INTEGER) RETURNS VOID AS $$
30     DECLARE
31         migration_schema ALIAS FOR $1;
32         sql ALIAS FOR $2;
33         nrows ALIAS FOR $3;
34     BEGIN
35         EXECUTE 'INSERT INTO ' || migration_schema || '.sql_log ( sql, row_count ) VALUES ( ' || quote_literal(sql) || ', ' || nrows || ' );';
36     END;
37 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
38
39 CREATE OR REPLACE FUNCTION migration_tools.exec (TEXT,TEXT) RETURNS VOID AS $$
40     DECLARE
41         migration_schema ALIAS FOR $1;
42         sql ALIAS FOR $2;
43         nrows INTEGER;
44     BEGIN
45         EXECUTE sql;
46         GET DIAGNOSTICS nrows = ROW_COUNT;
47         PERFORM migration_tools.log(migration_schema,sql,nrows);
48     EXCEPTION
49         WHEN OTHERS THEN 
50             RAISE EXCEPTION '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
51     END;
52 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
53
54 CREATE OR REPLACE FUNCTION migration_tools.init (TEXT) RETURNS VOID AS $$
55     DECLARE
56         migration_schema ALIAS FOR $1;
57         sql TEXT;
58     BEGIN
59         BEGIN
60             SELECT 'CREATE TABLE ' || migration_schema || '.sql_log ( time TIMESTAMP NOT NULL DEFAULT NOW(), row_count INTEGER, sql TEXT );' INTO STRICT sql;
61             EXECUTE sql;
62         EXCEPTION
63             WHEN OTHERS THEN 
64                 RAISE INFO '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
65         END;
66         PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.config;' );
67         PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.config ( key TEXT UNIQUE, value TEXT);' );
68         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'' );' );
69         PERFORM migration_tools.exec( $1, 'DROP TABLE IF EXISTS ' || migration_schema || '.fields_requiring_mapping;' );
70         PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.fields_requiring_mapping( table_schema TEXT, table_name TEXT, column_name TEXT, data_type TEXT);' );
71         BEGIN
72             PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_init'', now() );' );
73         EXCEPTION
74             WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_init'';' );
75         END;
76     END;
77 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
78
79 CREATE OR REPLACE FUNCTION migration_tools.build (TEXT) RETURNS VOID AS $$
80     DECLARE
81         migration_schema ALIAS FOR $1;
82         production_tables TEXT[];
83     BEGIN
84         --RAISE INFO 'In migration_tools.build(%)', migration_schema;
85         SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
86         PERFORM migration_tools.build_base_staging_tables(migration_schema,production_tables);
87     END;
88 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
89
90 CREATE OR REPLACE FUNCTION migration_tools.build_base_staging_tables (TEXT,TEXT[]) RETURNS VOID AS $$
91     DECLARE
92         migration_schema ALIAS FOR $1;
93         production_tables ALIAS FOR $2;
94     BEGIN
95         --RAISE INFO 'In migration_tools.build_base_staging_tables(%,%)', migration_schema, production_tables;
96         FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
97             PERFORM migration_tools.build_specific_base_staging_table(migration_schema,production_tables[i]);
98         END LOOP;
99     END;
100 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
101
102 CREATE OR REPLACE FUNCTION migration_tools.build_specific_base_staging_table (TEXT,TEXT) RETURNS VOID AS $$
103     DECLARE
104         migration_schema ALIAS FOR $1;
105         production_table ALIAS FOR $2;
106         base_staging_table TEXT;
107         columns RECORD;
108     BEGIN
109         base_staging_table = REPLACE( production_table, '.', '_' );
110         --RAISE INFO 'In migration_tools.build_specific_base_staging_table(%,%) -> %', migration_schema, production_table, base_staging_table;
111         PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.' || base_staging_table || ' ( LIKE ' || production_table || ' INCLUDING DEFAULTS EXCLUDING CONSTRAINTS );' );
112         PERFORM migration_tools.exec( $1, '
113             INSERT INTO ' || migration_schema || '.fields_requiring_mapping
114                 SELECT table_schema, table_name, column_name, data_type
115                 FROM information_schema.columns 
116                 WHERE table_schema = ''' || migration_schema || ''' AND table_name = ''' || base_staging_table || ''' AND is_nullable = ''NO'' AND column_default IS NULL;
117         ' );
118         FOR columns IN 
119             SELECT table_schema, table_name, column_name, data_type
120             FROM information_schema.columns 
121             WHERE table_schema = migration_schema AND table_name = base_staging_table AND is_nullable = 'NO' AND column_default IS NULL
122         LOOP
123             PERFORM migration_tools.exec( $1, 'ALTER TABLE ' || columns.table_schema || '.' || columns.table_name || ' ALTER COLUMN ' || columns.column_name || ' DROP NOT NULL;' );
124         END LOOP;
125     END;
126 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
127
128 CREATE OR REPLACE FUNCTION migration_tools.insert_base_into_production (TEXT) RETURNS VOID AS $$
129     DECLARE
130         migration_schema ALIAS FOR $1;
131         production_tables TEXT[];
132     BEGIN
133         --RAISE INFO 'In migration_tools.insert_into_production(%)', migration_schema;
134         SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
135         FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
136             PERFORM migration_tools.insert_into_production(migration_schema,production_tables[i]);
137         END LOOP;
138     END;
139 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
140
141 CREATE OR REPLACE FUNCTION migration_tools.insert_into_production (TEXT,TEXT) RETURNS VOID AS $$
142     DECLARE
143         migration_schema ALIAS FOR $1;
144         production_table ALIAS FOR $2;
145         base_staging_table TEXT;
146         columns RECORD;
147     BEGIN
148         base_staging_table = REPLACE( production_table, '.', '_' );
149         --RAISE INFO 'In migration_tools.insert_into_production(%,%) -> %', migration_schema, production_table, base_staging_table;
150         PERFORM migration_tools.exec( $1, 'INSERT INTO ' || production_table || ' SELECT * FROM ' || migration_schema || '.' || base_staging_table || ';' );
151     END;
152 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
153
154 CREATE OR REPLACE FUNCTION migration_tools.address_parse_out_citystatezip (TEXT) RETURNS TEXT[] AS $$
155     DECLARE
156         city_state_zip TEXT := $1;
157         city TEXT := '';
158         state TEXT := '';
159         zip TEXT := '';
160     BEGIN
161         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;
162         city_state_zip := REGEXP_REPLACE( city_state_zip, E'^(.*)\\d\\d\\d\\d\\d-?\\d*(.*)$', E'\\1\\2');
163         IF city_state_zip ~ ',' THEN
164             state := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\2');
165             city := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\1');
166         ELSE
167             IF city_state_zip ~ E'\\s+[A-Z][A-Z]\\s*' THEN
168                 state := REGEXP_REPLACE( city_state_zip, E'^.*,?\\s+([A-Z][A-Z])\\s*.*$', E'\\1' );
169                 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s+[A-Z][A-Z](\\s*.*)$', E'\\1\\2' );
170             ELSE
171                 IF city_state_zip ~ E'^\\S+$'  THEN
172                     city := city_state_zip;
173                     state := 'N/A';
174                 ELSE
175                     state := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\2');
176                     city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\1');
177                 END IF;
178             END IF;
179         END IF;
180         RETURN ARRAY[ TRIM(BOTH ' ' FROM city), TRIM(BOTH ' ' FROM state), TRIM(BOTH ' ' FROM zip) ];
181     END;
182 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
183
184 CREATE OR REPLACE FUNCTION migration_tools.rebarcode (o TEXT, t BIGINT) RETURNS TEXT AS $$
185     DECLARE
186         n TEXT := o;
187     BEGIN
188         IF o ~ E'^\\d+$' THEN
189             n = o::INT + t;
190         END IF;
191
192         RETURN n;
193     END;
194 $$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
195