Skip to content

Commit

Permalink
update stored procedure logic
Browse files Browse the repository at this point in the history
  • Loading branch information
agnessnowplow committed Feb 22, 2022
1 parent afdfa17 commit 0c270c3
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,13 @@ CREATE OR REPLACE PROCEDURE {{.output_schema}}.column_check(SOURCE_SCHEMA VARCHA
WHERE t.ordinal_position>= s.ordinal_position),
type_check AS (
SELECT SUM(CASE WHEN s.column_name IS NULL THEN 1 ELSE 0 END) AS missing_in_source,
SUM(CASE WHEN t.column_name IS NULL THEN 1 ELSE 0 END) AS missing_in_target
SELECT SUM(CASE WHEN s.column_name IS NULL AND ifnull(s.ordinal_position, 0) <= m.target_cols THEN 1 ELSE 0 END) AS missing_in_source,
SUM(CASE WHEN ifnull(s.ordinal_position,0) <= m.target_cols AND s.ordinal_position <> t.ordinal_position THEN 1 ELSE 0 END) AS missing_in_target
FROM target_data t
FULL OUTER JOIN SOURCE_data s
ON t.ordinal_position = s.ordinal_position AND T.data_type = s.data_type AND s.column_name = t.column_name
WHERE t.ordinal_position>= s.ordinal_position OR s.ordinal_position is null),
ON T.data_type = s.data_type AND t.column_name = s.column_name
LEFT JOIN (SELECT max(ordinal_position) target_cols FROM target_data) m
),
columns_to_add AS (
SELECT listagg(CASE WHEN t.ordinal_position IS NULL THEN s.column_definition end
Expand All @@ -153,15 +154,15 @@ CREATE OR REPLACE PROCEDURE {{.output_schema}}.column_check(SOURCE_SCHEMA VARCHA
cols_with_varchar_issue = res.getColumnValue(3);
cols_to_add = res.getColumnValue(4);

if (missing_in_target > 1) {
if (missing_in_source !== 0) {
throw "ERROR: Source table is missing column(s) which exist in target table.";
}

if (cols_with_varchar_issue !== '') {
throw "ERROR: field length for source varchar column(s) " + cols_with_varchar_issue + " is longer than the target."
}

if (missing_in_source > 0) {
if (missing_in_target !== 0) {
throw "ERROR: Can only migrate extra columns of the end of source"
}

Expand Down

0 comments on commit 0c270c3

Please sign in to comment.