Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lightning: avoid extracting db schema when schema file is not provided #35783

Merged
merged 3 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
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