(no commit message)
[migration-tools.git] / sql / base / base.sql
index 0882711..539035f 100644 (file)
@@ -4,12 +4,12 @@
 -- DROP SCHEMA foo CASCADE; CREATE SCHEMA foo; 
 -- \i base.sql
 -- SELECT migration_tools.init('foo');
--- SELECT migration_tools.build_default_base_staging_tables('foo');
+-- SELECT migration_tools.build('foo');
 -- SELECT * FROM foo.fields_requiring_mapping;
 -- \d foo.actor_usr
 -- create some incoming ILS specific staging tables, like CREATE foo.legacy_items ( l_barcode TEXT, .. ) INHERITS foo.asset_copy;
 -- Do some mapping, like UPDATE foo.legacy_items SET barcode = TRIM(BOTH ' ' FROM l_barcode);
--- Then, to move into production, do: select migration_tools.insert_default_into_production('foo')
+-- Then, to move into production, do: select migration_tools.insert_base_into_production('foo')
 
 CREATE SCHEMA migration_tools;
 
@@ -30,20 +30,25 @@ CREATE OR REPLACE FUNCTION migration_tools.init (TEXT) RETURNS VOID AS $$
     DECLARE
         migration_schema ALIAS FOR $1;
     BEGIN
-        EXECUTE 'CREATE SCHEMA ' || migration_schema || ';';
+        EXECUTE 'DROP TABLE IF EXISTS ' || migration_schema || '.config;';
         EXECUTE 'CREATE TABLE ' || migration_schema || '.config ( key TEXT UNIQUE, value TEXT);';
         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'' );';
         EXECUTE 'DROP TABLE IF EXISTS ' || migration_schema || '.fields_requiring_mapping;';
         EXECUTE 'CREATE TABLE ' || migration_schema || '.fields_requiring_mapping( table_schema TEXT, table_name TEXT, column_name TEXT, data_type TEXT);';
+        BEGIN
+            EXECUTE 'INSERT INTO ' || migration_schema || '.config (key,value) VALUES ( ''last_init'', now() );';
+        EXCEPTION
+            WHEN OTHERS THEN EXECUTE 'UPDATE ' || migration_schema || '.config SET value = now() WHERE key = ''last_init'';';
+        END;
     END;
 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
 
-CREATE OR REPLACE FUNCTION migration_tools.build_default_base_staging_tables (TEXT) RETURNS VOID AS $$
+CREATE OR REPLACE FUNCTION migration_tools.build (TEXT) RETURNS VOID AS $$
     DECLARE
         migration_schema ALIAS FOR $1;
         production_tables TEXT[];
     BEGIN
-        --RAISE INFO 'In migration_tools.build_default_base_staging_tables(%)', migration_schema;
+        --RAISE INFO 'In migration_tools.build(%)', migration_schema;
         SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables;
         EXECUTE migration_tools.build_base_staging_tables(migration_schema,production_tables);
     END;
@@ -87,7 +92,7 @@ CREATE OR REPLACE FUNCTION migration_tools.build_specific_base_staging_table (TE
     END;
 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
 
-CREATE OR REPLACE FUNCTION migration_tools.insert_default_into_production (TEXT) RETURNS VOID AS $$
+CREATE OR REPLACE FUNCTION migration_tools.insert_base_into_production (TEXT) RETURNS VOID AS $$
     DECLARE
         migration_schema ALIAS FOR $1;
         production_tables TEXT[];
@@ -143,3 +148,15 @@ CREATE OR REPLACE FUNCTION migration_tools.address_parse_out_citystatezip (TEXT)
     END;
 $$ LANGUAGE PLPGSQL STRICT VOLATILE;
 
+CREATE OR REPLACE FUNCTION migration_tools.rebarcode (o TEXT, t BIGINT) RETURNS TEXT AS $$
+    DECLARE
+        n TEXT := o;
+    BEGIN
+        IF o ~ E'^\\d+$' THEN
+            n = o::INT + t;
+        END IF;
+
+        RETURN n;
+    END;
+$$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
+