-
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: support alter table auto_increment #5472
Changes from 1 commit
84f2adb
20da919
7731152
2041e2e
c6c3135
0201816
1244fbf
115dbe6
06f0c3a
65d7b9c
fdd70e3
6c60f0c
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 |
---|---|---|
|
@@ -885,6 +885,13 @@ func (d *ddl) AlterTable(ctx context.Context, ident ast.Ident, specs []*ast.Alte | |
err = d.RenameTable(ctx, ident, newIdent) | ||
case ast.AlterTableDropPrimaryKey: | ||
err = ErrUnsupportedModifyPrimaryKey.GenByArgs("drop") | ||
case ast.AlterTableOption: | ||
for _, opt := range spec.Options { | ||
if opt.Tp == ast.TableOptionAutoIncrement { | ||
err = d.RebaseAutoID(ctx, ident, int64(opt.UintValue-1)) | ||
break | ||
} | ||
} | ||
default: | ||
// Nothing to do now. | ||
} | ||
|
@@ -897,6 +904,14 @@ func (d *ddl) AlterTable(ctx context.Context, ident ast.Ident, specs []*ast.Alte | |
return nil | ||
} | ||
|
||
func (d *ddl) RebaseAutoID(ctx context.Context, ident ast.Ident, newBase int64) error { | ||
t, err := d.infoHandle.Get().TableByName(ident.Schema, ident.Name) | ||
if err != nil { | ||
return errors.Trace(infoschema.ErrTableNotExists.GenByArgs(ident.Schema, ident.Name)) | ||
} | ||
return t.RebaseAutoID(ctx, newBase, true) | ||
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. We'd better clear all TiDBs' cache. 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. Done. PTAL @zimulala |
||
} | ||
|
||
func checkColumnConstraint(constraints []*ast.ColumnOption) error { | ||
for _, constraint := range constraints { | ||
switch constraint.Tp { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,9 +75,8 @@ func (s *testValidatorSuite) TestValidator(c *C) { | |
errors.New("[schema:1068]Multiple primary key defined")}, | ||
{"create table t(c1 int not null, c2 int not null, primary key(c1), primary key(c2))", true, | ||
errors.New("[schema:1068]Multiple primary key defined")}, | ||
{"alter table t auto_increment=1", true, errors.New("[autoid:3]No support for setting auto_increment using alter_table")}, | ||
{"alter table t add column c int auto_increment key, auto_increment=10", true, | ||
errors.New("[autoid:3]No support for setting auto_increment using alter_table")}, | ||
{"alter table t auto_increment=1", true, nil}, | ||
{"alter table t add column c int auto_increment key, auto_increment=10", true, nil}, | ||
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. This SQL is different with SQL in 78. Should we add tests in ddl_db_test.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. Done! PTAL @zimulala |
||
{"alter table t add column c int auto_increment key", true, nil}, | ||
{"alter table t add column char4294967295 char(255)", true, nil}, | ||
{"create table t (c float(53))", true, nil}, | ||
|
@@ -184,6 +183,6 @@ func (s *testValidatorSuite) TestValidator(c *C) { | |
c.Assert(stmts, HasLen, 1) | ||
stmt := stmts[0] | ||
err = plan.Preprocess(ctx, stmt, is, tt.inPrepare) | ||
c.Assert(terror.ErrorEqual(err, tt.err), IsTrue) | ||
c.Assert(terror.ErrorEqual(err, tt.err), IsTrue, Commentf("sql: %s", tt.sql)) | ||
} | ||
} |
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.
Why this is opt.UintValue-1?
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.
It is compatible with MySQL, if we set auto_increment = 10, the next auto increment insert column should take 10.
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.
int64(opt.UintValue)-1 should be better. I'll fix it.