diff --git a/lightning/restore/tidb.go b/lightning/restore/tidb.go index d2c0fbb4c..d60b88c8f 100644 --- a/lightning/restore/tidb.go +++ b/lightning/restore/tidb.go @@ -28,6 +28,7 @@ import ( "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" @@ -154,6 +155,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)) @@ -171,7 +173,7 @@ func InitSchema(ctx context.Context, g glue.Glue, database string, tablesSchema logger.With(zap.String("table", common.UniqueTable(database, tbl))), ) if err != nil { - break + break loopCreate } } } diff --git a/lightning/restore/tidb_test.go b/lightning/restore/tidb_test.go index 4b32834bc..664e47067 100644 --- a/lightning/restore/tidb_test.go +++ b/lightning/restore/tidb_test.go @@ -25,10 +25,11 @@ import ( "github.com/pingcap/parser/ast" "github.com/pingcap/parser/model" tmysql "github.com/pingcap/parser/mysql" - "github.com/pingcap/tidb-lightning/lightning/glue" "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/util/mock" + "github.com/pingcap/tidb-lightning/lightning/glue" + "github.com/pingcap/tidb-lightning/lightning/checkpoints" "github.com/pingcap/tidb-lightning/lightning/mydump" ) @@ -224,6 +225,30 @@ 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. + ExpectExec("CREATE TABLE IF NOT EXISTS.*"). + WillReturnError(&mysql.MySQLError{ + Number: tmysql.ErrTooBigFieldlength, + Message: "Column length too big", + }) + + s.mockDB. + ExpectClose() + + 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));", + }) + c.Assert(err, ErrorMatches, ".*Column length too big.*") +} + func (s *tidbSuite) TestInitSchemaUnsupportedSchemaError(c *C) { ctx := context.Background()