From 39eb74f8de36bbcd1bbd1e6a70cb8c5d00c82c6c Mon Sep 17 00:00:00 2001 From: zack-rma Date: Thu, 9 Jul 2026 16:41:37 -0700 Subject: [PATCH 1/8] Initial migration script, error handling and constraint handling in progress --- .../updateScripts/migrate_forecast_api.sql | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 schema/src/updateScripts/migrate_forecast_api.sql diff --git a/schema/src/updateScripts/migrate_forecast_api.sql b/schema/src/updateScripts/migrate_forecast_api.sql new file mode 100644 index 00000000..834f74eb --- /dev/null +++ b/schema/src/updateScripts/migrate_forecast_api.sql @@ -0,0 +1,57 @@ +define cwms_schema = CWMS_20 +set define on +set verify off +-------------------------------------------- +-- verify that both the new tables exist -- +-------------------------------------------- +begin + if (select count(*) from ALL_TABLES where table_name = 'AT_FCST_SPEC' and OWNER = cwms_schema) = 0 then + cwms_err.raise('ERROR', 'Expected new forecast tables to exist.'); + end if; +end; +/ +declare +begin + insert into at_fcst_spec + select + s.FORECAST_SPEC_CODE, + cwms_util.GET_OFFICE_CODE(s.source_office), + s.forecast_id, + null, + (select entity_code -- need to handle case where entity is not in at_entity + from at_entity + where entity_id = s.source_agency + and office_code = cwms_util.GET_OFFICE_CODE('CWMS')), + forecast_type + from at_forecast_spec s; + commit; + insert into at_fcst_location + select + s.forecast_spec_code, + s.target_location_code + from at_forecast_spec s; + commit; + insert into at_fcst_time_series + select + s.forecast_spec_code, + s.ts_code + from at_forecast_spec s; + commit; + insert into at_fcst_inst + select + DEFAULT, + s.forecast_spec_code, + t.forecast_date, + (case when t.issue_date is not null then t.issue_date else t.version_date end), + s.max_age, + null, + (select value from at_clob where clob_code = txt.clob_code) + from at_forecast_spec s + join at_forecast_ts t + on t.forecast_spec_code = s.forecast_spec_code + and t.ts_code = s.ts_code + join CWMS_20.at_forecast_text txt + on txt.forecast_spec_code = s.forecast_spec_code + and txt.ts_code = s.ts_code; + commit; +end; \ No newline at end of file From 3f754cc46c79e1fad17a8fbf4d6c762d187d1e3d Mon Sep 17 00:00:00 2001 From: zack-rma Date: Fri, 10 Jul 2026 12:13:05 -0700 Subject: [PATCH 2/8] Updated insertion handling to place locations in table with appropriate linking to spec records. Updated forecast name and designator handling to split concatenated names. --- .../updateScripts/migrate_forecast_api.sql | 75 ++++++++++++++----- 1 file changed, 56 insertions(+), 19 deletions(-) diff --git a/schema/src/updateScripts/migrate_forecast_api.sql b/schema/src/updateScripts/migrate_forecast_api.sql index 834f74eb..475dc286 100644 --- a/schema/src/updateScripts/migrate_forecast_api.sql +++ b/schema/src/updateScripts/migrate_forecast_api.sql @@ -5,31 +5,68 @@ set verify off -- verify that both the new tables exist -- -------------------------------------------- begin - if (select count(*) from ALL_TABLES where table_name = 'AT_FCST_SPEC' and OWNER = cwms_schema) = 0 then + if (select count(*) + from ALL_TABLES + where table_name = 'AT_FCST_SPEC' + and OWNER = cwms_schema) = 0 then cwms_err.raise('ERROR', 'Expected new forecast tables to exist.'); end if; end; / declare + cursor c_forecasts is + select distinct forecast_id, source_office + from at_forecast_spec; + l_forecast_spec_code at_forecast_spec.forecast_spec_code%type; + l_pattern varchar2(10) := '(.+)-(.+)'; + l_sort_order_support number := (select COUNT(*) + from user_tab_columns + where table_name = 'AT_FCST_LOCATION' + and column_name = 'SORT_ORDER'); begin - insert into at_fcst_spec - select - s.FORECAST_SPEC_CODE, - cwms_util.GET_OFFICE_CODE(s.source_office), - s.forecast_id, - null, - (select entity_code -- need to handle case where entity is not in at_entity - from at_entity - where entity_id = s.source_agency - and office_code = cwms_util.GET_OFFICE_CODE('CWMS')), - forecast_type - from at_forecast_spec s; - commit; - insert into at_fcst_location - select - s.forecast_spec_code, - s.target_location_code - from at_forecast_spec s; + for rec in c_forecasts loop + select FORECAST_SPEC_CODE + into l_forecast_spec_code + from at_forecast_spec + where forecast_id = rec.forecast_id + and source_office = rec.source_office + fetch first 1 rows only; + -- insert into new spec table + insert into at_fcst_spec + select + s.FORECAST_SPEC_CODE, + cwms_util.GET_OFFICE_CODE(s.source_office), + nvl(REGEXP_SUBSTR(s.forecast_id, l_pattern, 1, 1, 'i', 1), s.forecast_id), + REGEXP_SUBSTR(s.forecast_id, l_pattern, 1, 1, 'i', 2), + (select entity_code -- need to handle case where entity is not in at_entity + from at_entity + where entity_id = s.source_agency + and office_code = cwms_util.GET_OFFICE_CODE('CWMS')), + forecast_type + from at_forecast_spec s + where s.forecast_id = rec.forecast_id + and s.source_office = rec.source_office + and s.FORECAST_SPEC_CODE = l_forecast_spec_code; + commit; + -- insert into new location table using chosen spec code + for row in ( + select distinct target_location_code + from at_forecast_spec s + where s.forecast_id = rec.forecast_id + and s.source_office = rec.source_office + ) loop + -- check if schema supports new sort order column + if l_sort_order_support = 0 then + insert into at_fcst_location (fcst_spec_code, location_code) + values(l_forecast_spec_code, + row.target_location_code); + else + execute immediate + 'insert into at_fcst_location (fcst_spec_code, location_code, sort_order)' || + 'values (:1, :2, :3)' + using l_forecast_spec_code, row.target_location_code, 0; + end if; + end loop; commit; insert into at_fcst_time_series select From dc40a00c8f1c14099cd1473c7fc74d0e2cd2d172 Mon Sep 17 00:00:00 2001 From: zack-rma Date: Fri, 10 Jul 2026 13:32:19 -0700 Subject: [PATCH 3/8] Code cleanup, update to forecast instance insertion --- .../updateScripts/migrate_forecast_api.sql | 86 ++++++++++++------- 1 file changed, 54 insertions(+), 32 deletions(-) diff --git a/schema/src/updateScripts/migrate_forecast_api.sql b/schema/src/updateScripts/migrate_forecast_api.sql index 475dc286..628938f3 100644 --- a/schema/src/updateScripts/migrate_forecast_api.sql +++ b/schema/src/updateScripts/migrate_forecast_api.sql @@ -1,22 +1,23 @@ -define cwms_schema = CWMS_20 -set define on -set verify off -------------------------------------------- -- verify that both the new tables exist -- -------------------------------------------- +declare + l_schema_support number; begin - if (select count(*) - from ALL_TABLES - where table_name = 'AT_FCST_SPEC' - and OWNER = cwms_schema) = 0 then + select count(*) + into l_schema_support + from ALL_TABLES + where table_name = 'AT_FCST_SPEC' + and OWNER = 'CWMS_20'; + if l_schema_support = 0 then cwms_err.raise('ERROR', 'Expected new forecast tables to exist.'); end if; end; / declare cursor c_forecasts is - select distinct forecast_id, source_office - from at_forecast_spec; + (select distinct forecast_id, source_office + from at_forecast_spec); l_forecast_spec_code at_forecast_spec.forecast_spec_code%type; l_pattern varchar2(10) := '(.+)-(.+)'; l_sort_order_support number := (select COUNT(*) @@ -57,38 +58,59 @@ begin ) loop -- check if schema supports new sort order column if l_sort_order_support = 0 then - insert into at_fcst_location (fcst_spec_code, location_code) + insert into at_fcst_location (fcst_spec_code, primary_location_code) values(l_forecast_spec_code, row.target_location_code); else + -- insert with sort order of 0 execute immediate 'insert into at_fcst_location (fcst_spec_code, location_code, sort_order)' || 'values (:1, :2, :3)' using l_forecast_spec_code, row.target_location_code, 0; end if; end loop; - commit; - insert into at_fcst_time_series - select - s.forecast_spec_code, - s.ts_code - from at_forecast_spec s; - commit; - insert into at_fcst_inst - select - DEFAULT, - s.forecast_spec_code, - t.forecast_date, - (case when t.issue_date is not null then t.issue_date else t.version_date end), - s.max_age, - null, - (select value from at_clob where clob_code = txt.clob_code) - from at_forecast_spec s + commit; + -- insert into new ts table using chosen spec code + for row in ( + select distinct t.ts_code + from at_forecast_spec s join at_forecast_ts t - on t.forecast_spec_code = s.forecast_spec_code - and t.ts_code = s.ts_code - join CWMS_20.at_forecast_text txt - on txt.forecast_spec_code = s.forecast_spec_code - and txt.ts_code = s.ts_code; + on t.forecast_spec_code = s.forecast_spec_code + where s.forecast_id = rec.forecast_id + and s.source_office = rec.source_office + ) loop + insert into at_fcst_time_series + select + l_forecast_spec_code, + row.ts_code + from at_forecast_spec s; + end loop; + commit; + --insert into new fcst_inst table using chosen spec code + for row in ( + select t.forecast_spec_code + from at_forecast_spec s + join at_forecast_ts t + on t.forecast_spec_code = s.forecast_spec_code + where s.forecast_id = rec.forecast_id + and s.source_office = rec.source_office + ) loop + insert into at_fcst_inst + select + DEFAULT, + l_forecast_spec_code, + t.forecast_date, + nvl(t.issue_date, t.version_date), + s.max_age, + null, + (select value from at_clob where clob_code = txt.clob_code) + from at_forecast_spec s + join at_forecast_ts t + on t.forecast_spec_code = row.forecast_spec_code + join CWMS_20.at_forecast_text txt + on txt.forecast_spec_code = row.forecast_spec_code; + end loop; + commit; + end loop; commit; end; \ No newline at end of file From 45bd10dd5fa631ca249e6e30a9782e1c59020c11 Mon Sep 17 00:00:00 2001 From: zack-rma Date: Fri, 10 Jul 2026 14:53:06 -0700 Subject: [PATCH 4/8] Updated blob insertion --- .../updateScripts/migrate_forecast_api.sql | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/schema/src/updateScripts/migrate_forecast_api.sql b/schema/src/updateScripts/migrate_forecast_api.sql index 628938f3..2b5a9afe 100644 --- a/schema/src/updateScripts/migrate_forecast_api.sql +++ b/schema/src/updateScripts/migrate_forecast_api.sql @@ -20,11 +20,19 @@ declare from at_forecast_spec); l_forecast_spec_code at_forecast_spec.forecast_spec_code%type; l_pattern varchar2(10) := '(.+)-(.+)'; - l_sort_order_support number := (select COUNT(*) - from user_tab_columns - where table_name = 'AT_FCST_LOCATION' - and column_name = 'SORT_ORDER'); + l_sort_order_support number; + l_clob_value clob; + l_blob_value blob; + l_warning number; + l_dest_offset number := 1; + l_src_offset number := 1; + l_lang_context number := dbms_lob.default_lang_ctx; begin + select COUNT(*) + into l_sort_order_support + from user_tab_columns + where table_name = 'AT_FCST_LOCATION' + and column_name = 'SORT_ORDER'; for rec in c_forecasts loop select FORECAST_SPEC_CODE into l_forecast_spec_code @@ -95,21 +103,49 @@ begin where s.forecast_id = rec.forecast_id and s.source_office = rec.source_office ) loop + for txt_row in ( + select txt.clob_code, txt.forecast_spec_code + from at_forecast_text txt + where txt.forecast_spec_code = row.forecast_spec_code + ) loop + select value + into l_clob_value + from at_clob + where clob_code = txt_row.clob_code; + dbms_lob.converttoblob(dest_lob => l_blob_value, + src_clob => l_clob_value, + amount => 1, + dest_offset => l_dest_offset, + src_offset => l_src_offset, + blob_csid => dbms_lob.default_csid, + lang_context => l_lang_context, + warning => l_warning + ); + insert into at_fcst_inst select - DEFAULT, + cwms_seq.nextval, l_forecast_spec_code, t.forecast_date, nvl(t.issue_date, t.version_date), s.max_age, null, - (select value from at_clob where clob_code = txt.clob_code) + cwms_t_blob_file( + (select id + from at_clob + where clob_code = txt.clob_code) || '.txt', + 'text/plain', 0, + (select description + from at_clob + where clob_code = txt.clob_code), + l_blob_value) from at_forecast_spec s join at_forecast_ts t on t.forecast_spec_code = row.forecast_spec_code join CWMS_20.at_forecast_text txt on txt.forecast_spec_code = row.forecast_spec_code; end loop; + end loop; commit; end loop; commit; From fd006f0392baeef6a169abbe2d736c8c5bfe8d60 Mon Sep 17 00:00:00 2001 From: zack-rma Date: Fri, 10 Jul 2026 16:04:24 -0700 Subject: [PATCH 5/8] Fix ts insertion --- schema/src/updateScripts/migrate_forecast_api.sql | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/schema/src/updateScripts/migrate_forecast_api.sql b/schema/src/updateScripts/migrate_forecast_api.sql index 2b5a9afe..305588ff 100644 --- a/schema/src/updateScripts/migrate_forecast_api.sql +++ b/schema/src/updateScripts/migrate_forecast_api.sql @@ -21,8 +21,8 @@ declare l_forecast_spec_code at_forecast_spec.forecast_spec_code%type; l_pattern varchar2(10) := '(.+)-(.+)'; l_sort_order_support number; - l_clob_value clob; - l_blob_value blob; + l_clob_value clob := empty_clob(); + l_blob_value blob := empty_blob(); l_warning number; l_dest_offset number := 1; l_src_offset number := 1; @@ -88,10 +88,9 @@ begin and s.source_office = rec.source_office ) loop insert into at_fcst_time_series - select - l_forecast_spec_code, - row.ts_code - from at_forecast_spec s; + values (l_forecast_spec_code, + row.ts_code); + commit; end loop; commit; --insert into new fcst_inst table using chosen spec code @@ -144,6 +143,7 @@ begin on t.forecast_spec_code = row.forecast_spec_code join CWMS_20.at_forecast_text txt on txt.forecast_spec_code = row.forecast_spec_code; + commit; end loop; end loop; commit; From 5d287c9e7234d160ce8b13cdc41402fe3cfdc5a7 Mon Sep 17 00:00:00 2001 From: zack-rma Date: Fri, 10 Jul 2026 17:37:06 -0700 Subject: [PATCH 6/8] Forecast instance insertion updates --- schema/src/updateScripts/migrate_forecast_api.sql | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/schema/src/updateScripts/migrate_forecast_api.sql b/schema/src/updateScripts/migrate_forecast_api.sql index 305588ff..a9ee0d25 100644 --- a/schema/src/updateScripts/migrate_forecast_api.sql +++ b/schema/src/updateScripts/migrate_forecast_api.sql @@ -111,6 +111,7 @@ begin into l_clob_value from at_clob where clob_code = txt_row.clob_code; + dbms_lob.createtemporary(l_blob_value, true); dbms_lob.converttoblob(dest_lob => l_blob_value, src_clob => l_clob_value, amount => 1, @@ -126,7 +127,7 @@ begin cwms_seq.nextval, l_forecast_spec_code, t.forecast_date, - nvl(t.issue_date, t.version_date), + nvl(t.version_date, t.issue_date), s.max_age, null, cwms_t_blob_file( @@ -140,9 +141,12 @@ begin l_blob_value) from at_forecast_spec s join at_forecast_ts t - on t.forecast_spec_code = row.forecast_spec_code + on t.forecast_spec_code = s.forecast_spec_code join CWMS_20.at_forecast_text txt - on txt.forecast_spec_code = row.forecast_spec_code; + on txt.forecast_spec_code = s.forecast_spec_code + where s.forecast_id = rec.forecast_id + and s.source_office = rec.source_office + and txt.clob_code = txt_row.clob_code; commit; end loop; end loop; From cf7b26344eed265bd853cb8ec773cbff0f72451a Mon Sep 17 00:00:00 2001 From: zack-rma Date: Fri, 17 Jul 2026 10:20:07 -0700 Subject: [PATCH 7/8] Updated migration script per feedback. Still ironing out constraint conflicts for TS migration --- .../updateScripts/migrate_forecast_api.sql | 259 +++++++++--------- 1 file changed, 137 insertions(+), 122 deletions(-) diff --git a/schema/src/updateScripts/migrate_forecast_api.sql b/schema/src/updateScripts/migrate_forecast_api.sql index a9ee0d25..6e04b97b 100644 --- a/schema/src/updateScripts/migrate_forecast_api.sql +++ b/schema/src/updateScripts/migrate_forecast_api.sql @@ -4,6 +4,7 @@ declare l_schema_support number; begin + -- verify that the new tables exist select count(*) into l_schema_support from ALL_TABLES @@ -15,141 +16,155 @@ begin end; / declare - cursor c_forecasts is - (select distinct forecast_id, source_office - from at_forecast_spec); - l_forecast_spec_code at_forecast_spec.forecast_spec_code%type; - l_pattern varchar2(10) := '(.+)-(.+)'; l_sort_order_support number; - l_clob_value clob := empty_clob(); - l_blob_value blob := empty_blob(); - l_warning number; - l_dest_offset number := 1; - l_src_offset number := 1; - l_lang_context number := dbms_lob.default_lang_ctx; + l_new_spec at_fcst_spec%rowtype; + l_new_inst at_fcst_inst%rowtype; + l_clob clob; + l_blob blob; + l_src_offset integer; + l_dst_offset integer; + l_lang_ctx integer; + l_warning integer; + l_ts_row at_forecast_ts%rowtype; + l_ts_start date; + l_ts_end date; + l_count number; begin + -- determine which forecast location table schema is available select COUNT(*) into l_sort_order_support from user_tab_columns where table_name = 'AT_FCST_LOCATION' and column_name = 'SORT_ORDER'; - for rec in c_forecasts loop - select FORECAST_SPEC_CODE - into l_forecast_spec_code - from at_forecast_spec - where forecast_id = rec.forecast_id - and source_office = rec.source_office - fetch first 1 rows only; - -- insert into new spec table - insert into at_fcst_spec - select - s.FORECAST_SPEC_CODE, - cwms_util.GET_OFFICE_CODE(s.source_office), - nvl(REGEXP_SUBSTR(s.forecast_id, l_pattern, 1, 1, 'i', 1), s.forecast_id), - REGEXP_SUBSTR(s.forecast_id, l_pattern, 1, 1, 'i', 2), - (select entity_code -- need to handle case where entity is not in at_entity - from at_entity - where entity_id = s.source_agency - and office_code = cwms_util.GET_OFFICE_CODE('CWMS')), - forecast_type - from at_forecast_spec s - where s.forecast_id = rec.forecast_id - and s.source_office = rec.source_office - and s.FORECAST_SPEC_CODE = l_forecast_spec_code; + -- loop through all forecasts + for old_spec in (select * from at_forecast_spec) loop + -- create new forecast spec code + l_new_spec.fcst_spec_code := cwms_seq.nextval; + ----------------------- + -- populate new spec -- + ----------------------- + select bl.db_office_code + into l_new_spec.office_code + from at_base_location bl, + at_physical_location pl + where pl.location_code = old_spec.target_location_code + and bl.base_location_code = pl.base_location_code; + l_new_spec.fcst_spec_id := old_spec.forecast_id; + l_new_spec.fcst_designator := old_spec.forecast_type; + if l_new_spec.fcst_designator is not null then + l_new_spec.fcst_designator := l_new_spec.fcst_designator||'-'; + end if; + l_new_spec.fcst_designator := l_new_spec.fcst_designator||cwms_loc.get_location_id(old_spec.target_location_code); + case old_spec.source_agency + when 'USACE' then + select entity_code + into l_new_spec.source_entity + from at_entity + where entity_id = 'CE'||old_spec.source_office; + end case; + insert into at_fcst_spec values l_new_spec; commit; - -- insert into new location table using chosen spec code - for row in ( - select distinct target_location_code - from at_forecast_spec s - where s.forecast_id = rec.forecast_id - and s.source_office = rec.source_office - ) loop - -- check if schema supports new sort order column - if l_sort_order_support = 0 then - insert into at_fcst_location (fcst_spec_code, primary_location_code) - values(l_forecast_spec_code, - row.target_location_code); - else - -- insert with sort order of 0 - execute immediate - 'insert into at_fcst_location (fcst_spec_code, location_code, sort_order)' || - 'values (:1, :2, :3)' - using l_forecast_spec_code, row.target_location_code, 0; - end if; - end loop; - commit; - -- insert into new ts table using chosen spec code - for row in ( - select distinct t.ts_code - from at_forecast_spec s - join at_forecast_ts t - on t.forecast_spec_code = s.forecast_spec_code - where s.forecast_id = rec.forecast_id - and s.source_office = rec.source_office - ) loop - insert into at_fcst_time_series - values (l_forecast_spec_code, - row.ts_code); - commit; - end loop; - commit; - --insert into new fcst_inst table using chosen spec code - for row in ( - select t.forecast_spec_code - from at_forecast_spec s - join at_forecast_ts t - on t.forecast_spec_code = s.forecast_spec_code - where s.forecast_id = rec.forecast_id - and s.source_office = rec.source_office - ) loop - for txt_row in ( - select txt.clob_code, txt.forecast_spec_code - from at_forecast_text txt - where txt.forecast_spec_code = row.forecast_spec_code - ) loop - select value - into l_clob_value + -------------------------- + -- populate new inst(s) -- + -------------------------- + for old_ts in (select distinct forecast_date, + issue_date + from at_forecast_ts + where forecast_spec_code = old_spec.forecast_spec_code + ) + loop + if (old_ts.issue_date != old_ts.forecast_date) then + select * + into l_ts_row + from at_forecast_ts + where forecast_spec_code = old_spec.forecast_spec_code + and forecast_date = old_ts.forecast_date + and issue_date = old_ts.issue_date + fetch next 1 row only; + select start_date, END_DATE + into l_ts_start, l_ts_end + from av_tsv + where ts_code = l_ts_row.ts_code + and version_date = l_ts_row.version_date + fetch next 1 row only; + cwms_ts.CHANGE_VERSION_DATE(l_ts_row.ts_code, l_ts_row.version_date, old_ts.issue_date, l_ts_start, l_ts_end); + end if; + l_new_inst.fcst_inst_code := cwms_seq.nextval; + l_new_inst.fcst_spec_code := l_new_spec.fcst_spec_code; + l_new_inst.fcst_date_time := old_ts.forecast_date; + l_new_inst.issue_date_time := old_ts.issue_date; + l_new_inst.max_age := old_spec.max_age; + begin + select value + into l_clob from at_clob - where clob_code = txt_row.clob_code; - dbms_lob.createtemporary(l_blob_value, true); - dbms_lob.converttoblob(dest_lob => l_blob_value, - src_clob => l_clob_value, - amount => 1, - dest_offset => l_dest_offset, - src_offset => l_src_offset, - blob_csid => dbms_lob.default_csid, - lang_context => l_lang_context, - warning => l_warning - ); + where clob_code = (select clob_code + from at_forecast_text + where forecast_spec_code = old_spec.forecast_spec_code + and forecast_date = old_ts.forecast_date + and issue_date = old_ts.issue_date + ); + exception + when no_data_found then l_clob := null; + end; + if l_clob is not null then + dbms_lob.createtemporary(l_blob, true); + l_src_offset := 1; + l_dst_offset := 1; + l_lang_ctx := dbms_lob.default_lang_ctx; - insert into at_fcst_inst - select - cwms_seq.nextval, - l_forecast_spec_code, - t.forecast_date, - nvl(t.version_date, t.issue_date), - s.max_age, - null, - cwms_t_blob_file( - (select id - from at_clob - where clob_code = txt.clob_code) || '.txt', - 'text/plain', 0, - (select description - from at_clob - where clob_code = txt.clob_code), - l_blob_value) - from at_forecast_spec s - join at_forecast_ts t - on t.forecast_spec_code = s.forecast_spec_code - join CWMS_20.at_forecast_text txt - on txt.forecast_spec_code = s.forecast_spec_code - where s.forecast_id = rec.forecast_id - and s.source_office = rec.source_office - and txt.clob_code = txt_row.clob_code; + dbms_lob.converttoblob( + dest_lob => l_blob, + src_clob => l_clob, + amount => dbms_lob.lobmaxsize, + dest_offset => l_dst_offset, + src_offset => l_src_offset, + blob_csid => dbms_lob.default_csid, + lang_context => l_lang_ctx, + warning => l_warning); + + l_new_inst.blob_file := blob_file_t( + filename => 'forecast.txt', + media_type => 'text/plain', + quality_code => null, + the_blob => l_blob); + end if; + insert into at_fcst_inst values (l_new_inst); commit; end loop; + ------------------------------ + -- insert into new ts table -- + ------------------------------ + for ts_row in ( + select distinct t.ts_code + from at_forecast_ts t + where t.forecast_spec_code = old_spec.forecast_spec_code + ) loop + select count(*) + into l_count + from at_fcst_time_series ts + where ts.fcst_spec_code = l_new_spec.fcst_spec_code + and ts.ts_code = ts_row.ts_code; + if l_count = 0 then + insert into at_fcst_time_series + values (l_new_spec.fcst_spec_code, + ts_row.ts_code); + commit; + end if; end loop; + --------------------------- + -- populate new location -- + --------------------------- + -- check if schema supports new sort order column + if l_sort_order_support = 0 then + insert into at_fcst_location values (l_new_spec.fcst_spec_code, old_spec.target_location_code); + else + -- insert with sort order of 0 + execute immediate + 'insert into at_fcst_location (fcst_spec_code, location_code, sort_order)' || + 'values (:1, :2, :3)' + using l_new_spec.fcst_spec_code, old_spec.target_location_code, 0; + end if; commit; end loop; commit; From cfc5fb3723f246235a153fe62401ac2b07e73f5d Mon Sep 17 00:00:00 2001 From: zack-rma Date: Fri, 17 Jul 2026 15:52:10 -0700 Subject: [PATCH 8/8] Resolved time series constraint conflict, tested locally. --- schema/src/updateScripts/migrate_forecast_api.sql | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/schema/src/updateScripts/migrate_forecast_api.sql b/schema/src/updateScripts/migrate_forecast_api.sql index 6e04b97b..628efa3f 100644 --- a/schema/src/updateScripts/migrate_forecast_api.sql +++ b/schema/src/updateScripts/migrate_forecast_api.sql @@ -81,12 +81,10 @@ begin and forecast_date = old_ts.forecast_date and issue_date = old_ts.issue_date fetch next 1 row only; - select start_date, END_DATE + select min(start_date), max(END_DATE) into l_ts_start, l_ts_end from av_tsv - where ts_code = l_ts_row.ts_code - and version_date = l_ts_row.version_date - fetch next 1 row only; + where ts_code = l_ts_row.ts_code; cwms_ts.CHANGE_VERSION_DATE(l_ts_row.ts_code, l_ts_row.version_date, old_ts.issue_date, l_ts_start, l_ts_end); end if; l_new_inst.fcst_inst_code := cwms_seq.nextval; @@ -129,7 +127,7 @@ begin quality_code => null, the_blob => l_blob); end if; - insert into at_fcst_inst values (l_new_inst); + insert into at_fcst_inst values l_new_inst; commit; end loop; ------------------------------