From d3bdef71e97a4af420c7e5fb567c4d4d3cd7731a Mon Sep 17 00:00:00 2001 From: bb7133 Date: Fri, 13 Dec 2019 11:55:41 +0800 Subject: [PATCH] ddl: fix the bug that VIEWs can be dropped by `DROP TABLE` syntax --- ddl/ddl_api.go | 4 ++++ executor/ddl_test.go | 6 +++++- session/session_test.go | 11 +++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 923fd2bdfacaf..aa77ef128682b 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -3142,6 +3142,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, diff --git a/executor/ddl_test.go b/executor/ddl_test.go index f3c10fdd44339..38493e6e519e8 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -286,7 +286,11 @@ func (s *testSuite6) 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") diff --git a/session/session_test.go b/session/session_test.go index 44487241af18d..8839fb6b44236 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -99,10 +99,17 @@ func (s *testSessionSuiteBase) TearDownSuite(c *C) { func (s *testSessionSuiteBase) 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)) + } } }