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