Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
ignore exist
Browse files Browse the repository at this point in the history
  • Loading branch information
GMHDBJD authored and ti-chi-bot committed May 19, 2021
1 parent 56c7a8a commit bdd3896
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
12 changes: 1 addition & 11 deletions pkg/schema/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,9 @@ func cloneTableInfo(ti *model.TableInfo) *model.TableInfo {

// CreateTableIfNotExists creates a TABLE of the given name if it did not exist.
func (tr *Tracker) CreateTableIfNotExists(db, table string, ti *model.TableInfo) error {
infoSchema := tr.dom.InfoSchema()
dbName := model.NewCIStr(db)
tableName := model.NewCIStr(table)
if infoSchema.TableExists(dbName, tableName) {
return nil
}

dbInfo, exists := infoSchema.SchemaByName(dbName)
if !exists || dbInfo == nil {
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbName)
}

ti = cloneTableInfo(ti)
ti.Name = tableName
return tr.dom.DDL().CreateTableWithInfo(tr.se, dbName, ti, ddl.OnExistError, false)
return tr.dom.DDL().CreateTableWithInfo(tr.se, dbName, ti, ddl.OnExistIgnore, false)
}
4 changes: 4 additions & 0 deletions pkg/schema/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ func (s *trackerSuite) TestCreateTableIfNotExists(c *C) {
err = tracker.CreateTableIfNotExists("testdb", "foo", ti1)
c.Assert(err, IsNil)

// error if db not exist
err = tracker.CreateTableIfNotExists("test-another-db", "foo", ti1)
c.Assert(err, ErrorMatches, ".*Unknown database.*")

// Can use the table info to recover a table using a different name.
err = tracker.CreateTableIfNotExists("testdb", "bar", ti1)
c.Assert(err, IsNil)
Expand Down

0 comments on commit bdd3896

Please sign in to comment.