func (ts *testSuite) TestConstraintNames(c *C) { handle := infoschema.NewHandle(ts.store) handle.Set(nil) dd := ddl.NewDDL(ts.store, handle) se, _ := tidb.CreateSession(ts.store) ctx := se.(context.Context) schemaName := model.NewCIStr("test") tblName := model.NewCIStr("t") tbIdent := table.Ident{ Schema: schemaName, Name: tblName, } err := dd.CreateSchema(ctx, tbIdent.Schema) c.Assert(err, IsNil) tbStmt := statement("create table t (a int, b int, index a (a, b), index a (a))").(*stmts.CreateTableStmt) err = dd.CreateTable(ctx, tbIdent, tbStmt.Cols, tbStmt.Constraints) c.Assert(err, NotNil) tbStmt = statement("create table t (a int, b int, index A (a, b), index (a))").(*stmts.CreateTableStmt) err = dd.CreateTable(ctx, tbIdent, tbStmt.Cols, tbStmt.Constraints) c.Assert(err, IsNil) tbl, err := handle.Get().TableByName(schemaName, tblName) indices := tbl.Indices() c.Assert(len(indices), Equals, 2) c.Assert(indices[0].Name.O, Equals, "A") c.Assert(indices[1].Name.O, Equals, "a_2") err = dd.DropSchema(ctx, tbIdent.Schema) c.Assert(err, IsNil) }
// NewDomain creates a new domain. Should not create multiple domains for the same store. func NewDomain(store kv.Storage, lease time.Duration) (d *Domain, err error) { d = &Domain{ store: store, SchemaValidity: &schemaValidityInfo{}, exit: make(chan struct{}), } d.infoHandle, err = infoschema.NewHandle(d.store) if err != nil { return nil, errors.Trace(err) } d.ddl = ddl.NewDDL(d.store, d.infoHandle, &ddlCallback{do: d}, lease) if err = d.Reload(); err != nil { return nil, errors.Trace(err) } d.SchemaValidity.SetExpireInfo(false, 0) variable.RegisterStatistics(d) // Only when the store is local that the lease value is 0. // If the store is local, it doesn't need loadSchemaInLoop and checkValidityInLoop. if lease > 0 { go d.checkValidityInLoop(lease) } // Local store needs to get the change information for every DDL state in each session. go d.loadSchemaInLoop(lease) return d, nil }
// NewDomain creates a new domain. func NewDomain(store kv.Storage) (d *Domain, err error) { d = &Domain{ store: store, } d.infoHandle = infoschema.NewHandle(d.store) d.ddl = ddl.NewDDL(d.store, d.infoHandle, d.onDDLChange) err = d.reload() return d, errors.Trace(err) }
// NewDomain creates a new domain. func NewDomain(store kv.Storage, lease time.Duration) (d *Domain, err error) { d = &Domain{ store: store, leaseCh: make(chan time.Duration, 1), } d.infoHandle = infoschema.NewHandle(d.store) d.ddl = ddl.NewDDL(d.store, d.infoHandle, &ddlCallback{do: d}, lease) d.mustReload() variable.RegisterStatistics(d) go d.loadSchemaInLoop(lease) return d, nil }
// NewDomain creates a new domain. func NewDomain(store kv.Storage, lease int) (d *Domain, err error) { d = &Domain{ store: store, lease: lease, } d.infoHandle = infoschema.NewHandle(d.store) d.ddl = ddl.NewDDL(d.store, d.infoHandle, d.onDDLChange, lease) err = d.reload() if err != nil { log.Fatalf("load schema err %v", err) } go d.loadSchemaInLoop() return d, nil }
// NewDomain creates a new domain. func NewDomain(store kv.Storage, lease time.Duration) (d *Domain, err error) { d = &Domain{store: store} d.infoHandle, err = infoschema.NewHandle(d.store) if err != nil { return nil, errors.Trace(err) } d.ddl = ddl.NewDDL(d.store, d.infoHandle, &ddlCallback{do: d}, lease) d.mustReload() variable.RegisterStatistics(d) // Only when the store is local that the lease value is 0. // If the store is local, it doesn't need loadSchemaInLoop. if lease > 0 { d.leaseCh = make(chan time.Duration, 1) go d.loadSchemaInLoop(lease) } return d, nil }
func (ts *testSuite) TestT(c *C) { handle := infoschema.NewHandle(ts.store) handle.Set(nil) dd := ddl.NewDDL(ts.store, handle) se, _ := tidb.CreateSession(ts.store) ctx := se.(context.Context) schemaName := model.NewCIStr("test") tblName := model.NewCIStr("t") tbIdent := table.Ident{ Schema: schemaName, Name: tblName, } noExist := model.NewCIStr("noexist") err := dd.CreateSchema(ctx, tbIdent.Schema) c.Assert(err, IsNil) err = dd.CreateSchema(ctx, tbIdent.Schema) c.Assert(errors2.ErrorEqual(err, ddl.ErrExists), IsTrue) tbStmt := statement("create table t (a int primary key not null, b varchar(255), key idx_b (b), c int, d int unique)").(*stmts.CreateTableStmt) err = dd.CreateTable(ctx, table.Ident{Schema: noExist, Name: tbIdent.Name}, tbStmt.Cols, tbStmt.Constraints) c.Assert(errors2.ErrorEqual(err, qerror.ErrDatabaseNotExist), IsTrue) err = dd.CreateTable(ctx, tbIdent, tbStmt.Cols, tbStmt.Constraints) c.Assert(err, IsNil) err = dd.CreateTable(ctx, tbIdent, tbStmt.Cols, tbStmt.Constraints) c.Assert(errors2.ErrorEqual(err, ddl.ErrExists), IsTrue) tbIdent2 := tbIdent tbIdent2.Name = model.NewCIStr("t2") tbStmt = statement("create table t2 (a int unique not null)").(*stmts.CreateTableStmt) err = dd.CreateTable(ctx, tbIdent2, tbStmt.Cols, tbStmt.Constraints) c.Assert(err, IsNil) tb, err := handle.Get().TableByName(tbIdent.Schema, tbIdent.Name) c.Assert(err, IsNil) _, err = tb.AddRecord(ctx, []interface{}{1, "b", 2, 4}) c.Assert(err, IsNil) alterStmt := statement("alter table t add column aa int first").(*stmts.AlterTableStmt) dd.AlterTable(ctx, tbIdent, alterStmt.Specs) c.Assert(alterStmt.Specs[0].String(), Not(Equals), "") // Check indices info is := handle.Get() tbl, err := is.TableByName(schemaName, tblName) c.Assert(err, IsNil) c.Assert(tbl, NotNil) expectedOffset := make(map[string]int) expectedOffset["a"] = 1 expectedOffset["b"] = 2 expectedOffset["d"] = 4 for _, idx := range tbl.Indices() { for _, col := range idx.Columns { o, ok := expectedOffset[col.Name.L] c.Assert(ok, IsTrue) c.Assert(col.Offset, Equals, o) } } alterStmt = statement("alter table t add column bb int after b").(*stmts.AlterTableStmt) err = dd.AlterTable(ctx, tbIdent, alterStmt.Specs) c.Assert(err, IsNil) c.Assert(alterStmt.Specs[0].String(), Not(Equals), "") // Inserting a duplicated column will cause error. alterStmt = statement("alter table t add column bb int after b").(*stmts.AlterTableStmt) err = dd.AlterTable(ctx, tbIdent, alterStmt.Specs) c.Assert(err, NotNil) idxStmt := statement("CREATE INDEX idx_c ON t (c)").(*stmts.CreateIndexStmt) idxName := model.NewCIStr(idxStmt.IndexName) err = dd.CreateIndex(ctx, tbIdent, idxStmt.Unique, idxName, idxStmt.IndexColNames) c.Assert(err, IsNil) tbs := handle.Get().SchemaTables(tbIdent.Schema) c.Assert(len(tbs), Equals, 2) err = dd.DropIndex(ctx, tbIdent.Schema, tbIdent.Name, idxName) c.Assert(err, IsNil) err = dd.DropTable(ctx, tbIdent) c.Assert(err, IsNil) tbs = handle.Get().SchemaTables(tbIdent.Schema) c.Assert(len(tbs), Equals, 1) err = dd.DropSchema(ctx, noExist) c.Assert(errors2.ErrorEqual(err, ddl.ErrNotExists), IsTrue) err = dd.DropSchema(ctx, tbIdent.Schema) c.Assert(err, IsNil) }