Skip to content

Commit

Permalink
Merge pull request #64 from elemoine/ele_fix-migration
Browse files Browse the repository at this point in the history
Fixes to the migration script
  • Loading branch information
3nids authored Feb 12, 2018
2 parents f2bb89c + 3623a22 commit d199cda
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion scripts/dispatch_table_migration/migrate_dispatch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ else
echo " ${CWARN} warnings"
echo " ${CNOT} notices (columns skip or rename)."
echo "**************"
cat migration2.log | ${GP}egrep 'WARNING:'
cat migration2.log | ${GP}egrep 'WARNING:' || /bin/true
echo "**************"
cat migration2.log | ${GP}egrep '[^0]\d* elements' | ${GP}sed 's/INFO:\s*/ /'
echo "**************"
Expand Down
29 changes: 16 additions & 13 deletions scripts/dispatch_table_migration/migrate_dispatch_copy_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CREATE FUNCTION pg_temp.handle_missing_column(_source_columns text,
_drop_column bool,
_destination_table_name text,
_destination_column text,
_source_table_name text,
_source_column text default '' )
RETURNS TEXT AS
$func$
Expand All @@ -20,7 +21,7 @@ BEGIN
WHERE table_schema ='qgep'
AND table_name = '%1$I'
AND column_name = '%2$I'
$$, _destination_table_name, _destination_column ) INTO _column_exists;
$$, _source_table_name, _destination_column ) INTO _column_exists;
IF _column_exists = 0 THEN
IF _drop_column THEN
RAISE NOTICE '%', format('Handle missing column with drop: %1$I.%2$I', _destination_table_name, _destination_column);
Expand Down Expand Up @@ -53,6 +54,7 @@ DECLARE
_array_pos int;
_sequence_exists int;
_sequence_name text;
_sequence_name_fully_qualified text;
_sequence_value bigint;
_loop_count int;
BEGIN
Expand Down Expand Up @@ -117,22 +119,22 @@ BEGIN
-- handle missing schema update
_ordered_columns_source := _ordered_columns_dest;
IF _destination_table_name IN ('catchment_area_text') THEN
SELECT pg_temp.handle_missing_column(_ordered_columns_source, FALSE, _destination_table_name, 'fk_catchment_area', 'fk_catchment') INTO _ordered_columns_source;
SELECT pg_temp.handle_missing_column(_ordered_columns_source, FALSE, _destination_table_name, 'fk_catchment_area', r.table_name, 'fk_catchment') INTO _ordered_columns_source;
END IF;
IF _destination_table_name IN ('hq_relation', 'hydraulic_char_data', 'overflow') THEN
SELECT pg_temp.handle_missing_column(_ordered_columns_source, FALSE, _destination_table_name, 'fk_overflow_characteristic', 'fk_overflow_char') INTO _ordered_columns_source;
SELECT pg_temp.handle_missing_column(_ordered_columns_source, FALSE, _destination_table_name, 'fk_overflow_characteristic', r.table_name, 'fk_overflow_char') INTO _ordered_columns_source;
END IF;
IF _destination_table_name IN ('txt_symbol', 'txt_text') THEN
SELECT pg_temp.handle_missing_column(_ordered_columns_source, TRUE, _destination_table_name, 'fk_wastewater_structure') INTO _ordered_columns_source;
SELECT pg_temp.handle_missing_column(_ordered_columns_source, TRUE, _destination_table_name, 'fk_wastewater_structure', r.table_name) INTO _ordered_columns_source;
END IF;
IF _destination_table_name IN ('txt_text') THEN
SELECT pg_temp.handle_missing_column(_ordered_columns_source, TRUE, _destination_table_name, 'fk_catchment_area') INTO _ordered_columns_source;
SELECT pg_temp.handle_missing_column(_ordered_columns_source, TRUE, _destination_table_name, 'fk_reach') INTO _ordered_columns_source;
SELECT pg_temp.handle_missing_column(_ordered_columns_source, TRUE, _destination_table_name, 'fk_catchment_area', r.table_name) INTO _ordered_columns_source;
SELECT pg_temp.handle_missing_column(_ordered_columns_source, TRUE, _destination_table_name, 'fk_reach', r.table_name) INTO _ordered_columns_source;
END IF;
IF r.table_name LIKE 'vl_%' THEN
SELECT pg_temp.handle_missing_column(_ordered_columns_source, TRUE, _destination_table_name, 'value_it') INTO _ordered_columns_source;
SELECT pg_temp.handle_missing_column(_ordered_columns_source, TRUE, _destination_table_name, 'short_it') INTO _ordered_columns_source;
SELECT pg_temp.handle_missing_column(_ordered_columns_source, TRUE, _destination_table_name, 'abbr_it') INTO _ordered_columns_source;
SELECT pg_temp.handle_missing_column(_ordered_columns_source, TRUE, _destination_table_name, 'value_it', r.table_name) INTO _ordered_columns_source;
SELECT pg_temp.handle_missing_column(_ordered_columns_source, TRUE, _destination_table_name, 'short_it', r.table_name) INTO _ordered_columns_source;
SELECT pg_temp.handle_missing_column(_ordered_columns_source, TRUE, _destination_table_name, 'abbr_it', r.table_name) INTO _ordered_columns_source;
END IF;

-- Do the copy
Expand Down Expand Up @@ -162,7 +164,8 @@ BEGIN
EXECUTE format('INSERT INTO %1$I.%2$I (%5$s) (SELECT %4$s FROM qgep.%3$I);', _destination_schema_name, _destination_table_name, r.table_name, _ordered_columns_source, _ordered_columns_dest);
GET DIAGNOSTICS rc = ROW_COUNT;
RAISE INFO '% %: % elements copied', _destination_schema_name, _destination_table_name, rc;
_sequence_name := format('qgep.seq_%1$I_oid', r.table_name);
_sequence_name := format('seq_%1$I_oid', r.table_name);
_sequence_name_fully_qualified := format('qgep.%1$I', _sequence_name);
-- handle renamed sequences
EXECUTE format($$
SELECT COUNT(*)
Expand All @@ -171,11 +174,11 @@ BEGIN
AND sequence_name = '%1$I'
$$, _sequence_name ) INTO _sequence_exists;
IF _sequence_exists = 0 THEN
_sequence_name := replace(_sequence_name,'qgep.seq_od_hydraulic_char_data_oid','qgep.seq_od_hydraulic_characteristic_data_oid');
_sequence_name := replace(_sequence_name,'qgep.seq_od_overflow_char_oid','qgep.seq_od_overflow_characteristic_oid');
_sequence_name_fully_qualified := replace(_sequence_name_fully_qualified,'qgep.seq_od_hydraulic_char_data_oid','qgep.seq_od_hydraulic_characteristic_data_oid');
_sequence_name_fully_qualified := replace(_sequence_name_fully_qualified,'qgep.seq_od_overflow_char_oid','qgep.seq_od_overflow_characteristic_oid');
END IF;
-- update sequence
SELECT nextval(_sequence_name) INTO _sequence_value;
SELECT nextval(_sequence_name_fully_qualified) INTO _sequence_value;
EXECUTE format('SELECT setval(''%1$I.seq_%2$I_oid'', %3$s, FALSE);', _destination_schema_name, _destination_table_name, _sequence_value);
END IF;
END LOOP;
Expand Down

0 comments on commit d199cda

Please sign in to comment.