From ae9af89c4ad0866f398c9bb23fc1df210a532993 Mon Sep 17 00:00:00 2001 From: Yujie Xia Date: Tue, 28 Jun 2022 14:24:48 +0800 Subject: [PATCH] lightning: avoid extracting db schema when schema file is not provided --- br/pkg/lightning/mydump/loader.go | 26 ++++++++++++-------------- br/pkg/lightning/mydump/loader_test.go | 8 ++++++-- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/br/pkg/lightning/mydump/loader.go b/br/pkg/lightning/mydump/loader.go index 30f4f14c1464c..40091c61b2d03 100644 --- a/br/pkg/lightning/mydump/loader.go +++ b/br/pkg/lightning/mydump/loader.go @@ -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 { diff --git a/br/pkg/lightning/mydump/loader_test.go b/br/pkg/lightning/mydump/loader_test.go index 0f7079a1f95df..e256a685f20ee 100644 --- a/br/pkg/lightning/mydump/loader_test.go +++ b/br/pkg/lightning/mydump/loader_test.go @@ -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" @@ -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") } }