Skip to content

Commit

Permalink
add more UT
Browse files Browse the repository at this point in the history
Signed-off-by: lance6716 <lance6716@gmail.com>
  • Loading branch information
lance6716 committed Jul 11, 2022
1 parent b8ea465 commit f6a33be
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 30 deletions.
7 changes: 6 additions & 1 deletion ddl/db_rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,13 @@ func renameTableTest(t *testing.T, sql string, isAlterTable bool) {
}

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

ddlChecker := schematracker.NewChecker(dom.DDL())
dom.SetDDL(ddlChecker)
ddlChecker.CreateTestDB()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1(id int)")
Expand Down
40 changes: 39 additions & 1 deletion ddl/db_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func TestCreateTableWithIntegerColWithDefault(t *testing.T) {
ddlChecker := schematracker.NewChecker(dom.DDL())
dom.SetDDL(ddlChecker)
ddlChecker.CreateTestDB()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
// It's for failure cases.
Expand Down Expand Up @@ -929,3 +929,41 @@ func TestAddColumn2(t *testing.T) {
re.Check(testkit.Rows("1 2"))
tk.MustQuery("select a,b,_tidb_rowid from t2").Check(testkit.Rows("1 3 2"))
}

func TestDropTables(t *testing.T) {
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()

ddlChecker := schematracker.NewChecker(dom.DDL())
dom.SetDDL(ddlChecker)
ddlChecker.CreateTestDB()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1;")

failedSQL := "drop table t1;"
tk.MustGetErrCode(failedSQL, errno.ErrBadTable)
failedSQL = "drop table test2.t1;"
tk.MustGetErrCode(failedSQL, errno.ErrBadTable)

tk.MustExec("create table t1 (a int);")
tk.MustExec("drop table if exists t1, t2;")

tk.MustExec("create table t1 (a int);")
tk.MustExec("drop table if exists t2, t1;")

// Without IF EXISTS, the statement drops all named tables that do exist, and returns an error indicating which
// nonexisting tables it was unable to drop.
// https://dev.mysql.com/doc/refman/5.7/en/drop-table.html
tk.MustExec("create table t1 (a int);")
failedSQL = "drop table t1, t2;"
tk.MustGetErrCode(failedSQL, errno.ErrBadTable)

tk.MustExec("create table t1 (a int);")
failedSQL = "drop table t2, t1;"
tk.MustGetErrCode(failedSQL, errno.ErrBadTable)

failedSQL = "show create table t1;"
tk.MustGetErrCode(failedSQL, errno.ErrNoSuchTable)
}
68 changes: 42 additions & 26 deletions ddl/schematracker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ func (d Checker) checkDBInfo(ctx sessionctx.Context, dbName model.CIStr) {
dbInfo, _ := d.realDDL.GetInfoSchemaWithInterceptor(ctx).SchemaByName(dbName)
dbInfo2 := d.tracker.SchemaByName(dbName)

if dbInfo == nil || dbInfo2 == nil {
if dbInfo == nil && dbInfo2 == nil {
return
}
if dbInfo == nil {
errStr := fmt.Sprintf("%s should not exist", dbName)
panic(errStr)
}
if dbInfo2 == nil {
errStr := fmt.Sprintf("%s should exist", dbName)
panic(errStr)
}
}

result := bytes.NewBuffer(make([]byte, 0, 512))
err := executor.ConstructResultOfShowCreateDatabase(ctx, dbInfo, false, result)
if err != nil {
Expand All @@ -99,13 +113,24 @@ func (d Checker) checkTableInfo(ctx sessionctx.Context, dbName, tableName model.
}

tableInfo, _ := d.realDDL.GetInfoSchemaWithInterceptor(ctx).TableByName(dbName, tableName)
tableInfo2, err := d.tracker.TableByName(dbName, tableName)
if err != nil {
panic(err)
tableInfo2, _ := d.tracker.TableByName(dbName, tableName)

if tableInfo == nil || tableInfo2 == nil {
if tableInfo == nil && tableInfo2 == nil {
return
}
if tableInfo == nil {
errStr := fmt.Sprintf("%s.%s should not exist", dbName, tableName)
panic(errStr)
}
if tableInfo2 == nil {
errStr := fmt.Sprintf("%s.%s should exist", dbName, tableName)
panic(errStr)
}
}

result := bytes.NewBuffer(make([]byte, 0, 512))
err = executor.ConstructResultOfShowCreateTable(ctx, tableInfo.Meta(), autoid.Allocators{}, result)
err := executor.ConstructResultOfShowCreateTable(ctx, tableInfo.Meta(), autoid.Allocators{}, result)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -162,6 +187,8 @@ func (d Checker) DropSchema(ctx sessionctx.Context, stmt *ast.DropDatabaseStmt)
if err != nil {
panic(err)
}

d.checkDBInfo(ctx, stmt.Name)
return nil
}

Expand Down Expand Up @@ -201,14 +228,12 @@ func (d Checker) CreateView(ctx sessionctx.Context, stmt *ast.CreateViewStmt) er
// DropTable implements the DDL interface.
func (d Checker) DropTable(ctx sessionctx.Context, stmt *ast.DropTableStmt) (err error) {
err = d.realDDL.DropTable(ctx, stmt)
if err != nil {
return err
}
err = d.tracker.DropTable(ctx, stmt)
if err != nil {
panic(err)
_ = d.tracker.DropTable(ctx, stmt)

for _, tableName := range stmt.Tables {
d.checkTableInfo(ctx, tableName.Schema, tableName.Name)
}
return nil
return err
}

// RecoverTable implements the DDL interface.
Expand All @@ -227,6 +252,10 @@ func (d Checker) DropView(ctx sessionctx.Context, stmt *ast.DropTableStmt) (err
if err != nil {
panic(err)
}

for _, tableName := range stmt.Tables {
d.checkTableInfo(ctx, tableName.Schema, tableName.Name)
}
return nil
}

Expand Down Expand Up @@ -271,22 +300,9 @@ func (d Checker) RenameTable(ctx sessionctx.Context, stmt *ast.RenameTableStmt)
panic(err)
}

check := func(dbName, tableName model.CIStr) {
tableInfo, _ := d.realDDL.GetInfoSchemaWithInterceptor(ctx).TableByName(dbName, tableName)
if tableInfo == nil {
tableInfo2, _ := d.tracker.TableByName(dbName, tableName)
if tableInfo2 != nil {
errStr := fmt.Sprintf("%s.%s should not exist", dbName, tableName)
panic(errStr)
}
} else {
d.checkTableInfo(ctx, dbName, tableName)
}
}

for _, tableName := range stmt.TableToTables {
check(tableName.OldTable.Schema, tableName.OldTable.Name)
check(tableName.NewTable.Schema, tableName.NewTable.Name)
d.checkTableInfo(ctx, tableName.OldTable.Schema, tableName.OldTable.Name)
d.checkTableInfo(ctx, tableName.NewTable.Schema, tableName.NewTable.Name)
}
return nil
}
Expand Down
6 changes: 6 additions & 0 deletions ddl/schematracker/dm_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ func (d SchemaTracker) CreateTable(ctx sessionctx.Context, s *ast.CreateTableStm
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(ident.Schema)
}

// suppress ErrTooLongKey
ctx.GetSessionVars().StrictSQLMode = false

var (
referTbl *model.TableInfo
err error
Expand Down Expand Up @@ -281,6 +284,9 @@ func (d SchemaTracker) DropTable(ctx sessionctx.Context, stmt *ast.DropTableStmt
continue
}

// Without IF EXISTS, the statement drops all named tables that do exist, and returns an error indicating which
// nonexisting tables it was unable to drop.
// https://dev.mysql.com/doc/refman/5.7/en/drop-table.html
_ = d.DeleteTable(name.Schema, name.Name)
}

Expand Down
14 changes: 14 additions & 0 deletions ddl/schematracker/dm_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,17 @@ func TestCreateTableNoNumLimit(t *testing.T) {
err = tracker.CreateTable(sctx, stmt.(*ast.CreateTableStmt))
require.NoError(t, err)
}

func TestCreateTableLongIndex(t *testing.T) {
sql := "create table test.t (c1 int, c2 blob, c3 varchar(64), index idx_c2(c2(555555)));"

sctx := mock.NewContext()
p := parser.New()
stmt, err := p.ParseOneStmt(sql, "", "")
require.NoError(t, err)

tracker := NewSchemaTracker(2)
tracker.createTestDB()
err = tracker.CreateTable(sctx, stmt.(*ast.CreateTableStmt))
require.NoError(t, err)
}
14 changes: 12 additions & 2 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1499,8 +1499,13 @@ func TestRenameTable(t *testing.T) {
defer func() {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/meta/autoid/mockAutoIDChange"))
}()
store, clean := testkit.CreateMockStore(t)
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()

ddlChecker := schematracker.NewChecker(dom.DDL())
dom.SetDDL(ddlChecker)
ddlChecker.CreateTestDB()

tk := testkit.NewTestKit(t, store)

tk.MustExec("drop database if exists rename1")
Expand Down Expand Up @@ -1582,8 +1587,13 @@ func TestRenameMultiTables(t *testing.T) {
defer func() {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/meta/autoid/mockAutoIDChange"))
}()
store, clean := testkit.CreateMockStore(t)
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()

ddlChecker := schematracker.NewChecker(dom.DDL())
dom.SetDDL(ddlChecker)
ddlChecker.CreateTestDB()

tk := testkit.NewTestKit(t, store)

tk.MustExec("drop database if exists rename1")
Expand Down

0 comments on commit f6a33be

Please sign in to comment.