From 079faaf197ac64ab3f74f09646010db39232a986 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Thu, 27 May 2021 11:49:35 +0800 Subject: [PATCH] lightning: fix wrong error message of restoring table (#1049) (#1065) --- pkg/lightning/mydump/loader.go | 6 ++--- pkg/lightning/mydump/loader_test.go | 41 +++++++++++++++++++++++++++++ pkg/lightning/restore/restore.go | 10 +++++-- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/pkg/lightning/mydump/loader.go b/pkg/lightning/mydump/loader.go index bc2360071..296427f80 100644 --- a/pkg/lightning/mydump/loader.go +++ b/pkg/lightning/mydump/loader.go @@ -53,16 +53,16 @@ type SourceFileMeta struct { FileSize int64 } -func (m *MDTableMeta) GetSchema(ctx context.Context, store storage.ExternalStorage) string { +func (m *MDTableMeta) GetSchema(ctx context.Context, store storage.ExternalStorage) (string, error) { schema, err := ExportStatement(ctx, store, m.SchemaFile, m.charSet) if err != nil { log.L().Error("failed to extract table schema", zap.String("Path", m.SchemaFile.FileMeta.Path), log.ShortError(err), ) - return "" + return "", err } - return string(schema) + return string(schema), nil } /* diff --git a/pkg/lightning/mydump/loader_test.go b/pkg/lightning/mydump/loader_test.go index 21c7e534f..9e77505a5 100644 --- a/pkg/lightning/mydump/loader_test.go +++ b/pkg/lightning/mydump/loader_test.go @@ -26,6 +26,7 @@ import ( "github.com/pingcap/br/pkg/lightning/config" md "github.com/pingcap/br/pkg/lightning/mydump" + "github.com/pingcap/br/pkg/storage" ) var _ = Suite(&testMydumpLoaderSuite{}) @@ -164,6 +165,46 @@ func (s *testMydumpLoaderSuite) TestDuplicatedTable(c *C) { c.Assert(err, ErrorMatches, `invalid table schema file, duplicated item - .*db\.tbl-schema\.sql`) } +func (s *testMydumpLoaderSuite) TestTableInfoNotFound(c *C) { + s.cfg.Mydumper.CharacterSet = "auto" + + s.touch(c, "db-schema-create.sql") + s.touch(c, "db.tbl-schema.sql") + + ctx := context.Background() + store, err := storage.NewLocalStorage(s.sourceDir) + c.Assert(err, IsNil) + + loader, err := md.NewMyDumpLoader(ctx, s.cfg) + c.Assert(err, IsNil) + for _, dbMeta := range loader.GetDatabases() { + for _, tblMeta := range dbMeta.Tables { + sql, err := tblMeta.GetSchema(ctx, store) + c.Assert(sql, Equals, "") + c.Assert(err, IsNil) + } + } +} + +func (s *testMydumpLoaderSuite) TestTableUnexpectedError(c *C) { + s.touch(c, "db-schema-create.sql") + s.touch(c, "db.tbl-schema.sql") + + ctx := context.Background() + store, err := storage.NewLocalStorage(s.sourceDir) + c.Assert(err, IsNil) + + loader, err := md.NewMyDumpLoader(ctx, s.cfg) + c.Assert(err, IsNil) + for _, dbMeta := range loader.GetDatabases() { + for _, tblMeta := range dbMeta.Tables { + sql, err := tblMeta.GetSchema(ctx, store) + c.Assert(sql, Equals, "") + c.Assert(err, ErrorMatches, "failed to decode db.tbl-schema.sql as : Unsupported encoding ") + } + } +} + func (s *testMydumpLoaderSuite) TestDataNoHostDB(c *C) { /* Path/ diff --git a/pkg/lightning/restore/restore.go b/pkg/lightning/restore/restore.go index f18dd851f..c253c695e 100644 --- a/pkg/lightning/restore/restore.go +++ b/pkg/lightning/restore/restore.go @@ -414,7 +414,7 @@ func (worker *restoreSchemaWorker) makeJobs(dbMetas []*mydump.MDDatabaseMeta) er // 2. restore tables, execute statements concurrency for _, dbMeta := range dbMetas { for _, tblMeta := range dbMeta.Tables { - sql := tblMeta.GetSchema(worker.ctx, worker.store) + sql, err := tblMeta.GetSchema(worker.ctx, worker.store) if sql != "" { stmts, err := createTableIfNotExistsStmt(worker.glue.GetParser(), sql, dbMeta.Name, tblMeta.Name) if err != nil { @@ -436,6 +436,9 @@ func (worker *restoreSchemaWorker) makeJobs(dbMetas []*mydump.MDDatabaseMeta) er return err } } + if err != nil { + return err + } } } err = worker.wait() @@ -445,7 +448,7 @@ func (worker *restoreSchemaWorker) makeJobs(dbMetas []*mydump.MDDatabaseMeta) er // 3. restore views. Since views can cross database we must restore views after all table schemas are restored. for _, dbMeta := range dbMetas { for _, viewMeta := range dbMeta.Views { - sql := viewMeta.GetSchema(worker.ctx, worker.store) + sql, err := viewMeta.GetSchema(worker.ctx, worker.store) if sql != "" { stmts, err := createTableIfNotExistsStmt(worker.glue.GetParser(), sql, dbMeta.Name, viewMeta.Name) if err != nil { @@ -472,6 +475,9 @@ func (worker *restoreSchemaWorker) makeJobs(dbMetas []*mydump.MDDatabaseMeta) er return err } } + if err != nil { + return err + } } } return nil