ddl: Add index id when adding index with ddl
This commit is contained in:
@ -168,7 +168,17 @@ LOOP:
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
// check in index
|
||||
idx := kv.NewKVIndex(t.IndexPrefix(), "c3_index", 0, false)
|
||||
var nidx *column.IndexedCol
|
||||
for _, tidx := range t.Indices() {
|
||||
if tidx.Name.L == "c3_index" {
|
||||
nidx = tidx
|
||||
break
|
||||
}
|
||||
}
|
||||
// Make sure there is index with name c3_index
|
||||
c.Assert(nidx, NotNil)
|
||||
c.Assert(nidx.ID, Greater, int64(0))
|
||||
idx := kv.NewKVIndex(t.IndexPrefix(), "c3_index", nidx.ID, false)
|
||||
txn, err := ctx.GetTxn(true)
|
||||
c.Assert(err, IsNil)
|
||||
defer ctx.FinishTxn(true)
|
||||
@ -202,6 +212,15 @@ func (s *testDBSuite) testDropIndex(c *C) {
|
||||
for i := 0; i < num; i++ {
|
||||
s.mustExec(c, "insert into t1 values (?, ?, ?)", i, i, i)
|
||||
}
|
||||
t := s.testGetTable(c, "t1")
|
||||
var c3idx *column.IndexedCol
|
||||
for _, tidx := range t.Indices() {
|
||||
if tidx.Name.L == "c3_index" {
|
||||
c3idx = tidx
|
||||
break
|
||||
}
|
||||
}
|
||||
c.Assert(c3idx, NotNil)
|
||||
|
||||
go func() {
|
||||
s.mustExec(c, "drop index c3_index on t1")
|
||||
@ -237,8 +256,17 @@ LOOP:
|
||||
|
||||
handles := make(map[int64]struct{})
|
||||
|
||||
t := s.testGetTable(c, "t1")
|
||||
idx := kv.NewKVIndex(t.IndexPrefix(), "c3_index", 1, false)
|
||||
t = s.testGetTable(c, "t1")
|
||||
var nidx *column.IndexedCol
|
||||
for _, tidx := range t.Indices() {
|
||||
if tidx.Name.L == "c3_index" {
|
||||
nidx = tidx
|
||||
break
|
||||
}
|
||||
}
|
||||
// Make sure there is no index with name c3_index
|
||||
c.Assert(nidx, IsNil)
|
||||
idx := kv.NewKVIndex(t.IndexPrefix(), "c3_index", c3idx.ID, false)
|
||||
txn, err := ctx.GetTxn(true)
|
||||
c.Assert(err, IsNil)
|
||||
defer ctx.FinishTxn(true)
|
||||
|
||||
10
ddl/index.go
10
ddl/index.go
@ -29,7 +29,7 @@ import (
|
||||
"github.com/pingcap/tidb/util"
|
||||
)
|
||||
|
||||
func buildIndexInfo(tblInfo *model.TableInfo, unique bool, indexName model.CIStr, idxColNames []*coldef.IndexColName) (*model.IndexInfo, error) {
|
||||
func (d *ddl) buildIndexInfo(tblInfo *model.TableInfo, unique bool, indexName model.CIStr, idxColNames []*coldef.IndexColName) (*model.IndexInfo, error) {
|
||||
for _, col := range tblInfo.Columns {
|
||||
if col.Name.L == indexName.L {
|
||||
return nil, errors.Errorf("CREATE INDEX: index name collision with existing column: %s", indexName)
|
||||
@ -50,14 +50,18 @@ func buildIndexInfo(tblInfo *model.TableInfo, unique bool, indexName model.CIStr
|
||||
Length: ic.Length,
|
||||
})
|
||||
}
|
||||
id, err := d.genGlobalID()
|
||||
if err != nil {
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
// create index info
|
||||
idxInfo := &model.IndexInfo{
|
||||
ID: id,
|
||||
Name: indexName,
|
||||
Columns: idxColumns,
|
||||
Unique: unique,
|
||||
State: model.StateNone,
|
||||
}
|
||||
|
||||
return idxInfo, nil
|
||||
}
|
||||
|
||||
@ -128,7 +132,7 @@ func (d *ddl) onCreateIndex(t *meta.Meta, job *model.Job) error {
|
||||
}
|
||||
|
||||
if indexInfo == nil {
|
||||
indexInfo, err = buildIndexInfo(tblInfo, unique, indexName, idxColNames)
|
||||
indexInfo, err = d.buildIndexInfo(tblInfo, unique, indexName, idxColNames)
|
||||
if err != nil {
|
||||
job.State = model.JobCancelled
|
||||
return errors.Trace(err)
|
||||
|
||||
Reference in New Issue
Block a user