syntax
[migration-tools.git] / sql / base / base_migration_schema_procs.sql
1 --------------------------------------------------------------------------
2 -- An example of how to use:
3 -- 
4 -- DROP SCHEMA foo CASCADE; CREATE SCHEMA foo; 
5 -- \i base_migration_schema_procs.sql
6 -- SELECT migration_tools.build_default_base_staging_tables('foo');
7 -- SELECT * FROM migration_tools.fields_requiring_mapping;
8 -- \d foo.actor_usr
9 -- create some incoming ILS specific staging tables, like CREATE foo.legacy_items ( l_barcode TEXT, .. ) INHERITS foo.asset_copy;
10 -- Do some mapping, like UPDATE foo.legacy_items SET barcode = TRIM(BOTH ' ' FROM l_barcode);
11 -- Then, to move into production, do: select migration_tools.insert_default_into_production('foo')
12
13 CREATE SCHEMA migration_tools;
14
15 CREATE TABLE migration_tools.config (
16     key TEXT UNIQUE,
17     value TEXT
18 );
19
20 INSERT INTO migration_tools.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' );
21
22 CREATE OR REPLACE FUNCTION migration_tools.production_tables (TEXT) RETURNS TEXT[] AS $$
23     DECLARE
24         migration_schema ALIAS FOR $1;
25         output  RECORD;
26     BEGIN
27         FOR output IN
28             EXECUTE 'SELECT string_to_array(value,'','') AS tables FROM ' || migration_schema || '.config WHERE key = ''production_tables'';'
29         LOOP
30             RETURN output.tables;
31         END LOOP;
32     END;
33 $$ LANGUAGE PLPGSQL STRICT STABLE;
34
35 CREATE OR REPLACE FUNCTION migration_tools.base_init (TEXT) RETURNS VOID AS $$
36     DECLARE
37         migration_schema ALIAS FOR $1;
38     BEGIN
39         EXECUTE 'DROP TABLE IF EXISTS ' || migration_schema || '.fields_requiring_mapping;';
40         EXECUTE 'CREATE TABLE ' || migration_schema || '.fields_requiring_mapping( table_schema TEXT, table_name TEXT, column_name TEXT, data_type TEXT);';
41     END;
42 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
43
44 CREATE OR REPLACE FUNCTION migration_tools.build_default_base_staging_tables (TEXT) RETURNS VOID AS $$
45     DECLARE
46         migration_schema ALIAS FOR $1;
47         production_tables TEXT[];
48     BEGIN
49         --RAISE INFO 'In migration_tools.build_default_base_staging_tables(%)', migration_schema;
50         SELECT migration_tools.production_tables() INTO STRICT production_tables;
51         EXECUTE migration_tools.build_base_staging_tables(migration_schema,production_tables);
52     END;
53 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
54
55 CREATE OR REPLACE FUNCTION migration_tools.build_base_staging_tables (TEXT,TEXT[]) RETURNS VOID AS $$
56     DECLARE
57         migration_schema ALIAS FOR $1;
58         production_tables ALIAS FOR $2;
59     BEGIN
60         --RAISE INFO 'In migration_tools.build_base_staging_tables(%,%)', migration_schema, production_tables;
61         FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
62             EXECUTE migration_tools.build_specific_base_staging_table(migration_schema,production_tables[i]);
63         END LOOP;
64     END;
65 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
66
67 CREATE OR REPLACE FUNCTION migration_tools.build_specific_base_staging_table (TEXT,TEXT) RETURNS VOID AS $$
68     DECLARE
69         migration_schema ALIAS FOR $1;
70         production_table ALIAS FOR $2;
71         base_staging_table TEXT;
72         columns RECORD;
73     BEGIN
74         base_staging_table = REPLACE( production_table, '.', '_' );
75         --RAISE INFO 'In migration_tools.build_specific_base_staging_table(%,%) -> %', migration_schema, production_table, base_staging_table;
76         EXECUTE 'CREATE TABLE ' || migration_schema || '.' || base_staging_table || ' ( like ' || production_table || ' including defaults excluding constraints );';
77         INSERT INTO migration_tools.fields_requiring_mapping
78             SELECT table_schema, table_name, column_name, data_type
79             FROM information_schema.columns 
80             WHERE table_schema = migration_schema AND table_name = base_staging_table AND is_nullable = 'NO' AND column_default IS NULL;
81         FOR columns IN 
82             SELECT table_schema, table_name, column_name, data_type
83             FROM information_schema.columns 
84             WHERE table_schema = migration_schema AND table_name = base_staging_table AND is_nullable = 'NO' AND column_default IS NULL
85         LOOP
86             EXECUTE 'ALTER TABLE ' || columns.table_schema || '.' || columns.table_name || ' ALTER COLUMN ' || columns.column_name || ' DROP NOT NULL;';
87         END LOOP;
88     END;
89 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
90
91 CREATE OR REPLACE FUNCTION migration_tools.insert_default_into_production (TEXT) RETURNS VOID AS $$
92     DECLARE
93         migration_schema ALIAS FOR $1;
94         production_tables TEXT[];
95     BEGIN
96         --RAISE INFO 'In migration_tools.insert_into_production(%)', migration_schema;
97         SELECT migration_tools.production_tables() INTO STRICT production_tables;
98         FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
99             EXECUTE migration_tools.insert_into_production(migration_schema,production_tables[i]);
100         END LOOP;
101     END;
102 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
103
104 CREATE OR REPLACE FUNCTION migration_tools.insert_into_production (TEXT,TEXT) RETURNS VOID AS $$
105     DECLARE
106         migration_schema ALIAS FOR $1;
107         production_table ALIAS FOR $2;
108         base_staging_table TEXT;
109         columns RECORD;
110     BEGIN
111         base_staging_table = REPLACE( production_table, '.', '_' );
112         --RAISE INFO 'In migration_tools.insert_into_production(%,%) -> %', migration_schema, production_table, base_staging_table;
113         EXECUTE 'INSERT INTO ' || production_table || ' SELECT * FROM ' || migration_schema || '.' || base_staging_table || ';';
114     END;
115 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
116
117 CREATE OR REPLACE FUNCTION migration_tools.address_parse_out_citystatezip (TEXT) RETURNS TEXT[] AS $$
118     DECLARE
119         city_state_zip TEXT := $1;
120         city TEXT := '';
121         state TEXT := '';
122         zip TEXT := '';
123     BEGIN
124         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;
125         city_state_zip := REGEXP_REPLACE( city_state_zip, E'^(.*)\\d\\d\\d\\d\\d-?\\d*(.*)$', E'\\1\\2');
126         IF city_state_zip ~ ',' THEN
127             state := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\2');
128             city := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\1');
129         ELSE
130             IF city_state_zip ~ E'\\s+[A-Z][A-Z]\\s*' THEN
131                 state := REGEXP_REPLACE( city_state_zip, E'^.*,?\\s+([A-Z][A-Z])\\s*.*$', E'\\1' );
132                 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s+[A-Z][A-Z](\\s*.*)$', E'\\1\\2' );
133             ELSE
134                 IF city_state_zip ~ E'^\\S+$'  THEN
135                     city := city_state_zip;
136                     state := 'N/A';
137                 ELSE
138                     state := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\2');
139                     city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\1');
140                 END IF;
141             END IF;
142         END IF;
143         RETURN ARRAY[ TRIM(BOTH ' ' FROM city), TRIM(BOTH ' ' FROM state), TRIM(BOTH ' ' FROM zip) ];
144     END;
145 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
146