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 7 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
8 changes: 7 additions & 1 deletion lightning/restore/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import (

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"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"

"github.com/pingcap/tidb-lightning/lightning/glue"

. "github.com/pingcap/tidb-lightning/lightning/checkpoints"
Expand Down Expand Up @@ -154,6 +156,7 @@ func InitSchema(ctx context.Context, g glue.Glue, database string, tablesSchema

task := logger.Begin(zap.InfoLevel, "create tables")
var sqlCreateStmts []string
loopCreate:
for tbl, sqlCreateTable := range tablesSchema {
task.Debug("create table", zap.String("schema", sqlCreateTable))

Expand All @@ -170,8 +173,11 @@ 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() {
err = errors.Errorf("create %s failed", tbl)
})
if err != nil {
break
break loopCreate
}
}
}
Expand Down
20 changes: 20 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,25 @@ 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", "1*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")
// cannot predict which table will failed since map is unorder.
c.Assert(err, ErrorMatches, "create (.*) failed")
}

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

Expand Down