Skip to content

Commit

Permalink
ddl: support alter table options for multi-schema change (#36236)
Browse files Browse the repository at this point in the history
ref #14766, close #36234
  • Loading branch information
tangenta committed Jul 18, 2022
1 parent f675cf6 commit 8c380e3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
1 change: 1 addition & 0 deletions ddl/multi_schema_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func fillMultiSchemaInfo(info *model.MultiSchemaInfo, job *model.Job) (err error
case model.ActionAlterIndexVisibility:
idxName := job.Args[0].(model.CIStr)
info.AlterIndexes = append(info.AlterIndexes, idxName)
case model.ActionRebaseAutoID, model.ActionModifyTableComment, model.ActionModifyTableCharsetAndCollate:
default:
return dbterror.ErrRunMultiSchemaChanges
}
Expand Down
37 changes: 37 additions & 0 deletions ddl/multi_schema_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,43 @@ func TestMultiSchemaChangeAdminShowDDLJobs(t *testing.T) {
dom.DDL().SetHook(originHook)
}

func TestMultiSchemaChangeTableOption(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")

tk.MustExec("create table t (a int auto_increment primary key, b int);")
tk.MustExec("alter table t modify column b tinyint, auto_increment = 100;")
tk.MustExec("insert into t (b) values (1);")
tk.MustQuery("select * from t;").Check(testkit.Rows("100 1"))

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int auto_increment primary key, b int);")
tk.MustExec("alter table t auto_increment = 110, auto_increment = 90;")
tk.MustQuery("show warnings;").Check(testkit.Rows("Note 1105 Can't reset AUTO_INCREMENT to 90 without FORCE option, using 110 instead"))
tk.MustExec("insert into t (b) values (1);")
tk.MustQuery("select * from t;").Check(testkit.Rows("110 1"))

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int) charset = utf8 shard_row_id_bits=2;")
tk.MustExec("alter table t modify column a tinyint, comment = 'abc', charset = utf8mb4;")
tk.MustQuery("select TIDB_ROW_ID_SHARDING_INFO, TABLE_COMMENT, TABLE_COLLATION from information_schema.tables where table_name = 't';").
Check(testkit.Rows("SHARD_BITS=2 abc utf8mb4_bin"))
}

func TestMultiSchemaChangeNonPublicDefaultValue(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
tk.MustExec("create table t (a tinyint);")
tk.MustExec("insert into t set a = 10;")
tk.MustExec("alter table t add column b int not null, change column a c char(5) first;")
tk.MustQuery("select * from t;").Check(testkit.Rows("10 0"))
}

func TestMultiSchemaChangeAlterIndexVisibility(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand Down
42 changes: 36 additions & 6 deletions ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,23 +760,43 @@ func onRebaseAutoID(d *ddlCtx, store kv.Storage, t *meta.Meta, job *model.Job, t
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}

if job.MultiSchemaInfo != nil && job.MultiSchemaInfo.Revertible {
job.MarkNonRevertible()
return ver, nil
}

tblInfo, err := GetTableInfoAndCancelFaultJob(t, job, schemaID)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}
// No need to check `newBase` again, because `RebaseAutoID` will do this check.
if tp == autoid.RowIDAllocType {
tblInfo.AutoIncID = newBase
} else {
tblInfo.AutoRandID = newBase
}

tbl, err := getTable(store, schemaID, tblInfo)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}

if !force {
newBaseTemp, err := adjustNewBaseToNextGlobalID(nil, tbl, tp, newBase)
if err != nil {
return ver, errors.Trace(err)
}
if newBase != newBaseTemp {
job.Warning = toTError(fmt.Errorf("Can't reset AUTO_INCREMENT to %d without FORCE option, using %d instead",
newBase, newBaseTemp,
))
}
newBase = newBaseTemp
}

if tp == autoid.RowIDAllocType {
tblInfo.AutoIncID = newBase
} else {
tblInfo.AutoRandID = newBase
}

if alloc := tbl.Allocators(nil).Get(tp); alloc != nil {
// The next value to allocate is `newBase`.
newEnd := newBase - 1
Expand Down Expand Up @@ -1008,6 +1028,11 @@ func onModifyTableComment(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _
return ver, errors.Trace(err)
}

if job.MultiSchemaInfo != nil && job.MultiSchemaInfo.Revertible {
job.MarkNonRevertible()
return ver, nil
}

tblInfo.Comment = comment
ver, err = updateVersionAndTableInfo(d, t, job, tblInfo, true)
if err != nil {
Expand Down Expand Up @@ -1042,6 +1067,11 @@ func onModifyTableCharsetAndCollate(d *ddlCtx, t *meta.Meta, job *model.Job) (ve
return ver, errors.Trace(err)
}

if job.MultiSchemaInfo != nil && job.MultiSchemaInfo.Revertible {
job.MarkNonRevertible()
return ver, nil
}

tblInfo.Charset = toCharset
tblInfo.Collate = toCollate

Expand Down
9 changes: 6 additions & 3 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1303,9 +1303,12 @@ func (e *DDLJobsReaderExec) Next(ctx context.Context, req *chunk.Chunk) error {
for i := e.cursor; i < e.cursor+num; i++ {
e.appendJobToChunk(req, e.runningJobs[i], checker)
req.AppendString(12, e.runningJobs[i].Query)
for range e.runningJobs[i].MultiSchemaInfo.SubJobs {
req.AppendString(12, e.runningJobs[i].Query)
if e.runningJobs[i].MultiSchemaInfo != nil {
for range e.runningJobs[i].MultiSchemaInfo.SubJobs {
req.AppendString(12, e.runningJobs[i].Query)
}
}

}
e.cursor += num
count += num
Expand All @@ -1321,7 +1324,7 @@ func (e *DDLJobsReaderExec) Next(ctx context.Context, req *chunk.Chunk) error {
for _, job := range e.cacheJobs {
e.appendJobToChunk(req, job, checker)
req.AppendString(12, job.Query)
if job.Type == model.ActionMultiSchemaChange {
if job.MultiSchemaInfo != nil {
for range job.MultiSchemaInfo.SubJobs {
req.AppendString(12, job.Query)
}
Expand Down

0 comments on commit 8c380e3

Please sign in to comment.