Skip to content

Commit

Permalink
lightning: avoid extracting db schema when schema file is not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepymole committed Jun 28, 2022
1 parent ab27d49 commit ae9af89
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
26 changes: 12 additions & 14 deletions br/pkg/lightning/mydump/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,19 @@ type MDDatabaseMeta struct {
}

func (m *MDDatabaseMeta) GetSchema(ctx context.Context, store storage.ExternalStorage) string {
schema, err := ExportStatement(ctx, store, m.SchemaFile, m.charSet)
if err != nil {
log.FromContext(ctx).Warn("failed to extract table schema",
zap.String("Path", m.SchemaFile.FileMeta.Path),
log.ShortError(err),
)
schema = nil
}
schemaStr := strings.TrimSpace(string(schema))
// set default if schema sql is empty
if len(schemaStr) == 0 {
schemaStr = "CREATE DATABASE IF NOT EXISTS " + common.EscapeIdentifier(m.Name)
if m.SchemaFile.FileMeta.Path != "" {
schema, err := ExportStatement(ctx, store, m.SchemaFile, m.charSet)
if err != nil {
log.FromContext(ctx).Warn("failed to extract table schema",
zap.String("Path", m.SchemaFile.FileMeta.Path),
log.ShortError(err),
)
} else if schemaStr := strings.TrimSpace(string(schema)); schemaStr != "" {
return schemaStr
}
}

return schemaStr
// set default if schema sql is empty or failed to extract.
return "CREATE DATABASE IF NOT EXISTS " + common.EscapeIdentifier(m.Name)
}

type MDTableMeta struct {
Expand Down
8 changes: 6 additions & 2 deletions br/pkg/lightning/mydump/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/pingcap/tidb/br/pkg/lightning/config"
"github.com/pingcap/tidb/br/pkg/lightning/log"
md "github.com/pingcap/tidb/br/pkg/lightning/mydump"
"github.com/pingcap/tidb/br/pkg/storage"
filter "github.com/pingcap/tidb/util/table-filter"
Expand Down Expand Up @@ -181,13 +182,16 @@ func TestTableInfoNotFound(t *testing.T) {
loader, err := md.NewMyDumpLoader(ctx, s.cfg)
require.NoError(t, err)
for _, dbMeta := range loader.GetDatabases() {
dbSQL := dbMeta.GetSchema(ctx, store)
logger, buffer := log.MakeTestLogger()
logCtx := log.NewContext(ctx, logger)
dbSQL := dbMeta.GetSchema(logCtx, store)
require.Equal(t, "CREATE DATABASE IF NOT EXISTS `db`", dbSQL)
for _, tblMeta := range dbMeta.Tables {
sql, err := tblMeta.GetSchema(ctx, store)
sql, err := tblMeta.GetSchema(logCtx, store)
require.Equal(t, "", sql)
require.NoError(t, err)
}
require.NotContains(t, buffer.Stripped(), "failed to extract table schema")
}
}

Expand Down

0 comments on commit ae9af89

Please sign in to comment.