Skip to content

Commit

Permalink
Check constraint backend (#9)
Browse files Browse the repository at this point in the history
Backend Support for Check Constraint
  • Loading branch information
taherkl authored and akashthawaitcc committed Dec 26, 2024
1 parent c7f867d commit 1de71cf
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions webv2/api/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,91 @@ func (expressionVerificationHandler *ExpressionsVerificationHandler) VerifyCheck
json.NewEncoder(w).Encode(hasErrorOccurred)
}

// UpdateCheckConstraint processes the request to update spanner table check constraints, ensuring session and schema validity, and responds with the updated conversion metadata.
func UpdateCheckConstraint(w http.ResponseWriter, r *http.Request) {
tableId := r.FormValue("table")
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Body Read Error : %v", err), http.StatusInternalServerError)
}
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()

newCKs := []ddl.CheckConstraint{}
if err = json.Unmarshal(reqBody, &newCKs); err != nil {
http.Error(w, fmt.Sprintf("Request Body parse error : %v", err), http.StatusBadRequest)
return
}

sp := sessionState.Conv.SpSchema[tableId]
sp.CheckConstraints = newCKs
sessionState.Conv.SpSchema[tableId] = sp
session.UpdateSessionFile()

convm := session.ConvWithMetadata{
SessionMetadata: sessionState.SessionMetadata,
Conv: *sessionState.Conv,
}
w.WriteHeader(http.StatusOK)
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 1de71cf

Please sign in to comment.