diff --git a/internal/convert.go b/internal/convert.go index 1f54f8611..5a27a890f 100644 --- a/internal/convert.go +++ b/internal/convert.go @@ -133,6 +133,7 @@ const ( NumericPKNotSupported DefaultValueError TypeMismatch + DefaultValueError ) const ( diff --git a/internal/reports/report_helpers.go b/internal/reports/report_helpers.go index c4f3084bd..b6c8ad5f9 100644 --- a/internal/reports/report_helpers.go +++ b/internal/reports/report_helpers.go @@ -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, diff --git a/sources/common/toddl_test.go b/sources/common/toddl_test.go index f5c33c610..510ab638c 100644 --- a/sources/common/toddl_test.go +++ b/sources/common/toddl_test.go @@ -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) + }) + } +}