X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=blobdiff_plain;f=sql%2Fbase%2F00-infrastructure.sql;fp=sql%2Fbase%2F00-infrastructure.sql;h=4bd25cf7f30cf26f35e8f537f75738ade14ce69f;hp=c0953c6285c7b05146aaa47d6041ed1b6bae08f0;hb=2fefcf603c66dc442489e554a4b72733a1f1803f;hpb=5b3e8ef51110574c502303ea6f7070458022c1f2 diff --git a/sql/base/00-infrastructure.sql b/sql/base/00-infrastructure.sql index c0953c6..4bd25cf 100644 --- a/sql/base/00-infrastructure.sql +++ b/sql/base/00-infrastructure.sql @@ -62,13 +62,13 @@ CREATE OR REPLACE FUNCTION migration_tools.build (TEXT) RETURNS VOID AS $$ BEGIN SELECT migration_tools.production_tables(migration_schema) INTO STRICT production_tables; PERFORM migration_tools.build_base_staging_tables(migration_schema,production_tables); - EXECUTE 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_barcode_key ON ' || migration_schema || '.actor_card ( barcode );'; - EXECUTE 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_usrname_key ON ' || migration_schema || '.actor_usr ( usrname );'; - EXECUTE 'CREATE UNIQUE INDEX ' || migration_schema || '_copy_barcode_key ON ' || migration_schema || '.asset_copy ( barcode );'; - EXECUTE 'CREATE UNIQUE INDEX ' || migration_schema || '_copy_id_key ON ' || migration_schema || '.asset_copy ( id );'; - EXECUTE 'CREATE INDEX ' || migration_schema || '_callnum_record_idx ON ' || migration_schema || '.asset_call_number ( record );'; - EXECUTE 'CREATE INDEX ' || migration_schema || '_callnum_upper_label_id_lib_idx ON ' || migration_schema || '.asset_call_number ( UPPER(label),id,owning_lib );'; - EXECUTE 'CREATE UNIQUE INDEX ' || migration_schema || '_callnum_label_once_per_lib ON ' || migration_schema || '.asset_call_number ( record,owning_lib,label,prefix,suffix );'; + EXECUTE 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_barcode_key ON ' || migration_schema || '.m_actor_card ( barcode );'; + EXECUTE 'CREATE UNIQUE INDEX ' || migration_schema || '_patron_usrname_key ON ' || migration_schema || '.m_actor_usr ( usrname );'; + EXECUTE 'CREATE UNIQUE INDEX ' || migration_schema || '_copy_barcode_key ON ' || migration_schema || '.m_asset_copy ( barcode );'; + EXECUTE 'CREATE UNIQUE INDEX ' || migration_schema || '_copy_id_key ON ' || migration_schema || '.m_asset_copy ( id );'; + EXECUTE 'CREATE INDEX ' || migration_schema || '_callnum_record_idx ON ' || migration_schema || '.m_asset_call_number ( record );'; + EXECUTE 'CREATE INDEX ' || migration_schema || '_callnum_upper_label_id_lib_idx ON ' || migration_schema || '.m_asset_call_number ( UPPER(label),id,owning_lib );'; + EXECUTE 'CREATE UNIQUE INDEX ' || migration_schema || '_callnum_label_once_per_lib ON ' || migration_schema || '.m_asset_call_number ( record,owning_lib,label,prefix,suffix );'; END; $$ LANGUAGE PLPGSQL STRICT VOLATILE; @@ -90,7 +90,7 @@ CREATE OR REPLACE FUNCTION migration_tools.build_specific_base_staging_table (TE base_staging_table TEXT; columns RECORD; BEGIN - base_staging_table = REPLACE( production_table, '.', '_' ); + base_staging_table = REPLACE( 'm_' || production_table, '.', '_' ); EXECUTE 'CREATE TABLE ' || migration_schema || '.' || base_staging_table || ' ( LIKE ' || production_table || ' INCLUDING DEFAULTS EXCLUDING CONSTRAINTS );'; FOR columns IN SELECT table_schema, table_name, column_name, data_type @@ -115,7 +115,7 @@ CREATE OR REPLACE FUNCTION migration_tools.create_linked_legacy_table_from (TEXT BEGIN create_sql := 'CREATE TABLE ' || migration_schema || '.' || parent_table || '_legacy ( '; FOR columns IN - SELECT table_schema, table_name, column_name, data_type + SELECT table_schema, table_name, column_name, data_type, numeric_precision, numeric_scale FROM information_schema.columns WHERE table_schema = migration_schema AND table_name = source_table LOOP @@ -127,6 +127,8 @@ CREATE OR REPLACE FUNCTION migration_tools.create_linked_legacy_table_from (TEXT create_sql := create_sql || columns.column_name || ' '; if columns.data_type = 'ARRAY' then create_sql := create_sql || 'TEXT[]'; + elsif columns.data_type = 'numeric' then + create_sql := create_sql || 'NUMERIC('||columns.numeric_precision||','||columns.numeric_scale||')'; else create_sql := create_sql || columns.data_type; end if; @@ -141,3 +143,67 @@ CREATE OR REPLACE FUNCTION migration_tools.create_linked_legacy_table_from (TEXT END; $$ LANGUAGE PLPGSQL STRICT VOLATILE; +-- creates other child table so you can have more than one child table in a schema from a base table +CREATE OR REPLACE FUNCTION migration_tools.build_variant_staging_table(text, text, text) + RETURNS void + LANGUAGE plpgsql + STRICT +AS $function$ + DECLARE + migration_schema ALIAS FOR $1; + production_table ALIAS FOR $2; + base_staging_table ALIAS FOR $3; + columns RECORD; + BEGIN + --RAISE INFO 'In migration_tools.build_specific_base_staging_table(%,%) -> %', migration_schema, production_table, base_staging_table; + EXECUTE 'CREATE TABLE ' || migration_schema || '.' || base_staging_table || ' ( LIKE ' || production_table || ' INCLUDING DEFAULTS EXCLUDING CONSTRAINTS );'; + EXECUTE ' + INSERT INTO ' || migration_schema || '.fields_requiring_mapping + SELECT table_schema, table_name, column_name, data_type + FROM information_schema.columns + WHERE table_schema = ''' || migration_schema || ''' AND table_name = ''' || base_staging_table || ''' AND is_nullable = ''NO'' AND column_default IS NULL; + '; + FOR columns IN + SELECT table_schema, table_name, column_name, data_type + FROM information_schema.columns + WHERE table_schema = migration_schema AND table_name = base_staging_table AND is_nullable = 'NO' AND column_default IS NULL + LOOP + EXECUTE 'ALTER TABLE ' || columns.table_schema || '.' || columns.table_name || ' ALTER COLUMN ' || columns.column_name || ' DROP NOT NULL;'; + END LOOP; + END; +$function$; + +CREATE OR REPLACE FUNCTION migration_tools.assert (BOOLEAN) RETURNS VOID AS $$ + DECLARE + test ALIAS FOR $1; + BEGIN + IF NOT test THEN + RAISE EXCEPTION 'assertion'; + END IF; + END; +$$ LANGUAGE PLPGSQL STRICT VOLATILE; + +CREATE OR REPLACE FUNCTION migration_tools.assert (BOOLEAN,TEXT) RETURNS VOID AS $$ + DECLARE + test ALIAS FOR $1; + msg ALIAS FOR $2; + BEGIN + IF NOT test THEN + RAISE EXCEPTION '%', msg; + END IF; + END; +$$ LANGUAGE PLPGSQL STRICT VOLATILE; + +CREATE OR REPLACE FUNCTION migration_tools.assert (BOOLEAN,TEXT,TEXT) RETURNS TEXT AS $$ + DECLARE + test ALIAS FOR $1; + fail_msg ALIAS FOR $2; + success_msg ALIAS FOR $3; + BEGIN + IF NOT test THEN + RAISE EXCEPTION '%', fail_msg; + END IF; + RETURN success_msg; + END; +$$ LANGUAGE PLPGSQL STRICT VOLATILE; +