(no commit message)
[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.init (TEXT) RETURNS VOID AS $$
30     DECLARE
31         migration_schema ALIAS FOR $1;
32     BEGIN
33         EXECUTE 'DROP TABLE IF EXISTS ' || migration_schema || '.config;';
34         EXECUTE 'CREATE TABLE ' || migration_schema || '.config ( key TEXT UNIQUE, value TEXT);';
35         EXECUTE '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'' );';
36         EXECUTE 'DROP TABLE IF EXISTS ' || migration_schema || '.fields_requiring_mapping;';
37         EXECUTE 'CREATE TABLE ' || migration_schema || '.fields_requiring_mapping( table_schema TEXT, table_name TEXT, column_name TEXT, data_type TEXT);';
38         BEGIN
39             EXECUTE 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_init'', now() );';
40         EXCEPTION
41             WHEN OTHERS THEN EXECUTE 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_init'';';
42         END;
43     END;
44 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
45
46 CREATE OR REPLACE FUNCTION migration_tools.build (TEXT) RETURNS VOID AS $$
47     DECLARE
48         migration_schema ALIAS FOR $1;
49         production_tables TEXT[];
50     BEGIN
51         --RAISE INFO 'In migration_tools.build(%)', migration_schema;
52         SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
53         EXECUTE migration_tools.build_base_staging_tables(migration_schema,production_tables);
54     END;
55 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
56
57 CREATE OR REPLACE FUNCTION migration_tools.build_base_staging_tables (TEXT,TEXT[]) RETURNS VOID AS $$
58     DECLARE
59         migration_schema ALIAS FOR $1;
60         production_tables ALIAS FOR $2;
61     BEGIN
62         --RAISE INFO 'In migration_tools.build_base_staging_tables(%,%)', migration_schema, production_tables;
63         FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
64             EXECUTE migration_tools.build_specific_base_staging_table(migration_schema,production_tables[i]);
65         END LOOP;
66     END;
67 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
68
69 CREATE OR REPLACE FUNCTION migration_tools.build_specific_base_staging_table (TEXT,TEXT) RETURNS VOID AS $$
70     DECLARE
71         migration_schema ALIAS FOR $1;
72         production_table ALIAS FOR $2;
73         base_staging_table TEXT;
74         columns RECORD;
75     BEGIN
76         base_staging_table = REPLACE( production_table, '.', '_' );
77         --RAISE INFO 'In migration_tools.build_specific_base_staging_table(%,%) -> %', migration_schema, production_table, base_staging_table;
78         EXECUTE 'CREATE TABLE ' || migration_schema || '.' || base_staging_table || ' ( like ' || production_table || ' including defaults excluding constraints );';
79         EXECUTE '
80             INSERT INTO ' || migration_schema || '.fields_requiring_mapping
81                 SELECT table_schema, table_name, column_name, data_type
82                 FROM information_schema.columns 
83                 WHERE table_schema = ''' || migration_schema || ''' AND table_name = ''' || base_staging_table || ''' AND is_nullable = ''NO'' AND column_default IS NULL;
84         ';
85         FOR columns IN 
86             SELECT table_schema, table_name, column_name, data_type
87             FROM information_schema.columns 
88             WHERE table_schema = migration_schema AND table_name = base_staging_table AND is_nullable = 'NO' AND column_default IS NULL
89         LOOP
90             EXECUTE 'ALTER TABLE ' || columns.table_schema || '.' || columns.table_name || ' ALTER COLUMN ' || columns.column_name || ' DROP NOT NULL;';
91         END LOOP;
92     END;
93 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
94
95 CREATE OR REPLACE FUNCTION migration_tools.insert_base_into_production (TEXT) RETURNS VOID AS $$
96     DECLARE
97         migration_schema ALIAS FOR $1;
98         production_tables TEXT[];
99     BEGIN
100         --RAISE INFO 'In migration_tools.insert_into_production(%)', migration_schema;
101         SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
102         FOR i IN array_lower(production_tables,1) .. array_upper(production_tables,1) LOOP
103             EXECUTE migration_tools.insert_into_production(migration_schema,production_tables[i]);
104         END LOOP;
105     END;
106 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
107
108 CREATE OR REPLACE FUNCTION migration_tools.insert_into_production (TEXT,TEXT) RETURNS VOID AS $$
109     DECLARE
110         migration_schema ALIAS FOR $1;
111         production_table ALIAS FOR $2;
112         base_staging_table TEXT;
113         columns RECORD;
114     BEGIN
115         base_staging_table = REPLACE( production_table, '.', '_' );
116         --RAISE INFO 'In migration_tools.insert_into_production(%,%) -> %', migration_schema, production_table, base_staging_table;
117         EXECUTE 'INSERT INTO ' || production_table || ' SELECT * FROM ' || migration_schema || '.' || base_staging_table || ';';
118     END;
119 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
120
121 CREATE OR REPLACE FUNCTION migration_tools.address_parse_out_citystatezip (TEXT) RETURNS TEXT[] AS $$
122     DECLARE
123         city_state_zip TEXT := $1;
124         city TEXT := '';
125         state TEXT := '';
126         zip TEXT := '';
127     BEGIN
128         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;
129         city_state_zip := REGEXP_REPLACE( city_state_zip, E'^(.*)\\d\\d\\d\\d\\d-?\\d*(.*)$', E'\\1\\2');
130         IF city_state_zip ~ ',' THEN
131             state := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\2');
132             city := REGEXP_REPLACE( city_state_zip, E'^(.*),(.*)$', E'\\1');
133         ELSE
134             IF city_state_zip ~ E'\\s+[A-Z][A-Z]\\s*' THEN
135                 state := REGEXP_REPLACE( city_state_zip, E'^.*,?\\s+([A-Z][A-Z])\\s*.*$', E'\\1' );
136                 city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s+[A-Z][A-Z](\\s*.*)$', E'\\1\\2' );
137             ELSE
138                 IF city_state_zip ~ E'^\\S+$'  THEN
139                     city := city_state_zip;
140                     state := 'N/A';
141                 ELSE
142                     state := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\2');
143                     city := REGEXP_REPLACE( city_state_zip, E'^(.*?),?\\s*(\\S+)\\s*$', E'\\1');
144                 END IF;
145             END IF;
146         END IF;
147         RETURN ARRAY[ TRIM(BOTH ' ' FROM city), TRIM(BOTH ' ' FROM state), TRIM(BOTH ' ' FROM zip) ];
148     END;
149 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
150