From 50f5737ac1789ae3cb0d6d8e133035244a84caf0 Mon Sep 17 00:00:00 2001 From: 5kbpers Date: Fri, 10 Jan 2020 17:55:15 +0800 Subject: [PATCH] address comments Signed-off-by: 5kbpers --- pkg/restore/db.go | 5 ++++- pkg/restore/db_test.go | 12 ++++++------ pkg/restore/util.go | 9 +++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pkg/restore/db.go b/pkg/restore/db.go index b6ca2ea80..b114b7629 100644 --- a/pkg/restore/db.go +++ b/pkg/restore/db.go @@ -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", diff --git a/pkg/restore/db_test.go b/pkg/restore/db_test.go index 00dd4895e..9583f7f8c 100644 --- a/pkg/restore/db_test.go +++ b/pkg/restore/db_test.go @@ -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(), @@ -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)) } diff --git a/pkg/restore/util.go b/pkg/restore/util.go index 6dddb5aa5..126e864fd 100644 --- a/pkg/restore/util.go +++ b/pkg/restore/util.go @@ -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 +}