Skip to content

Commit

Permalink
Add ability to export rows conditionally. (#7)
Browse files Browse the repository at this point in the history
* Added Where property to Rules.
* Added example config for excluding non-current revisions of field_body.
* Added where conditions to mtk-dump.
  • Loading branch information
nicksantamaria authored Apr 23, 2020
1 parent bebc221 commit 13bdcca
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions dump/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ rewrite:
mail: concat(uid, "@localhost")
pass: '"password"'

where:
# Only include body field data for current revisions.
node_revision__body: |-
revision_id IN (SELECT vid FROM node)

nodata:
- cache*
- captcha_sessions
Expand Down
3 changes: 3 additions & 0 deletions dump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,8 @@ 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()

return d.Dump(stdout)
}
1 change: 1 addition & 0 deletions dump/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Rules struct {
Rewrite map[string]Rewrite `yaml:"rewrite" json:"rewrite"`
NoData []string `yaml:"nodata" json:"nodata"`
Ignore []string `yaml:"ignore" json:"ignore"`
Where map[string]string `yaml:"where" json:"where"`
}

// Rewrite rules for while dumping a database.
Expand Down
13 changes: 12 additions & 1 deletion dump/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ func TestLoad(t *testing.T) {
},
},
},
{
"where",
"test-data/where.yml",
Rules{
Where: map[string]string{
"some_table": "revision_id IN (SELECT revision_id FROM another_table)",
},
},
},
{
"mixed",
"test-data/mixed.yml",
Expand All @@ -56,10 +65,12 @@ func TestLoad(t *testing.T) {
"qux": "quux",
},
},
Where: map[string]string{
"corge": "grault IN (SELECT garply FROM waldo)",
},
},
},
}

for _, testCase := range testCases {
actual, err := Load(testCase.config)
fmt.Println(actual.SanitizeMap())
Expand Down
3 changes: 3 additions & 0 deletions dump/pkg/config/test-data/mixed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ nodata:
rewrite:
baz:
qux: quux
where:
corge: |-
grault IN (SELECT garply FROM waldo)
3 changes: 3 additions & 0 deletions dump/pkg/config/test-data/where.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
where:
some_table: |-
revision_id IN (SELECT revision_id FROM another_table)
11 changes: 11 additions & 0 deletions dump/pkg/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ func (r Rules) SanitizeMap() map[string]map[string]string {

return selectMap
}

// WhereMap list for conditional row exports in the MySQL dump.
func (r Rules) WhereMap() map[string]string {
whereMap := make(map[string]string)

for table, condition := range r.Where {
whereMap[table] = condition
}

return whereMap
}

0 comments on commit 13bdcca

Please sign in to comment.