From 5b091b606b5d9f6a6083f3fc9f69b970f90a60f9 Mon Sep 17 00:00:00 2001 From: Nick Santamaria Date: Mon, 7 Aug 2023 13:14:41 +1000 Subject: [PATCH] Add globbing support to "where" dump configuration. (#21) * Added glob support for 'where' config * Updated tests for where globbing * Added example to README.md for globbed where config. --- dump/README.md | 3 +++ dump/main.go | 15 ++++++++++++--- dump/pkg/config/config_test.go | 6 ++++-- dump/pkg/config/test-data/mixed.yml | 2 ++ dump/pkg/config/test-data/where.yml | 4 +++- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/dump/README.md b/dump/README.md index e9c3340..d128ae0 100644 --- a/dump/README.md +++ b/dump/README.md @@ -24,6 +24,9 @@ where: # Only include body field data for current revisions. node_revision__body: |- revision_id IN (SELECT vid FROM node) + # Use globbing on where config. + media_revision__*: |- + revision_id IN (SELECT vid FROM media) nodata: - cache* diff --git a/dump/main.go b/dump/main.go index a94072e..8b4bebd 100644 --- a/dump/main.go +++ b/dump/main.go @@ -54,7 +54,6 @@ func main() { } } -// func dump(stdout, stderr io.Writer, db *sql.DB, cfg config.Rules) error { d := dumper.NewClient(db, log.New(stderr, "", 0)) @@ -86,8 +85,18 @@ func dump(stdout, stderr io.Writer, db *sql.DB, cfg config.Rules) error { // Assign our sanitization rules to the dumper. d.SelectMap = cfg.SanitizeMap() - // Assign conditional row rules to the dumper. - d.WhereMap = cfg.WhereMap() + // Assign conditional row rules to the dumper, passed through a globber. + where := make(map[string]string, 0) + for glob, condition := range cfg.WhereMap() { + tables, err := dbutils.ListTables(db, []string{glob}) + if err != nil { + return err + } + for _, table := range tables { + where[table] = condition + } + } + d.WhereMap = where return d.DumpTables(stdout) } diff --git a/dump/pkg/config/config_test.go b/dump/pkg/config/config_test.go index 6857cb2..0fd79ff 100644 --- a/dump/pkg/config/config_test.go +++ b/dump/pkg/config/config_test.go @@ -39,7 +39,7 @@ func TestLoad(t *testing.T) { Rules{ Rewrite: map[string]Rewrite{ "accounts": map[string]string{ - "email": "concat(id, \"@sanitized\")", + "email": "concat(id, \"@sanitized\")", "password": "\"SANITIZED_PASSWORD\"", }, }, @@ -51,6 +51,7 @@ func TestLoad(t *testing.T) { Rules{ Where: map[string]string{ "some_table": "revision_id IN (SELECT revision_id FROM another_table)", + "fred_*": "plugh >= 1000", }, }, }, @@ -66,7 +67,8 @@ func TestLoad(t *testing.T) { }, }, Where: map[string]string{ - "corge": "grault IN (SELECT garply FROM waldo)", + "corge": "grault IN (SELECT garply FROM waldo)", + "fred_*": "plugh >= 1000", }, }, }, diff --git a/dump/pkg/config/test-data/mixed.yml b/dump/pkg/config/test-data/mixed.yml index 518a0f6..c420df5 100644 --- a/dump/pkg/config/test-data/mixed.yml +++ b/dump/pkg/config/test-data/mixed.yml @@ -8,3 +8,5 @@ rewrite: where: corge: |- grault IN (SELECT garply FROM waldo) + fred_*: |- + plugh >= 1000 \ No newline at end of file diff --git a/dump/pkg/config/test-data/where.yml b/dump/pkg/config/test-data/where.yml index 1c4db71..5b3559a 100644 --- a/dump/pkg/config/test-data/where.yml +++ b/dump/pkg/config/test-data/where.yml @@ -1,3 +1,5 @@ where: some_table: |- - revision_id IN (SELECT revision_id FROM another_table) \ No newline at end of file + revision_id IN (SELECT revision_id FROM another_table) + fred_*: |- + plugh >= 1000