Skip to content

Commit

Permalink
feat: APIs for Backend Changes for Default Values (GoogleCloudPlatfor…
Browse files Browse the repository at this point in the history
…m#965)

* backend apis

* linting

* comment changes

* comment changes
  • Loading branch information
asthamohta authored and akashthawaitcc committed Dec 26, 2024
1 parent 1937320 commit d3e9188
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ const (
NumericPKNotSupported
DefaultValueError
TypeMismatch
DefaultValueError
)

const (
Expand Down
6 changes: 6 additions & 0 deletions internal/reports/report_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,12 @@ func buildTableReportBody(conv *internal.Conv, tableId string, issues map[string
}
l = append(l, toAppend)

case internal.DefaultValueError:
toAppend := Issue{
Category: IssueDB[i].Category,
Description: fmt.Sprintf("%s for table '%s' column '%s'", IssueDB[i].Brief, conv.SpSchema[tableId].Name, spColName),
}
l = append(l, toAppend)
default:
toAppend := Issue{
Category: IssueDB[i].Category,
Expand Down
109 changes: 109 additions & 0 deletions sources/common/toddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,112 @@ func Test_cvtCheckContraint(t *testing.T) {
result := cvtCheckConstraint(conv, srcSchema)
assert.Equal(t, spSchema, result)
}

func TestSpannerSchemaApplyExpressions(t *testing.T) {
makeConv := func() *internal.Conv {
conv := internal.MakeConv()
conv.SchemaIssues = make(map[string]internal.TableIssues)
conv.SchemaIssues["table1"] = internal.TableIssues{
ColumnLevelIssues: make(map[string][]internal.SchemaIssue),
}
conv.SpSchema = ddl.Schema{
"table1": {
ColDefs: map[string]ddl.ColumnDef{
"col1": {},
},
},
}
return conv
}

makeResultConv := func(SpSchema ddl.Schema, SchemaIssues map[string]internal.TableIssues) *internal.Conv {
conv := internal.MakeConv()
conv.SpSchema = SpSchema
conv.SchemaIssues = SchemaIssues
return conv
}

testCases := []struct {
name string
conv *internal.Conv
expressions internal.VerifyExpressionsOutput
expectedConv *internal.Conv
}{
{
name: "successful default value application",
conv: makeConv(),
expressions: internal.VerifyExpressionsOutput{
ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{
{
Result: true,
ExpressionDetail: internal.ExpressionDetail{
Type: "DEFAULT",
ExpressionId: "expr1",
Expression: "SELECT 1",
Metadata: map[string]string{"TableId": "table1", "ColId": "col1"},
},
},
},
},
expectedConv: makeResultConv(
ddl.Schema{
"table1": {
ColDefs: map[string]ddl.ColumnDef{
"col1": {
DefaultValue: ddl.DefaultValue{
IsPresent: true,
Value: ddl.Expression{
ExpressionId: "expr1",
Statement: "SELECT 1",
},
},
},
},
},
}, map[string]internal.TableIssues{
"table1": {
ColumnLevelIssues: make(map[string][]internal.SchemaIssue),
},
}),
},
{
name: "failed default value application",
conv: makeConv(),
expressions: internal.VerifyExpressionsOutput{
ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{
{
Result: false,
ExpressionDetail: internal.ExpressionDetail{
Type: "DEFAULT",
ExpressionId: "expr1",
Expression: "SELECT 1",
Metadata: map[string]string{"TableId": "table1", "ColId": "col1"},
},
},
},
},
expectedConv: makeResultConv(
ddl.Schema{
"table1": {
ColDefs: map[string]ddl.ColumnDef{
"col1": {},
},
},
},
map[string]internal.TableIssues{
"table1": {
ColumnLevelIssues: map[string][]internal.SchemaIssue{
"col1": {internal.DefaultValue},
},
},
}),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
spannerSchemaApplyExpressions(tc.conv, tc.expressions)
assert.Equal(t, tc.expectedConv, tc.conv)
})
}
}

0 comments on commit d3e9188

Please sign in to comment.