-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ddl: check constraint name. #72
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ package ddl | |
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
|
@@ -261,6 +262,37 @@ func checkDuplicateColumn(colDefs []*coldef.ColumnDef) error { | |
return nil | ||
} | ||
|
||
func checkConstraintNames(constraints []*coldef.TableConstraint) error { | ||
m := map[string]bool{} | ||
|
||
// check not empty constraint name do not have duplication. | ||
for _, constr := range constraints { | ||
if constr.ConstrName != "" { | ||
nameLower := strings.ToLower(constr.ConstrName) | ||
if m[nameLower] { | ||
return errors.Errorf("CREATE TABLE: duplicate key %s", constr.ConstrName) | ||
} | ||
m[nameLower] = true | ||
} | ||
} | ||
|
||
// set empty constraint names. | ||
for _, constr := range constraints { | ||
if constr.ConstrName == "" && len(constr.Keys) > 0 { | ||
colName := constr.Keys[0].ColumnName | ||
constrName := colName | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The colName will be reused in the for loop. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it. |
||
i := 2 | ||
for m[strings.ToLower(constrName)] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for is if here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, it is ok here, |
||
constrName = fmt.Sprintf("%s_%d", colName, i) | ||
i++ | ||
} | ||
constr.ConstrName = constrName | ||
m[constrName] = true | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (d *ddl) buildTableInfo(tableName model.CIStr, cols []*column.Col, constraints []*coldef.TableConstraint) (tbInfo *model.TableInfo, err error) { | ||
tbInfo = &model.TableInfo{ | ||
Name: tableName, | ||
|
@@ -321,6 +353,11 @@ func (d *ddl) CreateTable(ctx context.Context, ident table.Ident, colDefs []*col | |
return errors.Trace(err) | ||
} | ||
|
||
err = checkConstraintNames(newConstraints) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
tbInfo, err := d.buildTableInfo(ident.Name, cols, newConstraints) | ||
if err != nil { | ||
return errors.Trace(err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use another clear name instead of m?