Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
lightning: fix wrong error message of restoring table (#1049) (#1065)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored May 27, 2021
1 parent d176d6e commit 079faaf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pkg/lightning/mydump/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/*
Expand Down
41 changes: 41 additions & 0 deletions pkg/lightning/mydump/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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/
Expand Down
10 changes: 8 additions & 2 deletions pkg/lightning/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -436,6 +436,9 @@ func (worker *restoreSchemaWorker) makeJobs(dbMetas []*mydump.MDDatabaseMeta) er
return err
}
}
if err != nil {
return err
}
}
}
err = worker.wait()
Expand All @@ -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 {
Expand All @@ -472,6 +475,9 @@ func (worker *restoreSchemaWorker) makeJobs(dbMetas []*mydump.MDDatabaseMeta) er
return err
}
}
if err != nil {
return err
}
}
}
return nil
Expand Down

0 comments on commit 079faaf

Please sign in to comment.