Skip to content

Commit

Permalink
Add globbing support to "where" dump configuration. (#21)
Browse files Browse the repository at this point in the history
* Added glob support for 'where' config
* Updated tests for where globbing
* Added example to README.md for globbed where config.
  • Loading branch information
nicksantamaria authored Aug 7, 2023
1 parent b652842 commit 5b091b6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
3 changes: 3 additions & 0 deletions dump/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
15 changes: 12 additions & 3 deletions dump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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)
}
6 changes: 4 additions & 2 deletions dump/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\"",
},
},
Expand All @@ -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",
},
},
},
Expand All @@ -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",
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions dump/pkg/config/test-data/mixed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ rewrite:
where:
corge: |-
grault IN (SELECT garply FROM waldo)
fred_*: |-
plugh >= 1000
4 changes: 3 additions & 1 deletion dump/pkg/config/test-data/where.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
where:
some_table: |-
revision_id IN (SELECT revision_id FROM another_table)
revision_id IN (SELECT revision_id FROM another_table)
fred_*: |-
plugh >= 1000

0 comments on commit 5b091b6

Please sign in to comment.