Skip to content

Commit 25952d4

Browse files
authored
ddl, executor, server: add drop if exists warnings (#14807)
1 parent 249ff99 commit 25952d4

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

ddl/sequence_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ func (s *testSequenceSuite) TestDropSequence(c *C) {
152152
_, err = tk1.Exec("drop sequence my_seq")
153153
c.Assert(err, NotNil)
154154
c.Assert(err.Error(), Equals, "[planner:1142]DROP command denied to user 'localhost'@'myuser' for table 'my_seq'")
155+
156+
// Test for `drop sequence if exists`.
157+
s.tk.MustExec("drop sequence if exists seq_if_exists")
158+
s.tk.MustQuery("show warnings;").Check(testkit.Rows("Note 4139 Unknown SEQUENCE: 'test.seq_if_exists'"))
155159
}
156160

157161
func (s *testSequenceSuite) TestShowCreateSequence(c *C) {

executor/ddl.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,16 @@ func (e *DDLExec) dropTableObject(objects []*ast.TableName, obt objectType, ifEx
334334
}
335335
return infoschema.ErrTableDropExists.GenWithStackByArgs(strings.Join(notExistTables, ","))
336336
}
337+
// We need add warning when use if exists.
338+
if len(notExistTables) > 0 && ifExists {
339+
for _, table := range notExistTables {
340+
if obt == sequenceObject {
341+
e.ctx.GetSessionVars().StmtCtx.AppendNote(infoschema.ErrSequenceDropExists.GenWithStackByArgs(table))
342+
} else {
343+
e.ctx.GetSessionVars().StmtCtx.AppendNote(infoschema.ErrTableDropExists.GenWithStackByArgs(table))
344+
}
345+
}
346+
}
337347
return nil
338348
}
339349

executor/ddl_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ func (s *testSuite6) TestCreateTable(c *C) {
173173
tk.MustExec("insert into create_auto_increment_test (name) values ('aa')")
174174
r = tk.MustQuery("select * from create_auto_increment_test;")
175175
r.Check(testkit.Rows("1000 aa"))
176+
177+
// Test for `drop table if exists`.
178+
tk.MustExec("drop table if exists t_if_exists;")
179+
tk.MustQuery("show warnings;").Check(testkit.Rows("Note 1051 Unknown table 'test.t_if_exists'"))
180+
tk.MustExec("create table if not exists t1_if_exists(c int)")
181+
tk.MustExec("drop table if exists t1_if_exists,t2_if_exists,t3_if_exists")
182+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Note|1051|Unknown table 'test.t2_if_exists'", "Note|1051|Unknown table 'test.t3_if_exists'"))
176183
}
177184

178185
func (s *testSuite6) TestCreateView(c *C) {
@@ -253,6 +260,13 @@ func (s *testSuite6) TestCreateView(c *C) {
253260
tk.MustExec("drop view v")
254261
tk.MustExec("create view v as (select * from t1 union select * from t2)")
255262
tk.MustExec("drop view v")
263+
264+
// Test for `drop view if exists`.
265+
tk.MustExec("drop view if exists v_if_exists;")
266+
tk.MustQuery("show warnings;").Check(testkit.Rows("Note 1051 Unknown table 'test.v_if_exists'"))
267+
tk.MustExec("create view v1_if_exists as (select * from t1)")
268+
tk.MustExec("drop view if exists v1_if_exists,v2_if_exists,v3_if_exists")
269+
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Note|1051|Unknown table 'test.v2_if_exists'", "Note|1051|Unknown table 'test.v3_if_exists'"))
256270
}
257271

258272
func (s *testSuite6) TestCreateDropDatabase(c *C) {

server/http_handler_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"sync/atomic"
3131
"time"
3232

33+
dmysql "github.com/go-sql-driver/mysql"
3334
. "github.com/pingcap/check"
3435
"github.com/pingcap/failpoint"
3536
zaplog "github.com/pingcap/log"
@@ -927,7 +928,10 @@ func (ts *HTTPHandlerTestSuite) TestPostSettings(c *C) {
927928
c.Assert(atomic.LoadUint32(&variable.DDLSlowOprThreshold), Equals, uint32(200))
928929

929930
// test check_mb4_value_in_utf8
930-
db, err := sql.Open("mysql", ts.getDSN())
931+
overriders := []configOverrider{func(config *dmysql.Config) {
932+
config.Strict = false
933+
}}
934+
db, err := sql.Open("mysql", ts.getDSN(overriders...))
931935
c.Assert(err, IsNil, Commentf("Error connecting"))
932936
defer db.Close()
933937
dbt := &DBTest{c, db}

server/server_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,9 @@ func (cli *testServerClient) runTestLoadData(c *C, server *Server) {
791791

792792
func (cli *testServerClient) runTestConcurrentUpdate(c *C) {
793793
dbName := "Concurrent"
794-
cli.runTestsOnNewDB(c, nil, dbName, func(dbt *DBTest) {
794+
cli.runTestsOnNewDB(c, func(config *mysql.Config) {
795+
config.Strict = false
796+
}, dbName, func(dbt *DBTest) {
795797
dbt.mustExec("drop table if exists test2")
796798
dbt.mustExec("create table test2 (a int, b int)")
797799
dbt.mustExec("insert test2 values (1, 1)")
@@ -1014,7 +1016,9 @@ func (cli *testServerClient) runTestDBNameEscape(c *C) {
10141016
}
10151017

10161018
func (cli *testServerClient) runTestResultFieldTableIsNull(c *C) {
1017-
cli.runTestsOnNewDB(c, nil, "ResultFieldTableIsNull", func(dbt *DBTest) {
1019+
cli.runTestsOnNewDB(c, func(config *mysql.Config) {
1020+
config.Strict = false
1021+
}, "ResultFieldTableIsNull", func(dbt *DBTest) {
10181022
dbt.mustExec("drop table if exists test;")
10191023
dbt.mustExec("create table test (c int);")
10201024
dbt.mustExec("explain select * from test;")

0 commit comments

Comments
 (0)