69c33b093802619ae749b36457a131b755e94cf7
[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.country_code (TEXT) RETURNS TEXT AS $$
30     DECLARE
31         migration_schema ALIAS FOR $1;
32         output TEXT;
33     BEGIN
34         FOR output IN
35             EXECUTE 'SELECT value FROM ' || migration_schema || '.config WHERE key = ''country_code'';'
36         LOOP
37             RETURN output;
38         END LOOP;
39     END;
40 $$ LANGUAGE PLPGSQL STRICT STABLE;
41
42
43 CREATE OR REPLACE FUNCTION migration_tools.log (TEXT,TEXT,INTEGER) RETURNS VOID AS $$
44     DECLARE
45         migration_schema ALIAS FOR $1;
46         sql ALIAS FOR $2;
47         nrows ALIAS FOR $3;
48     BEGIN
49         EXECUTE 'INSERT INTO ' || migration_schema || '.sql_log ( sql, row_count ) VALUES ( ' || quote_literal(sql) || ', ' || nrows || ' );';
50     END;
51 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
52
53 CREATE OR REPLACE FUNCTION migration_tools.exec (TEXT,TEXT) RETURNS VOID AS $$
54     DECLARE
55         migration_schema ALIAS FOR $1;
56         sql ALIAS FOR $2;
57         nrows INTEGER;
58     BEGIN
59         EXECUTE 'UPDATE ' || migration_schema || '.sql_current SET sql = ' || quote_literal(sql) || ';';
60         EXECUTE sql;
61         GET DIAGNOSTICS nrows = ROW_COUNT;
62         PERFORM migration_tools.log(migration_schema,sql,nrows);
63     EXCEPTION
64         WHEN OTHERS THEN 
65             RAISE EXCEPTION '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
66     END;
67 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
68
69 CREATE OR REPLACE FUNCTION migration_tools.init (TEXT) RETURNS VOID AS $$
70     DECLARE
71         migration_schema ALIAS FOR $1;
72         sql TEXT;
73     BEGIN
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 ( '''' );';
77         BEGIN
78             SELECT 'CREATE TABLE ' || migration_schema || '.sql_log ( time TIMESTAMP NOT NULL DEFAULT NOW(), row_count INTEGER, sql TEXT );' INTO STRICT sql;
79             EXECUTE sql;
80         EXCEPTION
81             WHEN OTHERS THEN 
82                 RAISE INFO '!!!!!!!!!!! state = %, msg = %, sql = %', SQLSTATE, SQLERRM, sql;
83         END;
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);' );
90         BEGIN
91             PERFORM migration_tools.exec( $1, 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_init'', now() );' );
92         EXCEPTION
93             WHEN OTHERS THEN PERFORM migration_tools.exec( $1, 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_init'';' );
94         END;
95     END;
96 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
97
98 CREATE OR REPLACE FUNCTION migration_tools.build (TEXT) RETURNS VOID AS $$
99     DECLARE
100         migration_schema ALIAS FOR $1;
101         production_tables TEXT[];
102     BEGIN
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);
106         PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_barcode_key ON ' || migration_schema || '.actor_card ( barcode );' );
107         PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_usrname_key ON ' || migration_schema || '.actor_usr ( usrname );' );
108         PERFORM migration_tools.exec( $1, 'CREATE UNIQUE INDEX ' || migration_schema || '_copy_barcode_key ON ' || migration_schema || '.asset_copy ( barcode );' );
109         PERFORM migration_tools.exec( $1, 'CREATE INDEX ' || migration_schema || '_callnum_record_idx ON ' || migration_schema || '.asset_call_number ( record );' );
110         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 );' );
111         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 );' );
112     END;
113 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
114
115 CREATE OR REPLACE FUNCTION migration_tools.build_base_staging_tables (TEXT,TEXT[]) RETURNS VOID AS $$
116     DECLARE
117         migration_schema ALIAS FOR $1;
118         production_tables ALIAS FOR $2;
119     BEGIN
120         --RAISE INFO 'In migration_tools.build_base_staging_tables(%,%)', migration_schema, production_tables;
121         FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
122             PERFORM migration_tools.build_specific_base_staging_table(migration_schema,production_tables[i]);
123         END LOOP;
124     END;
125 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
126
127 CREATE OR REPLACE FUNCTION migration_tools.build_specific_base_staging_table (TEXT,TEXT) RETURNS VOID AS $$
128     DECLARE
129         migration_schema ALIAS FOR $1;
130         production_table ALIAS FOR $2;
131         base_staging_table TEXT;
132         columns RECORD;
133     BEGIN
134         base_staging_table = REPLACE( production_table, '.', '_' );
135         --RAISE INFO 'In migration_tools.build_specific_base_staging_table(%,%) -> %', migration_schema, production_table, base_staging_table;
136         PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.' || base_staging_table || ' ( LIKE ' || production_table || ' INCLUDING DEFAULTS EXCLUDING CONSTRAINTS );' );
137         PERFORM migration_tools.exec( $1, '
138             INSERT INTO ' || migration_schema || '.fields_requiring_mapping
139                 SELECT table_schema, table_name, column_name, data_type
140                 FROM information_schema.columns 
141                 WHERE table_schema = ''' || migration_schema || ''' AND table_name = ''' || base_staging_table || ''' AND is_nullable = ''NO'' AND column_default IS NULL;
142         ' );
143         FOR columns IN 
144             SELECT table_schema, table_name, column_name, data_type
145             FROM information_schema.columns 
146             WHERE table_schema = migration_schema AND table_name = base_staging_table AND is_nullable = 'NO' AND column_default IS NULL
147         LOOP
148             PERFORM migration_tools.exec( $1, 'ALTER TABLE ' || columns.table_schema || '.' || columns.table_name || ' ALTER COLUMN ' || columns.column_name || ' DROP NOT NULL;' );
149         END LOOP;
150     END;
151 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
152
153 CREATE OR REPLACE FUNCTION migration_tools.insert_base_into_production (TEXT) RETURNS VOID AS $$
154     DECLARE
155         migration_schema ALIAS FOR $1;
156         production_tables TEXT[];
157     BEGIN
158         --RAISE INFO 'In migration_tools.insert_into_production(%)', migration_schema;
159         SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
160         FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
161             PERFORM migration_tools.insert_into_production(migration_schema,production_tables[i]);
162         END LOOP;
163     END;
164 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
165
166 CREATE OR REPLACE FUNCTION migration_tools.insert_into_production (TEXT,TEXT) RETURNS VOID AS $$
167     DECLARE
168         migration_schema ALIAS FOR $1;
169         production_table ALIAS FOR $2;
170         base_staging_table TEXT;
171         columns RECORD;
172     BEGIN
173         base_staging_table = REPLACE( production_table, '.', '_' );
174         --RAISE INFO 'In migration_tools.insert_into_production(%,%) -> %', migration_schema, production_table, base_staging_table;
175         PERFORM migration_tools.exec( $1, 'INSERT INTO ' || production_table || ' SELECT * FROM ' || migration_schema || '.' || base_staging_table || ';' );
176     END;
177 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
178
179 CREATE OR REPLACE FUNCTION migration_tools.address_parse_out_citystatezip (TEXT) RETURNS TEXT[] AS $$
180     DECLARE
181         city_state_zip TEXT := $1;
182         city TEXT := '';
183         state TEXT := '';
184         zip TEXT := '';
185     BEGIN
186         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;
187         city_state_zip := REGEXP_REPLACE( city_state_zip, E'^(.*)\\d\\d\\d\\d\\d-?\\d*(.*)$', E'\\1\\2');
188         IF city_state_zip ~ ',' THEN
189             state := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\2');
190             city := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\1');
191         ELSE
192             IF city_state_zip ~ E'\\s+[A-Z][A-Z]\\s*' THEN
193                 state := REGEXP_REPLACE( city_state_zip, E'^.*,?\\s+([A-Z][A-Z])\\s*.*$', E'\\1' );
194                 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s+[A-Z][A-Z](\\s*.*)$', E'\\1\\2' );
195             ELSE
196                 IF city_state_zip ~ E'^\\S+$'  THEN
197                     city := city_state_zip;
198                     state := 'N/A';
199                 ELSE
200                     state := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\2');
201                     city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\1');
202                 END IF;
203             END IF;
204         END IF;
205         RETURN ARRAY[ TRIM(BOTH ' ' FROM city), TRIM(BOTH ' ' FROM state), TRIM(BOTH ' ' FROM zip) ];
206     END;
207 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
208
209 CREATE OR REPLACE FUNCTION migration_tools.rebarcode (o TEXT, t BIGINT) RETURNS TEXT AS $$
210     DECLARE
211         n TEXT := o;
212     BEGIN
213         IF o ~ E'^\\d+$' AND o !~ E'^0' THEN
214             IF o::BIGINT < t THEN
215                 n = o::BIGINT + t;
216             END IF;
217         END IF;
218
219         RETURN n;
220     END;
221 $$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
222