Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed May 3, 2024
1 parent 40722b3 commit c1f444e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
8 changes: 4 additions & 4 deletions lib/destination/dml/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (m *MergeArgument) buildRedshiftInsertQuery(columns []columns.Column) strin
)
}

func (m *MergeArgument) buildRedshiftUpdateQuery(columns []columns.Column, softDelete bool) string {
func (m *MergeArgument) buildRedshiftUpdateQuery(columns []columns.Column) string {
stringBuilder := strings.Builder{}

stringBuilder.WriteString(fmt.Sprintf(`UPDATE %s as c SET %s FROM %s as cc WHERE %s`,
Expand All @@ -114,7 +114,7 @@ func (m *MergeArgument) buildRedshiftUpdateQuery(columns []columns.Column, softD
stringBuilder.WriteString(fmt.Sprintf(" AND cc.%s >= c.%s", m.IdempotentKey, m.IdempotentKey))
}

if !softDelete {
if !m.SoftDelete {
stringBuilder.WriteString(fmt.Sprintf(" AND COALESCE(cc.%s, false) = false", m.Dialect.QuoteIdentifier(constants.DeleteColumnMarker)))
}

Expand Down Expand Up @@ -158,7 +158,7 @@ func (m *MergeArgument) GetParts() ([]string, error) {
if m.SoftDelete {
return []string{
m.buildRedshiftInsertQuery(m.Columns),
m.buildRedshiftUpdateQuery(m.Columns, true),
m.buildRedshiftUpdateQuery(m.Columns),
}, nil
}

Expand All @@ -170,7 +170,7 @@ func (m *MergeArgument) GetParts() ([]string, error) {

parts := []string{
m.buildRedshiftInsertQuery(columns),
m.buildRedshiftUpdateQuery(columns, false),
m.buildRedshiftUpdateQuery(columns),
}

if *m.ContainsHardDeletes {
Expand Down
42 changes: 29 additions & 13 deletions lib/destination/dml/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,37 @@ func TestMergeArgument_BuildRedshiftUpdateQuery(t *testing.T) {
columns.NewColumn("col3", typing.Invalid),
}

mergeArg := MergeArgument{
TableID: MockTableIdentifier{"{TABLE_ID}"},
SubQuery: "{SUB_QUERY}",
PrimaryKeys: []columns.Column{cols[0], cols[2]},
Dialect: sql.SnowflakeDialect{},
{
// Soft deletes enabled:
mergeArg := MergeArgument{
TableID: MockTableIdentifier{"{TABLE_ID}"},
SubQuery: "{SUB_QUERY}",
PrimaryKeys: []columns.Column{cols[0], cols[2]},
Dialect: sql.SnowflakeDialect{},
SoftDelete: true,
}

assert.Equal(t,
`UPDATE {TABLE_ID} as c SET "COL1"=cc."COL1","COL2"=cc."COL2","COL3"=cc."COL3" FROM {SUB_QUERY} as cc WHERE c."COL1" = cc."COL1" and c."COL3" = cc."COL3";`,
mergeArg.buildRedshiftUpdateQuery(cols),
)
}
{
// Soft deletes disabled:
mergeArg := MergeArgument{
TableID: MockTableIdentifier{"{TABLE_ID}"},
SubQuery: "{SUB_QUERY}",
PrimaryKeys: []columns.Column{cols[0], cols[2]},
Dialect: sql.SnowflakeDialect{},
SoftDelete: false,
}

assert.Equal(t,
`UPDATE {TABLE_ID} as c SET "COL1"=cc."COL1","COL2"=cc."COL2","COL3"=cc."COL3" FROM {SUB_QUERY} as cc WHERE c."COL1" = cc."COL1" and c."COL3" = cc."COL3" AND COALESCE(cc."__ARTIE_DELETE", false) = false;`,
mergeArg.buildRedshiftUpdateQuery(cols),
)
}

assert.Equal(t,
`UPDATE {TABLE_ID} as c SET "COL1"=cc."COL1","COL2"=cc."COL2","COL3"=cc."COL3" FROM {SUB_QUERY} as cc WHERE c."COL1" = cc."COL1" and c."COL3" = cc."COL3";`,
mergeArg.buildRedshiftUpdateQuery(cols, true),
)
assert.Equal(t,
`UPDATE {TABLE_ID} as c SET "COL1"=cc."COL1","COL2"=cc."COL2","COL3"=cc."COL3" FROM {SUB_QUERY} as cc WHERE c."COL1" = cc."COL1" and c."COL3" = cc."COL3" AND COALESCE(cc."__ARTIE_DELETE", false) = false;`,
mergeArg.buildRedshiftUpdateQuery(cols, false),
)
}

func TestMergeArgument_BuildRedshiftDeleteQuery(t *testing.T) {
Expand Down

0 comments on commit c1f444e

Please sign in to comment.