Skip to content

Commit

Permalink
executor: reset default database after drop current database (pingcap…
Browse files Browse the repository at this point in the history
  • Loading branch information
XuHuaiyu authored Nov 24, 2016
1 parent 7188225 commit 856d723
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
17 changes: 16 additions & 1 deletion executor/executor_ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/privilege"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/util/types"
)

// DDLExec represents a DDL executor.
Expand Down Expand Up @@ -125,14 +127,27 @@ func (e *DDLExec) executeCreateIndex(s *ast.CreateIndexStmt) error {
}

func (e *DDLExec) executeDropDatabase(s *ast.DropDatabaseStmt) error {
err := sessionctx.GetDomain(e.ctx).DDL().DropSchema(e.ctx, model.NewCIStr(s.Name))
dbName := model.NewCIStr(s.Name)
err := sessionctx.GetDomain(e.ctx).DDL().DropSchema(e.ctx, dbName)
if terror.ErrorEqual(err, infoschema.ErrDatabaseNotExists) {
if s.IfExists {
err = nil
} else {
err = infoschema.ErrDatabaseDropExists.Gen("Can't drop database '%s'; database doesn't exist", s.Name)
}
}
sessionVars := e.ctx.GetSessionVars()
if err == nil && strings.ToLower(sessionVars.CurrentDB) == dbName.L {
sessionVars.CurrentDB = ""
err = sessionVars.SetSystemVar(variable.CharsetDatabase, types.NewStringDatum("utf8"))
if err != nil {
return errors.Trace(err)
}
err = sessionVars.SetSystemVar(variable.CollationDatabase, types.NewStringDatum("utf8_unicode_ci"))
if err != nil {
return errors.Trace(err)
}
}
return errors.Trace(err)
}

Expand Down
20 changes: 20 additions & 0 deletions executor/executor_ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,23 @@ func (s *testSuite) TestAlterTableModifyColumn(c *C) {
expected := "CREATE TABLE `mc` (\n `c1` bigint(21) DEFAULT NULL,\n `c2` text DEFAULT NULL\n) ENGINE=InnoDB"
c.Assert(createSQL, Equals, expected)
}

func (s *testSuite) TestDefaultDBAfterDropCurDB(c *C) {
defer testleak.AfterTest(c)()
tk := testkit.NewTestKit(c, s.store)

testSQL := `create database if not exists test_db CHARACTER SET latin1 COLLATE latin1_swedish_ci;`
tk.MustExec(testSQL)

testSQL = `use test_db;`
tk.MustExec(testSQL)
tk.MustQuery(`select database();`).Check(testkit.Rows("test_db"))
tk.MustQuery(`select @@character_set_database;`).Check(testkit.Rows("latin1"))
tk.MustQuery(`select @@collation_database;`).Check(testkit.Rows("latin1_swedish_ci"))

testSQL = `drop database test_db;`
tk.MustExec(testSQL)
tk.MustQuery(`select database();`).Check(testkit.Rows("<nil>"))
tk.MustQuery(`select @@character_set_database;`).Check(testkit.Rows("utf8"))
tk.MustQuery(`select @@collation_database;`).Check(testkit.Rows("utf8_unicode_ci"))
}

0 comments on commit 856d723

Please sign in to comment.