From 13bdcca57cbf1db7c97c47ce54823425f95feb89 Mon Sep 17 00:00:00 2001 From: Nick Santamaria Date: Thu, 23 Apr 2020 19:03:51 +1000 Subject: [PATCH] Add ability to export rows conditionally. (#7) * Added Where property to Rules. * Added example config for excluding non-current revisions of field_body. * Added where conditions to mtk-dump. --- dump/README.md | 5 +++++ dump/main.go | 3 +++ dump/pkg/config/config.go | 1 + dump/pkg/config/config_test.go | 13 ++++++++++++- dump/pkg/config/test-data/mixed.yml | 3 +++ dump/pkg/config/test-data/where.yml | 3 +++ dump/pkg/config/utils.go | 11 +++++++++++ 7 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 dump/pkg/config/test-data/where.yml diff --git a/dump/README.md b/dump/README.md index 942896a..350f0c5 100644 --- a/dump/README.md +++ b/dump/README.md @@ -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 diff --git a/dump/main.go b/dump/main.go index 5e93ec5..0cabafb 100644 --- a/dump/main.go +++ b/dump/main.go @@ -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) } diff --git a/dump/pkg/config/config.go b/dump/pkg/config/config.go index 0313a1b..209e929 100644 --- a/dump/pkg/config/config.go +++ b/dump/pkg/config/config.go @@ -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. diff --git a/dump/pkg/config/config_test.go b/dump/pkg/config/config_test.go index b8f5e63..6857cb2 100644 --- a/dump/pkg/config/config_test.go +++ b/dump/pkg/config/config_test.go @@ -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", @@ -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()) diff --git a/dump/pkg/config/test-data/mixed.yml b/dump/pkg/config/test-data/mixed.yml index 184786b..518a0f6 100644 --- a/dump/pkg/config/test-data/mixed.yml +++ b/dump/pkg/config/test-data/mixed.yml @@ -5,3 +5,6 @@ nodata: rewrite: baz: qux: quux +where: + corge: |- + grault IN (SELECT garply FROM waldo) diff --git a/dump/pkg/config/test-data/where.yml b/dump/pkg/config/test-data/where.yml new file mode 100644 index 0000000..1c4db71 --- /dev/null +++ b/dump/pkg/config/test-data/where.yml @@ -0,0 +1,3 @@ +where: + some_table: |- + revision_id IN (SELECT revision_id FROM another_table) \ No newline at end of file diff --git a/dump/pkg/config/utils.go b/dump/pkg/config/utils.go index cd83d95..ce0da6f 100644 --- a/dump/pkg/config/utils.go +++ b/dump/pkg/config/utils.go @@ -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 +}