ddl: support rename the same table name (#6391)
This commit is contained in:
@ -1620,6 +1620,10 @@ func (d *ddl) RenameTable(ctx sessionctx.Context, oldIdent, newIdent ast.Ident)
|
||||
if err != nil {
|
||||
return errFileNotFound.GenByArgs(oldIdent.Schema, oldIdent.Name)
|
||||
}
|
||||
if newIdent.Schema.L == oldIdent.Schema.L && newIdent.Name.L == oldIdent.Name.L {
|
||||
// oldIdent is equal to newIdent, do nothing
|
||||
return nil
|
||||
}
|
||||
newSchema, ok := is.SchemaByName(newIdent.Schema)
|
||||
if !ok {
|
||||
return errErrorOnRename.GenByArgs(oldIdent.Schema, oldIdent.Name, newIdent.Schema, newIdent.Name)
|
||||
|
||||
@ -1535,6 +1535,7 @@ func (s *testDBSuite) testRenameTable(c *C, sql string) {
|
||||
c.Assert(newTblInfo.Meta().ID, Equals, oldTblID)
|
||||
s.tk.MustQuery("select * from t1").Check(testkit.Rows("1 1", "2 2"))
|
||||
s.tk.MustExec("use test")
|
||||
|
||||
// Make sure t doesn't exist.
|
||||
s.tk.MustExec("create table t (c1 int, c2 int)")
|
||||
s.tk.MustExec("drop table t")
|
||||
@ -1554,12 +1555,19 @@ func (s *testDBSuite) testRenameTable(c *C, sql string) {
|
||||
// for failure case
|
||||
failSQL := fmt.Sprintf(sql, "test_not_exist.t", "test_not_exist.t")
|
||||
s.testErrorCode(c, failSQL, tmysql.ErrFileNotFound)
|
||||
failSQL = fmt.Sprintf(sql, "test.test_not_exist", "test.test_not_exist")
|
||||
s.testErrorCode(c, failSQL, tmysql.ErrFileNotFound)
|
||||
failSQL = fmt.Sprintf(sql, "test.t_not_exist", "test_not_exist.t")
|
||||
s.testErrorCode(c, failSQL, tmysql.ErrFileNotFound)
|
||||
failSQL = fmt.Sprintf(sql, "test1.t2", "test_not_exist.t")
|
||||
s.testErrorCode(c, failSQL, tmysql.ErrErrorOnRename)
|
||||
failSQL = fmt.Sprintf(sql, "test1.t2", "test1.t2")
|
||||
s.testErrorCode(c, failSQL, tmysql.ErrTableExists)
|
||||
|
||||
// for the same table name
|
||||
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"))
|
||||
|
||||
s.tk.MustExec("drop database test1")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user