Skip to content

Commit

Permalink
ddl: add some test for drop indexes (pingcap#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta committed Mar 17, 2022
1 parent c897a72 commit 12df33f
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 14 deletions.
4 changes: 4 additions & 0 deletions ddl/db_legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,10 @@ func (t *testDDLJobIDCallback) OnJobUpdated(job *model.Job) {
}
}

func (t *testDDLJobIDCallback) Clear() {
t.jobID = 0
}

func wrapJobIDExtCallback(oldCallback ddl.Callback) *testDDLJobIDCallback {
return &testDDLJobIDCallback{
Callback: oldCallback,
Expand Down
2 changes: 1 addition & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5767,7 +5767,7 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI
SchemaName: schema.Name.L,
Type: jobTp,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{indexName, indexInfo.ID, nil /* partition IDs */, ifExists},
Args: []interface{}{indexName, ifExists},
}
err = d.doDDLJob(ctx, job)
err = d.callHookOnChanged(err)
Expand Down
6 changes: 4 additions & 2 deletions ddl/delete_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,9 @@ func insertJobIntoDeleteRangeTable(ctx context.Context, sctx sessionctx.Context,
case model.ActionAddIndex, model.ActionAddPrimaryKey:
tableID := job.TableID
var indexID int64
var ifExists bool
var partitionIDs []int64
if err := job.DecodeArgs(&indexID, &partitionIDs); err != nil {
if err := job.DecodeArgs(&indexID, &ifExists, &partitionIDs); err != nil {
return errors.Trace(err)
}
if len(partitionIDs) > 0 {
Expand All @@ -314,9 +315,10 @@ func insertJobIntoDeleteRangeTable(ctx context.Context, sctx sessionctx.Context,
case model.ActionDropIndex, model.ActionDropPrimaryKey:
tableID := job.TableID
var indexName interface{}
var ifExists bool
var indexID int64
var partitionIDs []int64
if err := job.DecodeArgs(&indexName, &indexID, &partitionIDs); err != nil {
if err := job.DecodeArgs(&indexName, &ifExists, &indexID, &partitionIDs); err != nil {
return errors.Trace(err)
}
if len(partitionIDs) > 0 {
Expand Down
3 changes: 1 addition & 2 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,7 @@ func checkDropIndex(t *meta.Meta, job *model.Job) (*model.TableInfo, *model.Inde

var indexName model.CIStr
var ifExists bool
var placeholder interface{}
if err = job.DecodeArgs(&indexName, &placeholder, &placeholder, &ifExists); err != nil {
if err = job.DecodeArgs(&indexName, &ifExists); err != nil {
job.State = model.JobStateCancelled
return nil, nil, false, errors.Trace(err)
}
Expand Down
1 change: 1 addition & 0 deletions ddl/index_modify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,7 @@ func testDropIndexes(t *testing.T, store kv.Storage, createSQL, dropIdxSQL strin
tk.MustExec("use test")
tk.MustExec("drop table if exists test_drop_indexes")
tk.MustExec(createSQL)
tk.MustExec("set @@global.tidb_enable_change_multi_schema = 1;")
done := make(chan error, 1)

num := 100
Expand Down
2 changes: 1 addition & 1 deletion ddl/multi_schema_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func fillMultiSchemaInfo(info *model.MultiSchemaInfo, job *model.Job) (err error
case model.ActionDropColumn:
colName := job.Args[0].(model.CIStr)
info.DropColumns = append(info.DropColumns, colName)
case model.ActionDropIndex:
case model.ActionDropIndex, model.ActionDropPrimaryKey:
indexName := job.Args[0].(model.CIStr)
info.DropIndexes = append(info.DropIndexes, indexName)
case model.ActionAddIndex, model.ActionAddPrimaryKey:
Expand Down
50 changes: 43 additions & 7 deletions ddl/multi_schema_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,21 @@ func TestMultiSchemaChangeAddColumns(t *testing.T) {
tk.MustExec("insert into t values (1);")
tk.MustExec("alter table t add column (b int default 2, c int default 3);")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3"))

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int, c int);")
tk.MustExec("insert into t values (1, 2, 3);")
tk.MustExec("alter table t add column (d int default 4, e int default 5);")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3 4 5"))

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int default 1);")
tk.MustExec("insert into t values ();")
tk.MustExec("alter table t add column if not exists (b int default 2, c int default 3);")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3"))
tk.MustExec("alter table t add column if not exists (c int default 3, d int default 4);")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3 4"))

// Test referencing previous column in multi-schema change is not supported.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
Expand Down Expand Up @@ -339,30 +348,57 @@ func TestMultiSchemaChangeAddIndexes(t *testing.T) {
}

func TestMultiSchemaChangeDropIndexes(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set @@global.tidb_enable_change_multi_schema = 1")
tk.MustExec("use test;")
tk.MustExec("set @@global.tidb_enable_change_multi_schema = 1;")
getIndexID := func(name string) int64 {
tt, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(t, err)
for _, idx := range tt.Indices() {
if idx.Meta().Name.L == name {
return idx.Meta().ID
}
}
return -1
}

jobIDExt := wrapJobIDExtCallback(dom.DDL().GetHook())
dom.DDL().SetHook(jobIDExt)

// Test drop same index.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int, c int, index t(a))")
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, drop index t", errno.ErrUnsupportedDDLOperation)

tk.MustExec("drop table if exists t")
tk.MustExec("create table t (id int, c1 int, c2 int, primary key(id), key i1(c1), key i2(c2));")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (id int, c1 int, c2 int, primary key(id) nonclustered, key i1(c1), key i2(c2), key i3(c1, c2));")
tk.MustExec("insert into t values (1, 2, 3);")
jobIDExt.Clear()
i1, i2 := getIndexID("i1"), getIndexID("i2")
tk.MustExec("alter table t drop index i1, drop index i2;")
tk.MustGetErrCode("select * from t use index(i1);", errno.ErrKeyDoesNotExist)
tk.MustGetErrCode("select * from t use index(i2);", errno.ErrKeyDoesNotExist)
checkDelRangeAdded(tk, jobIDExt.jobID, i1)
checkDelRangeAdded(tk, jobIDExt.jobID, i2)
jobIDExt.Clear()
pk, i3 := getIndexID("primary"), getIndexID("i3")
tk.MustExec("alter table t drop index i3, drop primary key;")
tk.MustGetErrCode("select * from t use index(primary);", errno.ErrKeyDoesNotExist)
tk.MustGetErrCode("select * from t use index(i3);", errno.ErrKeyDoesNotExist)
checkDelRangeAdded(tk, jobIDExt.jobID, pk)
checkDelRangeAdded(tk, jobIDExt.jobID, i3)

// Test drop index with drop column.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int default 1, b int default 2, c int default 3, index t(a))")
tk.MustExec("insert into t values ();")
jobIDExt.Clear()
idx := getIndexID("t")
tk.MustExec("alter table t drop index t, drop column a")
tk.MustGetErrCode("select * from t force index(t)", errno.ErrKeyDoesNotExist)
checkDelRangeAdded(tk, jobIDExt.jobID, idx)
}

func TestMultiSchemaChangeDropIndexesCancelled(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion ddl/rollingback.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func convertAddIdxJob2RollbackJob(t *meta.Meta, job *model.Job, tblInfo *model.T
}

// the second args will be used in onDropIndex.
job.Args = []interface{}{indexInfo.Name, getPartitionIDs(tblInfo)}
job.Args = []interface{}{indexInfo.Name /* ifExists */, false, getPartitionIDs(tblInfo)}
// If add index job rollbacks in write reorganization state, its need to delete all keys which has been added.
// Its work is the same as drop index job do.
// The write reorganization state in add index job that likes write only state in drop index job.
Expand Down

0 comments on commit 12df33f

Please sign in to comment.