Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

prepare: filter databases before show table status (#324) #338

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions v4/export/block_allow_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ import (
tcontext "github.com/pingcap/dumpling/v4/context"
)

func filterDatabases(tctx *tcontext.Context, conf *Config, databases []string) []string {
tctx.L().Debug("start to filter databases")
newDatabases := make([]string, 0, len(databases))
ignoreDatabases := make([]string, 0, len(databases))
for _, database := range databases {
if conf.TableFilter.MatchSchema(database) {
newDatabases = append(newDatabases, database)
} else {
ignoreDatabases = append(ignoreDatabases, database)
}
}
if len(ignoreDatabases) > 0 {
tctx.L().Debug("ignore database", zap.Strings("databases", ignoreDatabases))
}
return newDatabases
}

func filterTables(tctx *tcontext.Context, conf *Config) {
filterTablesFunc(tctx, conf, conf.TableFilter.MatchTable)
}
Expand Down
5 changes: 5 additions & 0 deletions v4/export/block_allow_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var _ = Suite(&testBWListSuite{})
type testBWListSuite struct{}

func (s *testBWListSuite) TestFilterTables(c *C) {
tctx := tcontext.Background().WithLogger(appLogger)
dbTables := DatabaseTables{}
expectedDBTables := DatabaseTables{}

Expand All @@ -36,7 +37,11 @@ func (s *testBWListSuite) TestFilterTables(c *C) {
TableFilter: tableFilter,
}

databases := []string{filter.InformationSchemaName, filter.PerformanceSchemaName, "xxx", "yyy"}
c.Assert(filterDatabases(tctx, conf, databases), DeepEquals, databases)

conf.TableFilter = tf.NewSchemasFilter("xxx")
c.Assert(filterDatabases(tctx, conf, databases), DeepEquals, []string{"xxx"})
filterTables(tcontext.Background(), conf)
c.Assert(conf.Tables, HasLen, 1)
c.Assert(conf.Tables, DeepEquals, expectedDBTables)
Expand Down
2 changes: 1 addition & 1 deletion v4/export/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ func extractTiDBRowIDFromDecodedKey(indexField, key string) (string, error) {
}

func prepareTableListToDump(tctx *tcontext.Context, conf *Config, db *sql.Conn) error {
databases, err := prepareDumpingDatabases(conf, db)
databases, err := prepareDumpingDatabases(tctx, conf, db)
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions v4/export/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"text/template"

"github.com/pingcap/errors"

tcontext "github.com/pingcap/dumpling/v4/context"
)

const (
Expand Down Expand Up @@ -75,10 +77,14 @@ func ParseOutputFileTemplate(text string) (*template.Template, error) {
return template.Must(DefaultOutputFileTemplate.Clone()).Parse(text)
}

func prepareDumpingDatabases(conf *Config, db *sql.Conn) ([]string, error) {
func prepareDumpingDatabases(tctx *tcontext.Context, conf *Config, db *sql.Conn) ([]string, error) {
databases, err := ShowDatabases(db)
if err != nil {
return nil, err
}
databases = filterDatabases(tctx, conf, databases)
if len(conf.Databases) == 0 {
return databases, err
return databases, nil
}
dbMap := make(map[string]interface{}, len(databases))
for _, database := range databases {
Expand Down
11 changes: 6 additions & 5 deletions v4/export/prepare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func (s *testPrepareSuite) TestPrepareDumpingDatabases(c *C) {
db, mock, err := sqlmock.New()
c.Assert(err, IsNil)
defer db.Close()
conn, err := db.Conn(context.Background())
tctx := tcontext.Background().WithLogger(appLogger)
conn, err := db.Conn(tctx)
c.Assert(err, IsNil)

rows := sqlmock.NewRows([]string{"Database"}).
Expand All @@ -31,7 +32,7 @@ func (s *testPrepareSuite) TestPrepareDumpingDatabases(c *C) {
mock.ExpectQuery("SHOW DATABASES").WillReturnRows(rows)
conf := defaultConfigForTest(c)
conf.Databases = []string{"db1", "db2", "db3"}
result, err := prepareDumpingDatabases(conf, conn)
result, err := prepareDumpingDatabases(tctx, conf, conn)
c.Assert(err, IsNil)
c.Assert(result, DeepEquals, []string{"db1", "db2", "db3"})

Expand All @@ -40,12 +41,12 @@ func (s *testPrepareSuite) TestPrepareDumpingDatabases(c *C) {
AddRow("db1").
AddRow("db2")
mock.ExpectQuery("SHOW DATABASES").WillReturnRows(rows)
result, err = prepareDumpingDatabases(conf, conn)
result, err = prepareDumpingDatabases(tctx, conf, conn)
c.Assert(err, IsNil)
c.Assert(result, DeepEquals, []string{"db1", "db2"})

mock.ExpectQuery("SHOW DATABASES").WillReturnError(fmt.Errorf("err"))
_, err = prepareDumpingDatabases(conf, conn)
_, err = prepareDumpingDatabases(tctx, conf, conn)
c.Assert(err, NotNil)

rows = sqlmock.NewRows([]string{"Database"}).
Expand All @@ -55,7 +56,7 @@ func (s *testPrepareSuite) TestPrepareDumpingDatabases(c *C) {
AddRow("db5")
mock.ExpectQuery("SHOW DATABASES").WillReturnRows(rows)
conf.Databases = []string{"db1", "db2", "db4", "db6"}
_, err = prepareDumpingDatabases(conf, conn)
_, err = prepareDumpingDatabases(tctx, conf, conn)
c.Assert(err, ErrorMatches, `Unknown databases \[db4,db6\]`)
c.Assert(mock.ExpectationsWereMet(), IsNil)
}
Expand Down