-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: Make 'IF NOT EXISTS' great again in 'CREATE TABLE IF NOT EXISTS LI…
- Loading branch information
Showing
8 changed files
with
149 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2018 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ddl_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/juju/errors" | ||
. "github.com/pingcap/check" | ||
"github.com/pingcap/tidb/domain" | ||
"github.com/pingcap/tidb/infoschema" | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/session" | ||
"github.com/pingcap/tidb/sessionctx" | ||
"github.com/pingcap/tidb/sessionctx/stmtctx" | ||
"github.com/pingcap/tidb/store/mockstore" | ||
"github.com/pingcap/tidb/terror" | ||
"github.com/pingcap/tidb/util/mock" | ||
"github.com/pingcap/tidb/util/testkit" | ||
"github.com/pingcap/tidb/util/testleak" | ||
) | ||
|
||
var _ = Suite(&testIntegrationSuite{}) | ||
|
||
type testIntegrationSuite struct { | ||
store kv.Storage | ||
dom *domain.Domain | ||
ctx sessionctx.Context | ||
} | ||
|
||
func (s *testIntegrationSuite) TearDownTest(c *C) { | ||
tk := testkit.NewTestKit(c, s.store) | ||
tk.MustExec("use test") | ||
r := tk.MustQuery("show tables") | ||
for _, tb := range r.Rows() { | ||
tableName := tb[0] | ||
tk.MustExec(fmt.Sprintf("drop table %v", tableName)) | ||
} | ||
} | ||
|
||
func (s *testIntegrationSuite) SetUpSuite(c *C) { | ||
var err error | ||
testleak.BeforeTest() | ||
s.store, s.dom, err = newStoreWithBootstrap() | ||
c.Assert(err, IsNil) | ||
s.ctx = mock.NewContext() | ||
} | ||
|
||
func (s *testIntegrationSuite) TearDownSuite(c *C) { | ||
s.dom.Close() | ||
s.store.Close() | ||
testleak.AfterTest(c)() | ||
} | ||
|
||
// for issue #6879 | ||
func (s *testIntegrationSuite) TestCreateTableIfNotExists(c *C) { | ||
tk := testkit.NewTestKit(c, s.store) | ||
|
||
tk.MustExec("USE test;") | ||
|
||
tk.MustExec("create table t1(a bigint)") | ||
tk.MustExec("create table t(a bigint)") | ||
|
||
// Test duplicate create-table with `LIKE` clause | ||
tk.MustExec("create table if not exists t like t1;") | ||
warnings := tk.Se.GetSessionVars().StmtCtx.GetWarnings() | ||
c.Assert(len(warnings), GreaterEqual, 1) | ||
lastWarn := warnings[len(warnings)-1] | ||
c.Assert(terror.ErrorEqual(infoschema.ErrTableExists, lastWarn.Err), IsTrue) | ||
c.Assert(lastWarn.Level, Equals, stmtctx.WarnLevelNote) | ||
|
||
// Test duplicate create-table without `LIKE` clause | ||
tk.MustExec("create table if not exists t(b bigint, c varchar(60));") | ||
warnings = tk.Se.GetSessionVars().StmtCtx.GetWarnings() | ||
c.Assert(len(warnings), GreaterEqual, 1) | ||
lastWarn = warnings[len(warnings)-1] | ||
c.Assert(terror.ErrorEqual(infoschema.ErrTableExists, lastWarn.Err), IsTrue) | ||
} | ||
|
||
func newStoreWithBootstrap() (kv.Storage, *domain.Domain, error) { | ||
store, err := mockstore.NewMockTikvStore() | ||
if err != nil { | ||
return nil, nil, errors.Trace(err) | ||
} | ||
session.SetSchemaLease(0) | ||
session.SetStatsLease(0) | ||
dom, err := session.BootstrapSession(store) | ||
return store, dom, errors.Trace(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters