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