From ef7082da1cb2da10bee4ad70eb6ec9412bc96a90 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 22 Dec 2018 22:19:41 +0800 Subject: [PATCH] ddl, executor: fix rename table compatibility (#8709) --- ddl/db_test.go | 17 ++++++++++++----- ddl/ddl.go | 2 +- ddl/ddl_api.go | 7 ++++--- executor/ddl.go | 3 ++- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/ddl/db_test.go b/ddl/db_test.go index bc07e194ba39d..72f2237d8193c 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -1431,14 +1431,16 @@ func (s *testDBSuite) TestTruncateTable(c *C) { } func (s *testDBSuite) TestRenameTable(c *C) { - s.testRenameTable(c, "rename table %s to %s") + isAlterTable := false + s.testRenameTable(c, "rename table %s to %s", isAlterTable) } func (s *testDBSuite) TestAlterTableRenameTable(c *C) { - s.testRenameTable(c, "alter table %s rename to %s") + isAlterTable := true + s.testRenameTable(c, "alter table %s rename to %s", isAlterTable) } -func (s *testDBSuite) testRenameTable(c *C, sql string) { +func (s *testDBSuite) testRenameTable(c *C, sql string, isAlterTable bool) { s.tk = testkit.NewTestKit(c, s.store) s.tk.MustExec("use test") // for different databases @@ -1489,8 +1491,13 @@ func (s *testDBSuite) testRenameTable(c *C, sql string) { s.tk.MustExec("use test1") s.tk.MustExec("create table if not exists t (c1 int, c2 int)") s.tk.MustExec("create table if not exists t1 (c1 int, c2 int)") - s.tk.MustExec(fmt.Sprintf(sql, "test1.t", "t")) - s.tk.MustExec(fmt.Sprintf(sql, "test1.t1", "test1.t1")) + if isAlterTable { + s.tk.MustExec(fmt.Sprintf(sql, "test1.t", "t")) + s.tk.MustExec(fmt.Sprintf(sql, "test1.t1", "test1.T1")) + } else { + s.testErrorCode(c, fmt.Sprintf(sql, "test1.t", "t"), tmysql.ErrTableExists) + s.testErrorCode(c, fmt.Sprintf(sql, "test1.t1", "test1.T1"), tmysql.ErrTableExists) + } s.tk.MustExec("drop database test1") } diff --git a/ddl/ddl.go b/ddl/ddl.go index 74fc80138933c..026f0f08b5b97 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -218,7 +218,7 @@ type DDL interface { DropIndex(ctx sessionctx.Context, tableIdent ast.Ident, indexName model.CIStr) error AlterTable(ctx sessionctx.Context, tableIdent ast.Ident, spec []*ast.AlterTableSpec) error TruncateTable(ctx sessionctx.Context, tableIdent ast.Ident) error - RenameTable(ctx sessionctx.Context, oldTableIdent, newTableIdent ast.Ident) error + RenameTable(ctx sessionctx.Context, oldTableIdent, newTableIdent ast.Ident, isAlterTable bool) error // GetLease returns current schema lease time. GetLease() time.Duration diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 60e8c320f94ae..b9fbbf80e9c51 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1291,7 +1291,8 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A err = d.AlterColumn(ctx, ident, spec) case ast.AlterTableRenameTable: newIdent := ast.Ident{Schema: spec.NewTable.Schema, Name: spec.NewTable.Name} - err = d.RenameTable(ctx, ident, newIdent) + isAlterTable := true + err = d.RenameTable(ctx, ident, newIdent, isAlterTable) case ast.AlterTableDropPrimaryKey: err = ErrUnsupportedModifyPrimaryKey.GenWithStackByArgs("drop") case ast.AlterTableRenameIndex: @@ -2237,7 +2238,7 @@ func (d *ddl) TruncateTable(ctx sessionctx.Context, ti ast.Ident) error { return errors.Trace(err) } -func (d *ddl) RenameTable(ctx sessionctx.Context, oldIdent, newIdent ast.Ident) error { +func (d *ddl) RenameTable(ctx sessionctx.Context, oldIdent, newIdent ast.Ident, isAlterTable bool) error { is := d.GetInformationSchema(ctx) oldSchema, ok := is.SchemaByName(oldIdent.Schema) if !ok { @@ -2247,7 +2248,7 @@ func (d *ddl) RenameTable(ctx sessionctx.Context, oldIdent, newIdent ast.Ident) if err != nil { return errFileNotFound.GenWithStackByArgs(oldIdent.Schema, oldIdent.Name) } - if newIdent.Schema.L == oldIdent.Schema.L && newIdent.Name.L == oldIdent.Name.L { + if isAlterTable && newIdent.Schema.L == oldIdent.Schema.L && newIdent.Name.L == oldIdent.Name.L { // oldIdent is equal to newIdent, do nothing return nil } diff --git a/executor/ddl.go b/executor/ddl.go index 16c21fee5496b..802b0fc99d527 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -129,7 +129,8 @@ func (e *DDLExec) executeRenameTable(s *ast.RenameTableStmt) error { } oldIdent := ast.Ident{Schema: s.OldTable.Schema, Name: s.OldTable.Name} newIdent := ast.Ident{Schema: s.NewTable.Schema, Name: s.NewTable.Name} - err := domain.GetDomain(e.ctx).DDL().RenameTable(e.ctx, oldIdent, newIdent) + isAlterTable := false + err := domain.GetDomain(e.ctx).DDL().RenameTable(e.ctx, oldIdent, newIdent, isAlterTable) return errors.Trace(err) }