Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao committed Aug 27, 2018
1 parent ad05169 commit a9f82e6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
29 changes: 20 additions & 9 deletions executor/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/privilege/privileges"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
Expand Down Expand Up @@ -155,17 +154,15 @@ func (e *DDLExec) executeCreateIndex(s *ast.CreateIndexStmt) error {
return errors.Trace(err)
}

var errForbidDrop = errors.New("Drop mysql tables is forbidden")
var errForbidDrop = errors.New("Drop tidb system tables is forbidden")

func (e *DDLExec) executeDropDatabase(s *ast.DropDatabaseStmt) error {
dbName := model.NewCIStr(s.Name)

// Protect important system table from been dropped by a mistake.
// I can hardly find a case that a user really need to do this.
if dbName.L == "mysql" {
if !privileges.SkipWithGrant {
return errors.Trace(errForbidDrop)
}
return errors.Trace(errForbidDrop)
}

err := domain.GetDomain(e.ctx).DDL().DropSchema(e.ctx, dbName)
Expand All @@ -191,6 +188,22 @@ func (e *DDLExec) executeDropDatabase(s *ast.DropDatabaseStmt) error {
return errors.Trace(err)
}

var systemTables = map[string]struct{}{
"tidb": {},
"gc_delete_range": {},
"gc_delete_range_done": {},
}

func isSystemTable(schema, table string) bool {
if schema != "mysql" {
return false
}
if _, ok := systemTables[table]; ok {
return true
}
return false
}

func (e *DDLExec) executeDropTable(s *ast.DropTableStmt) error {
var notExistTables []string
for _, tn := range s.Tables {
Expand All @@ -212,10 +225,8 @@ func (e *DDLExec) executeDropTable(s *ast.DropTableStmt) error {

// Protect important system table from been dropped by a mistake.
// I can hardly find a case that a user really need to do this.
if tn.Schema.L == "mysql" {
if !privileges.SkipWithGrant {
return errors.Trace(errForbidDrop)
}
if isSystemTable(tn.Schema.L, tn.Name.L) {
return errors.Trace(errForbidDrop)
}

if config.CheckTableBeforeDrop {
Expand Down
2 changes: 1 addition & 1 deletion executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *testSuite) TestCreateDropTable(c *C) {
tk.MustExec("create table drop_test (a int)")
tk.MustExec("drop table drop_test")

_, err := tk.Exec("drop table mysql.user")
_, err := tk.Exec("drop table mysql.gc_delete_range")
c.Assert(err, NotNil)
}

Expand Down
6 changes: 0 additions & 6 deletions privilege/privileges/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,6 @@ func (s *testCacheSuite) TestAbnormalMySQLTable(c *C) {
c.Assert(err, IsNil)
defer se.Close()

saveSkipWithGrant := privileges.SkipWithGrant
defer func() {
privileges.SkipWithGrant = saveSkipWithGrant
}()
privileges.SkipWithGrant = true

// Simulate the case mysql.user is synchronized from MySQL.
mustExec(c, se, "DROP TABLE mysql.user;")
mustExec(c, se, "USE mysql;")
Expand Down

0 comments on commit a9f82e6

Please sign in to comment.