-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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: fix drop index failed when index contain auto_increment column and more than 2 index contain auto_increment_column #12230
Changes from 2 commits
734cbea
0847a0d
21d3d13
2d23cc7
8cc0592
91ff895
350307b
e3870c3
6a47b9c
b6dd4bc
25397f0
29ff01c
c3c82d4
1beebea
fdb220e
2af81dd
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 |
---|---|---|
|
@@ -3396,9 +3396,10 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI | |
return err | ||
} | ||
|
||
// Check for drop index on auto_increment column. | ||
cols := t.Cols() | ||
for _, idxCol := range indexInfo.Columns { | ||
if mysql.HasAutoIncrementFlag(cols[idxCol.Offset].Flag) { | ||
if mysql.HasAutoIncrementFlag(cols[idxCol.Offset].Flag) && countOfIndexContainColumn(t.Meta(), idxCol.Name.L) < 2 { | ||
return autoid.ErrWrongAutoKey | ||
} | ||
} | ||
|
@@ -3422,6 +3423,20 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI | |
return errors.Trace(err) | ||
} | ||
|
||
func countOfIndexContainColumn(tblInfo *model.TableInfo, colName string) int { | ||
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. Could we put this function to index.go? 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. Great, Done. |
||
count := 0 | ||
for _, idx := range tblInfo.Indices { | ||
for _, c := range idx.Columns { | ||
if c.Name.L != colName { | ||
continue | ||
} | ||
count++ | ||
break | ||
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. I think we can change this code as follows:
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. Great, Done. |
||
} | ||
} | ||
return count | ||
} | ||
|
||
func isDroppableColumn(tblInfo *model.TableInfo, colName model.CIStr) error { | ||
// Check whether there are other columns depend on this column or not. | ||
for _, col := range tblInfo.Columns { | ||
|
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.
Could we extract this code(line3400-3405) directly into a function instead of using
countOfIndexContainColumn
?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.
Great, Done.