Skip to content

Commit

Permalink
undo me not understanding PG merge
Browse files Browse the repository at this point in the history
  • Loading branch information
heavycrystal committed Jan 9, 2024
1 parent 30bf7e8 commit fb30e5f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion flow/connectors/postgres/normalize_stmt_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (n *normalizeStmtGenerator) generateUpdateStatements(allCols []string) []st
unquotedUnchangedColsArray := strings.Split(cols, ",")
unchangedColsArray := make([]string, 0, len(unquotedUnchangedColsArray))
for _, unchangedToastCol := range unquotedUnchangedColsArray {
unchangedColsArray = append(unchangedColsArray, unchangedToastCol)
unchangedColsArray = append(unchangedColsArray, fmt.Sprintf(`"%s"`, unchangedToastCol))
}
otherCols := utils.ArrayMinus(allCols, unchangedColsArray)
tmpArray := make([]string, 0, len(otherCols))
Expand Down
40 changes: 21 additions & 19 deletions flow/connectors/postgres/normalize_stmt_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
)

func TestGenerateMergeUpdateStatement(t *testing.T) {
allCols := []string{"col1", "col2", "col3"}
allCols := []string{`"col1"`, `"col2"`, `"col3"`}
unchangedToastCols := []string{""}

expected := []string{
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns=''
THEN UPDATE SET col1=src.col1,col2=src.col2,col3=src.col3,
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","col3"=src."col3",
"_peerdb_synced_at"=CURRENT_TIMESTAMP`,
}
normalizeGen := &normalizeStmtGenerator{
Expand All @@ -38,15 +38,15 @@ func TestGenerateMergeUpdateStatement(t *testing.T) {
}

func TestGenerateMergeUpdateStatement_WithSoftDelete(t *testing.T) {
allCols := []string{"col1", "col2", "col3"}
allCols := []string{`"col1"`, `"col2"`, `"col3"`}
unchangedToastCols := []string{""}

expected := []string{
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns=''
THEN UPDATE SET col1=src.col1,col2=src.col2,col3=src.col3,
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","col3"=src."col3",
"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
`WHEN MATCHED AND src._peerdb_record_type=2 AND _peerdb_unchanged_toast_columns=''
THEN UPDATE SET col1=src.col1,col2=src.col2,col3=src.col3,
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","col3"=src."col3",
"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
}
normalizeGen := &normalizeStmtGenerator{
Expand All @@ -70,18 +70,18 @@ func TestGenerateMergeUpdateStatement_WithSoftDelete(t *testing.T) {
}

func TestGenerateMergeUpdateStatement_WithUnchangedToastCols(t *testing.T) {
allCols := []string{"col1", "col2", "col3"}
allCols := []string{`"col1"`, `"col2"`, `"col3"`}
unchangedToastCols := []string{"", "col2,col3", "col2", "col3"}

expected := []string{
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns=''
THEN UPDATE SET col1=src.col1,col2=src.col2,col3=src.col3,"_peerdb_synced_at"=CURRENT_TIMESTAMP`,
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","col3"=src."col3","_peerdb_synced_at"=CURRENT_TIMESTAMP`,
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns='col2,col3'
THEN UPDATE SET col1=src.col1,"_peerdb_synced_at"=CURRENT_TIMESTAMP`,
THEN UPDATE SET "col1"=src."col1","_peerdb_synced_at"=CURRENT_TIMESTAMP`,
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns='col2'
THEN UPDATE SET col1=src.col1,col3=src.col3,"_peerdb_synced_at"=CURRENT_TIMESTAMP`,
THEN UPDATE SET "col1"=src."col1","col3"=src."col3","_peerdb_synced_at"=CURRENT_TIMESTAMP`,
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns='col3'
THEN UPDATE SET col1=src.col1,col2=src.col2,"_peerdb_synced_at"=CURRENT_TIMESTAMP`,
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","_peerdb_synced_at"=CURRENT_TIMESTAMP`,
}
normalizeGen := &normalizeStmtGenerator{
unchangedToastColumns: unchangedToastCols,
Expand All @@ -104,26 +104,28 @@ func TestGenerateMergeUpdateStatement_WithUnchangedToastCols(t *testing.T) {
}

func TestGenerateMergeUpdateStatement_WithUnchangedToastColsAndSoftDelete(t *testing.T) {
allCols := []string{"col1", "col2", "col3"}
allCols := []string{`"col1"`, `"col2"`, `"col3"`}
unchangedToastCols := []string{"", "col2,col3", "col2", "col3"}

expected := []string{
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns=''
THEN UPDATE SET col1=src.col1,col2=src.col2,col3=src.col3,"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","col3"=src."col3",
"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
`WHEN MATCHED AND src._peerdb_record_type=2 AND _peerdb_unchanged_toast_columns=''
THEN UPDATE SET col1=src.col1,col2=src.col2,col3=src.col3,"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","col3"=src."col3",
"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns='col2,col3'
THEN UPDATE SET col1=src.col1,"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
THEN UPDATE SET "col1"=src."col1","_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
`WHEN MATCHED AND src._peerdb_record_type=2 AND _peerdb_unchanged_toast_columns='col2,col3'
THEN UPDATE SET col1=src.col1,"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
THEN UPDATE SET "col1"=src."col1","_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns='col2'
THEN UPDATE SET col1=src.col1,col3=src.col3,"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
THEN UPDATE SET "col1"=src."col1","col3"=src."col3","_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
`WHEN MATCHED AND src._peerdb_record_type=2 AND _peerdb_unchanged_toast_columns='col2'
THEN UPDATE SET col1=src.col1,col3=src.col3,"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
THEN UPDATE SET "col1"=src."col1","col3"=src."col3","_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns='col3'
THEN UPDATE SET col1=src.col1,col2=src.col2,"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
`WHEN MATCHED AND src._peerdb_record_type=2 AND _peerdb_unchanged_toast_columns='col3'
THEN UPDATE SET col1=src.col1,col2=src.col2,"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
}
normalizeGen := &normalizeStmtGenerator{
unchangedToastColumns: unchangedToastCols,
Expand Down

0 comments on commit fb30e5f

Please sign in to comment.