-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
executor: forbid user to drop important system table #7471
Changes from all commits
3c9d354
a7fd95c
8bbeac7
ad05169
a9f82e6
a0a844e
d872685
27b89fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,6 +156,13 @@ func (e *DDLExec) executeCreateIndex(s *ast.CreateIndexStmt) error { | |
|
||
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" { | ||
return errors.New("Drop 'mysql' database is forbidden") | ||
} | ||
|
||
err := domain.GetDomain(e.ctx).DDL().DropSchema(e.ctx, dbName) | ||
if infoschema.ErrDatabaseNotExists.Equal(err) { | ||
if s.IfExists { | ||
|
@@ -179,6 +186,24 @@ func (e *DDLExec) executeDropDatabase(s *ast.DropDatabaseStmt) error { | |
return errors.Trace(err) | ||
} | ||
|
||
// If one drop those tables by mistake, it's difficult to recover. | ||
// In the worst case, the whole TiDB cluster fails to bootstrap, so we prevent user from dropping them. | ||
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 { | ||
|
@@ -198,6 +223,12 @@ func (e *DDLExec) executeDropTable(s *ast.DropTableStmt) error { | |
return errors.Trace(err) | ||
} | ||
|
||
// 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 isSystemTable(tn.Schema.L, tn.Name.L) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about forbidding drop any tables in database There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's your opinion? @shenli @lamxTyler There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest protecting all the tables in the |
||
return errors.Errorf("Drop tidb system table '%s.%s' is forbidden", tn.Schema.L, tn.Name.L) | ||
} | ||
|
||
if config.CheckTableBeforeDrop { | ||
log.Warnf("admin check table `%s`.`%s` before drop.", fullti.Schema.O, fullti.Name.O) | ||
sql := fmt.Sprintf("admin check table `%s`.`%s`", fullti.Schema.O, fullti.Name.O) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we put it to infoschema.go? I think it is like "IsMemoryDB", we can put these together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is removed, we forbid dropping tables in "mysql" database.