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 <[email protected]>
Co-authored-by: Akash Thawait <[email protected]>
Co-authored-by: Vivek Yadav <[email protected]>
  • Loading branch information
4 people committed Dec 26, 2024
1 parent ef3c414 commit 243107e
Showing 1 changed file with 0 additions and 51 deletions.
51 changes: 0 additions & 51 deletions webv2/api/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,57 +523,6 @@ func UpdateCheckConstraint(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(convm)
}

func doesNameExist(spcks []ddl.CheckConstraint, targetName string) bool {
for _, spck := range spcks {
if strings.Contains(spck.Expr, targetName) {
return true
}
}
return false
}

// ValidateCheckConstraint verifies if the type of a database column has been altered and add an error if a change is detected.
func ValidateCheckConstraint(w http.ResponseWriter, r *http.Request) {
sessionState := session.GetSessionState()
if sessionState.Conv == nil || sessionState.Driver == "" {
http.Error(w, fmt.Sprintf("Schema is not converted or Driver is not configured properly. Please retry converting the database to Spanner."), http.StatusNotFound)
return
}
sessionState.Conv.ConvLock.Lock()
defer sessionState.Conv.ConvLock.Unlock()

sp := sessionState.Conv.SpSchema
srcschema := sessionState.Conv.SrcSchema
flag := true
var schemaIssue []internal.SchemaIssue

for _, src := range srcschema {
for _, col := range sp[src.Id].ColDefs {
if len(sp[src.Id].CheckConstraints) > 0 {
spType := col.T.Name
srcType := srcschema[src.Id].ColDefs[col.Id].Type
actualType := mysqlDefaultTypeMap[srcType.Name]
if actualType.Name != spType {
columnName := sp[src.Id].ColDefs[col.Id].Name
spcks := sp[src.Id].CheckConstraints
if doesNameExist(spcks, columnName) {
flag = false
schemaIssue = sessionState.Conv.SchemaIssues[src.Id].ColumnLevelIssues[col.Id]
if !utilities.IsSchemaIssuePresent(schemaIssue, internal.TypeMismatch) {
schemaIssue = append(schemaIssue, internal.TypeMismatch)
}
sessionState.Conv.SchemaIssues[src.Id].ColumnLevelIssues[col.Id] = schemaIssue
break
}
}
}
}
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(flag)
}

// renameForeignKeys checks the new names for spanner name validity, ensures the new names are already not used by existing tables
// secondary indexes or foreign key constraints. If above checks passed then foreignKey renaming reflected in the schema else appropriate
// error thrown.
Expand Down

0 comments on commit 243107e

Please sign in to comment.