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

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
Signed-off-by: 5kbpers <tangminghua@pingcap.com>
  • Loading branch information
5kbpers committed Jan 10, 2020
1 parent 807c840 commit 50f5737
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
5 changes: 4 additions & 1 deletion pkg/restore/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ func (db *DB) CreateTable(ctx context.Context, table *utils.Table) error {
zap.Error(err))
return errors.Trace(err)
}
alterAutoIncIDSQL := fmt.Sprintf("alter table %s auto_increment = %d", schema.Name, schema.AutoIncID)
alterAutoIncIDSQL := fmt.Sprintf(
"alter table %s auto_increment = %d",
escapeTableName(schema.Name),
schema.AutoIncID)
_, err = db.se.Execute(ctx, alterAutoIncIDSQL)
if err != nil {
log.Error("alter AutoIncID failed",
Expand Down
12 changes: 6 additions & 6 deletions pkg/restore/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ func (s *testRestoreSchemaSuite) TestRestoreAutoIncID(c *C) {
tk := testkit.NewTestKit(c, s.mock.Storage)
tk.MustExec("use test")
tk.MustExec("set @@sql_mode=''")
tk.MustExec("drop table if exists t;")
tk.MustExec("drop table if exists `\"t\"`;")
// Test SQL Mode
tk.MustExec("create table t (" +
tk.MustExec("create table `\"t\"` (" +
"a int not null," +
"time timestamp not null default '0000-00-00 00:00:00'," +
"primary key (a));",
)
tk.MustExec("insert into t values (10, '0000-00-00 00:00:00');")
tk.MustExec("insert into `\"t\"` values (10, '0000-00-00 00:00:00');")
// Query the current AutoIncID
autoIncID, err := strconv.ParseUint(tk.MustQuery("admin show t next_row_id").Rows()[0][3].(string), 10, 64)
autoIncID, err := strconv.ParseUint(tk.MustQuery("admin show `\"t\"` next_row_id").Rows()[0][3].(string), 10, 64)
c.Assert(err, IsNil, Commentf("Error query auto inc id: %s", err))
// Get schemas of db and table
info, err := s.mock.Domain.GetSnapshotInfoSchema(math.MaxUint64)
c.Assert(err, IsNil, Commentf("Error get snapshot info schema: %s", err))
dbInfo, exists := info.SchemaByName(model.NewCIStr("test"))
c.Assert(exists, IsTrue, Commentf("Error get db info"))
tableInfo, err := info.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
tableInfo, err := info.TableByName(model.NewCIStr("test"), model.NewCIStr("\"t\""))
c.Assert(err, IsNil, Commentf("Error get table info: %s", err))
table := utils.Table{
Schema: tableInfo.Meta(),
Expand Down Expand Up @@ -88,7 +88,7 @@ func (s *testRestoreSchemaSuite) TestRestoreAutoIncID(c *C) {
c.Assert(err, IsNil, Commentf("Error create table: %s %s", err, s.mock.DSN))
tk.MustExec("use test")
// Check if AutoIncID is altered successfully
autoIncID, err = strconv.ParseUint(tk.MustQuery("admin show t next_row_id").Rows()[0][3].(string), 10, 64)
autoIncID, err = strconv.ParseUint(tk.MustQuery("admin show `\"t\"` next_row_id").Rows()[0][3].(string), 10, 64)
c.Assert(err, IsNil, Commentf("Error query auto inc id: %s", err))
c.Assert(autoIncID, Equals, uint64(globalAutoID+100))
}
9 changes: 9 additions & 0 deletions pkg/restore/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,12 @@ func encodeKeyPrefix(key []byte) []byte {
encodedPrefix = append(encodedPrefix, codec.EncodeBytes([]byte{}, key[:len(key)-ungroupedLen])...)
return append(encodedPrefix[:len(encodedPrefix)-9], key[len(key)-ungroupedLen:]...)
}

// escape the identifier for pretty-printing.
// For instance, the identifier "foo `bar`" will become "`foo ``bar```".
// The sqlMode controls whether to escape with backquotes (`) or double quotes
// (`"`) depending on whether mysql.ModeANSIQuotes is enabled.
func escapeTableName(cis model.CIStr) string {
quote := "`"
return quote + strings.Replace(cis.O, quote, quote+quote, -1) + quote
}

0 comments on commit 50f5737

Please sign in to comment.