Skip to content

Commit

Permalink
ddl: multi-shema-change, check renames columns/indexes (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Defined2014 authored Mar 14, 2022
1 parent aecd5dc commit e0da4c5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4707,6 +4707,10 @@ func (d *ddl) RenameColumn(ctx sessionctx.Context, ident ast.Ident, spec *ast.Al
},
Args: []interface{}{&newCol, oldColName, spec.Position, 0},
}
if info := ctx.GetSessionVars().StmtCtx.MultiSchemaInfo; info != nil {
info.AddColumns = append(info.AddColumns, newCol)
info.DropColumns = append(info.DropColumns, oldCol.ToInfo())
}
err = d.doDDLJob(ctx, job)
err = d.callHookOnChanged(err)
return errors.Trace(err)
Expand Down Expand Up @@ -5163,7 +5167,14 @@ func (d *ddl) RenameIndex(ctx sessionctx.Context, ident ast.Ident, spec *ast.Alt
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{spec.FromKey, spec.ToKey},
}
if info := ctx.GetSessionVars().StmtCtx.MultiSchemaInfo; info != nil {
idx := tb.Meta().FindIndexByName(spec.FromKey.L)
newIdx := idx.Clone()
newIdx.Name = spec.ToKey

info.DropIndexes = append(info.DropIndexes, idx)
info.AddIndexes = append(info.AddIndexes, newIdx)
}
err = d.doDDLJob(ctx, job)
err = d.callHookOnChanged(err)
return errors.Trace(err)
Expand Down
44 changes: 44 additions & 0 deletions ddl/multi_schema_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,32 @@ func TestMultiSchemaChangeAddDropColumns(t *testing.T) {
tk.MustQuery("select * from t;").Check(testkit.Rows("4 3"))
}

func TestMultiSchemaRenameColumns(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set @@global.tidb_enable_change_multi_schema = 1")

// Test add and rename to same column name
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t rename column b to c, add column c int", errno.ErrUnsupportedDDLOperation)

// Test drop and rename with same column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2);")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t drop column b, rename column b to c", errno.ErrUnsupportedDDLOperation)

// Test add index and rename with same column
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1, b int default 2, index t(a, b));")
tk.MustExec("insert into t values ();")
tk.MustGetErrCode("alter table t rename column b to c, add index t1(a, b)", errno.ErrUnsupportedDDLOperation)
}

func TestMultiSchemaChangeAddIndexes(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand Down Expand Up @@ -192,3 +218,21 @@ func TestMultiSchemaChangeAddDropIndexes(t *testing.T) {
tk.MustExec("create table t (a int, b int, c int, index t(a))")
tk.MustGetErrCode("alter table t add index t1(b), drop index t1", errno.ErrUnsupportedDDLOperation)
}

func TestMultiSchemaRenameIndexes(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set @@global.tidb_enable_change_multi_schema = 1")

// Test drop and rename same index.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int, c int, index t(a))")
tk.MustGetErrCode("alter table t drop index t, rename index t to t1", errno.ErrUnsupportedDDLOperation)

// Test add and rename to same index name.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int, c int, index t(a))")
tk.MustGetErrCode("alter table t add index t1(b), rename index t to t1", errno.ErrUnsupportedDDLOperation)
}

0 comments on commit e0da4c5

Please sign in to comment.