Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkg/restore/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/log"
Expand Down Expand Up @@ -86,6 +87,22 @@ func (db *DB) CreateTable(ctx context.Context, table *utils.Table) error {
return errors.Trace(err)
}
createSQL := buf.String()
// Insert `IF NOT EXISTS` statement to skip the created tables
words := strings.Split(createSQL, " ")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use SplitN and limits n to 3.

if len(words) > 2 && strings.ToUpper(words[0]) == "CREATE" && strings.ToUpper(words[1]) == "TABLE" {
resWords := append([]string{}, words[:2]...)
resWords = append(resWords, "IF NOT EXISTS")
resWords = append(resWords, words[2:]...)
createSQL = strings.Join(resWords, " ")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then just write "create table if not exists " + words[2] here.

} else {
log.Error(
"build create table SQL error",
zap.Stringer("db", table.Db.Name),
zap.Stringer("table", schema.Name),
zap.String("SQL", createSQL),
zap.Error(err))
return errors.New("build create table SQL failed")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to immediately bail out if the SQL does not start with CREATE TABLE.

}
_, err = db.se.Execute(ctx, createSQL)
if err != nil {
log.Error("create table failed",
Expand Down
4 changes: 3 additions & 1 deletion pkg/restore/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *testRestoreSchemaSuite) TearDownSuite(c *C) {
testleak.AfterTest(c)()
}

func (s *testRestoreSchemaSuite) TestRestoreAutoIncID(c *C) {
func (s *testRestoreSchemaSuite) TestRestoreTableSchema(c *C) {
c.Assert(s.mock.Start(), IsNil)
defer s.mock.Stop()

Expand Down Expand Up @@ -86,6 +86,8 @@ func (s *testRestoreSchemaSuite) TestRestoreAutoIncID(c *C) {
c.Assert(err, IsNil, Commentf("Error create empty charset db: %s %s", err, s.mock.DSN))
err = db.CreateTable(context.Background(), &table)
c.Assert(err, IsNil, Commentf("Error create table: %s %s", err, s.mock.DSN))
err = db.CreateTable(context.Background(), &table)
c.Assert(err, IsNil, Commentf("Error create existed 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)
Expand Down