Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

restore: fix error lost in create schema #530

Merged
merged 8 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
21 changes: 19 additions & 2 deletions lightning/restore/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import (
"context"
"database/sql"
"fmt"
"sort"
"strconv"
"strings"

tmysql "github.com/go-sql-driver/mysql"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/format"
Expand Down Expand Up @@ -154,7 +156,15 @@ func InitSchema(ctx context.Context, g glue.Glue, database string, tablesSchema

task := logger.Begin(zap.InfoLevel, "create tables")
var sqlCreateStmts []string
for tbl, sqlCreateTable := range tablesSchema {
tables := make([]string, 0, len(tablesSchema))
for tbl := range tablesSchema {
tables = append(tables, tbl)
}
// sort tables for failpoint test
Copy link
Contributor

Choose a reason for hiding this comment

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

I think a better solution is to use regexp to match the execute result in failpoint test

sort.Strings(tables)
loopCreate:
for i, tbl := range tables {
sqlCreateTable := tablesSchema[tbl]
task.Debug("create table", zap.String("schema", sqlCreateTable))

sqlCreateStmts, err = createTableIfNotExistsStmt(g.GetParser(), sqlCreateTable, database, tbl)
Expand All @@ -170,8 +180,15 @@ func InitSchema(ctx context.Context, g glue.Glue, database string, tablesSchema
"create table",
logger.With(zap.String("table", common.UniqueTable(database, tbl))),
)
failpoint.Inject("sqlCreateStmts", func() {
if i == 0 {
Copy link
Contributor

@glorv glorv Dec 23, 2020

Choose a reason for hiding this comment

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

if the count matters, I think you can add an int parameter to this fail-point to determine at which condition, it should return an error. So maybe we can test with the error returns at first, middle or last position.

err = errors.Errorf("create %s failed", tbl)
} else {
err = nil
kennytm marked this conversation as resolved.
Show resolved Hide resolved
}
})
if err != nil {
break
break loopCreate
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions lightning/restore/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/go-sql-driver/mysql"
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
tmysql "github.com/pingcap/parser/mysql"
Expand Down Expand Up @@ -224,6 +225,24 @@ func (s *tidbSuite) TestInitSchemaSyntaxError(c *C) {
c.Assert(err, NotNil)
}

func (s *tidbSuite) TestInitSchemaErrorLost(c *C) {
ctx := context.Background()

s.mockDB.
ExpectExec("CREATE DATABASE IF NOT EXISTS `db`").
WillReturnResult(sqlmock.NewResult(1, 1))
s.mockDB.
ExpectClose()
kennytm marked this conversation as resolved.
Show resolved Hide resolved

failpoint.Enable("github.com/pingcap/tidb-lightning/lightning/restore/sqlCreateStmts", "return")
err := InitSchema(ctx, s.tiGlue, "db", map[string]string{
"t1": "create table `t1` (a int);",
"t2": "create table t2 (a int primary key, b varchar(200));",
})
failpoint.Disable("github.com/pingcap/tidb-lightning/lightning/restore/sqlCreateStmts")
c.Assert(err, ErrorMatches, "create t1 failed")
}

func (s *tidbSuite) TestInitSchemaUnsupportedSchemaError(c *C) {
ctx := context.Background()

Expand Down