Skip to content

Commit

Permalink
Support check constraint backend (GoogleCloudPlatform#962)
Browse files Browse the repository at this point in the history
* Check constraint backend (#9)

Backend Support for Check Constraint

* update api

* fix PR comment

* remove api call to while validating constraints

* Fixed db collation regex to remove collation name from the results

* renamed function name to formatCheckConstraints and added check if constraint name is empty

* fixed PR comments

* added test case for the empty check constraint name

* fix: added regular exprression to match the exact column

* fix: added regular expression to replace table name

* Added test case for the column rename for check constraint

* 1. Refactored GetConstraint function
2. Fixed inforschema unit tests

* added comment at handling case for check constraints

* reverted white spaces

* reverted white spaces

* nit: doesCheckConstraintNameExist

* added comments for doesCheckConstraintNameExist

* PR and UT fixes

* fix UT

* UT fix

* Removed isCheckConstraintsTablePresent function

* moved regex globally

* Fix UT

* fixed UT

* fixed handling of the constraints

* removed unused function

* added unit tests for incompatable name

* Combined unit tests

* added test case for the renaming column having substring of other column

* added the query changes which return distinct value

---------

Co-authored-by: taherkl <taher.lakdawala@ollion.com>
Co-authored-by: Akash Thawait <aakash@ollion.com>
Co-authored-by: Vivek Yadav <vivek.yadav@ollion.com>
  • Loading branch information
4 people committed Dec 20, 2024
1 parent 260da8a commit e51ede2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
1 change: 0 additions & 1 deletion sources/common/toddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ func (ss *SchemaToSpannerImpl) SchemaToSpannerDDLHelper(conv *internal.Conv, tod
Comment: comment,
Id: srcTable.Id,
}

return nil
}

Expand Down
40 changes: 28 additions & 12 deletions sources/common/toddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,22 @@ func Test_cvtCheckContraint(t *testing.T) {
conv := internal.MakeConv()
srcSchema := []schema.CheckConstraint{
{
Id: "cc1",
Name: "check_1",
Expr: "age > 0",
Id: "cc1",
Name: "check_1",
Expr: "age > 0",
ExprId: "expr1",
},
{
Id: "cc2",
Name: "check_2",
Expr: "age < 99",
Id: "cc2",
Name: "check_2",
Expr: "age < 99",
ExprId: "expr2",
},
{
Id: "cc3",
Name: "@invalid_name", // incompatabile name
Expr: "age != 0",
ExprId: "expr3",
},
{
Id: "cc3",
Expand All @@ -455,14 +463,22 @@ func Test_cvtCheckContraint(t *testing.T) {
}
spSchema := []ddl.CheckConstraint{
{
Id: "cc1",
Name: "check_1",
Expr: "age > 0",
Id: "cc1",
Name: "check_1",
Expr: "age > 0",
ExprId: "expr1",
},
{
Id: "cc2",
Name: "check_2",
Expr: "age < 99",
ExprId: "expr2",
},
{
Id: "cc2",
Name: "check_2",
Expr: "age < 99",
Id: "cc3",
Name: "Ainvalid_name",
Expr: "age != 0",
ExprId: "expr3",
},
{
Id: "cc3",
Expand Down
1 change: 0 additions & 1 deletion sources/mysql/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ func (isi InfoSchemaImpl) processRow(
// Case added to handle check constraints
case "CHECK":
checkClause = collationRegex.ReplaceAllString(checkClause, "")
// constraintName := fmt.Sprintf("%s_check", col)
*checkKeys = append(*checkKeys, schema.CheckConstraint{Name: constraintName, Expr: checkClause, ExprId: internal.GenerateCheckConstrainstExprId(), Id: internal.GenerateCheckConstrainstId()})
default:
m[col] = append(m[col], constraintType)
Expand Down
2 changes: 1 addition & 1 deletion sources/mysql/infoschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func TestProcessData_MultiCol(t *testing.T) {
}
internal.AssertSpSchema(conv, t, expectedSchema, stripSchemaComments(conv.SpSchema))
columnLevelIssues := map[string][]internal.SchemaIssue{
"c5": {
"c49": {
2,
},
}
Expand Down
12 changes: 0 additions & 12 deletions webv2/api/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api_test

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -2630,17 +2629,6 @@ func (errReader) Read(p []byte) (n int, err error) {
return 0, fmt.Errorf("simulated read error")
}

// MockExpressionVerificationAccessor is a mock of ExpressionVerificationAccessor
type MockExpressionVerificationAccessor struct {
mock.Mock
}

// VerifyExpressions is a mocked method for expression verification
func (m *MockExpressionVerificationAccessor) VerifyExpressions(ctx context.Context, input internal.VerifyExpressionsInput) internal.VerifyExpressionsOutput {
args := m.Called(ctx, input)
return args.Get(0).(internal.VerifyExpressionsOutput)
}

func TestVerifyCheckConstraintExpressions(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit e51ede2

Please sign in to comment.