Skip to content

Commit

Permalink
ddl: fix the bug that VIEWs can be dropped by DROP TABLE syn… (#14052)
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored and zz-jason committed Dec 28, 2019
1 parent 8f13cf1 commit 64abbbd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ddl/ddl_api.go
Expand Up @@ -2991,6 +2991,10 @@ func (d *ddl) DropTable(ctx sessionctx.Context, ti ast.Ident) (err error) {
return errors.Trace(err)
}

if tb.Meta().IsView() {
return infoschema.ErrTableNotExists.GenWithStackByArgs(ti.Schema, ti.Name)
}

job := &model.Job{
SchemaID: schema.ID,
TableID: tb.Meta().ID,
Expand Down
6 changes: 5 additions & 1 deletion executor/ddl_test.go
Expand Up @@ -286,7 +286,11 @@ func (s *testSuite3) TestCreateDropView(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create or replace view drop_test as select 1,2")
_, err := tk.Exec("drop view if exists drop_test")

_, err := tk.Exec("drop table drop_test")
c.Assert(err.Error(), Equals, "[schema:1051]Unknown table 'test.drop_test'")

_, err = tk.Exec("drop view if exists drop_test")
c.Assert(err, IsNil)

_, err = tk.Exec("drop view mysql.gc_delete_range")
Expand Down
11 changes: 9 additions & 2 deletions session/session_test.go
Expand Up @@ -85,10 +85,17 @@ func (s *testSessionSuite) TearDownSuite(c *C) {

func (s *testSessionSuite) TearDownTest(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
r := tk.MustQuery("show tables")
r := tk.MustQuery("show full tables")
for _, tb := range r.Rows() {
tableName := tb[0]
tk.MustExec(fmt.Sprintf("drop table %v", tableName))
tableType := tb[1]
if tableType == "VIEW" {
tk.MustExec(fmt.Sprintf("drop view %v", tableName))
} else if tableType == "BASE TABLE" {
tk.MustExec(fmt.Sprintf("drop table %v", tableName))
} else {
panic(fmt.Sprintf("Unexpected table '%s' with type '%s'.", tableName, tableType))
}
}
}

Expand Down

0 comments on commit 64abbbd

Please sign in to comment.