Skip to content

Commit 9a3a037

Browse files
authored
Optimise dump table (skpr#24)
Co-authored-by: Nick Schuch <nick@myschuch.com>
1 parent 875fcb3 commit 9a3a037

1 file changed

Lines changed: 74 additions & 3 deletions

File tree

cmd/mtk/dump/command.go

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"os"
88

9+
"github.com/gobwas/glob"
910
"github.com/spf13/cobra"
1011

1112
"github.com/skpr/mtk/internal/mysql"
@@ -89,6 +90,14 @@ func (o *Options) Run(w io.Writer, logger *log.Logger, conn *mysql.Connection, d
8990

9091
client := mysql.NewClient(db, logger)
9192

93+
if table != "" {
94+
return o.runDumpTable(w, client, table, cfg)
95+
}
96+
97+
return o.runDumpTables(w, client, cfg)
98+
}
99+
100+
func (o *Options) runDumpTables(w io.Writer, client *mysql.Client, cfg config.Rules) error {
92101
// Get a list of tables to nodata, passed through a globber.
93102
nodata, err := client.ListTablesByGlob(cfg.NoData)
94103
if err != nil {
@@ -137,9 +146,71 @@ func (o *Options) Run(w io.Writer, logger *log.Logger, conn *mysql.Connection, d
137146

138147
params.WhereMap = where
139148

140-
if table != "" {
141-
return client.DumpTable(w, table, params)
149+
return client.DumpTables(w, params)
150+
}
151+
152+
// Helper function to dump a single table.
153+
// This function builds a list of DumpParams to that are specific to this table to avoid any performance bottlenecks.
154+
//
155+
// eg. runDumpTables has to perform ListTablesByGlobal for each table, which is slow.
156+
func (o *Options) runDumpTable(w io.Writer, client *mysql.Client, table string, cfg config.Rules) error {
157+
params := mysql.DumpParams{
158+
ExtendedInsertRows: o.ExtendedInsertRows,
142159
}
143160

144-
return client.DumpTables(w, params)
161+
// If this table matches an ignore glob, then skip it.
162+
for _, query := range cfg.Ignore {
163+
g := glob.MustCompile(query)
164+
165+
if !g.Match(table) {
166+
continue
167+
}
168+
169+
params.FilterMap = map[string]string{
170+
table: "ignore",
171+
}
172+
173+
return nil
174+
}
175+
176+
// If this table matches a nodata glob, then add it to the nodata params.
177+
for _, query := range cfg.NoData {
178+
g := glob.MustCompile(query)
179+
180+
if !g.Match(table) {
181+
continue
182+
}
183+
184+
params.FilterMap = map[string]string{
185+
table: "nodata",
186+
}
187+
}
188+
189+
// Add sanitization rules for this table if they match the glob.
190+
for query, condition := range cfg.SanitizeMap() {
191+
g := glob.MustCompile(query)
192+
193+
if !g.Match(table) {
194+
continue
195+
}
196+
197+
params.SelectMap = map[string]map[string]string{
198+
table: condition,
199+
}
200+
}
201+
202+
// Add where conditions for this table if they match the glob.
203+
for query, condition := range cfg.WhereMap() {
204+
g := glob.MustCompile(query)
205+
206+
if !g.Match(table) {
207+
continue
208+
}
209+
210+
params.WhereMap = map[string]string{
211+
table: condition,
212+
}
213+
}
214+
215+
return client.DumpTable(w, table, params)
145216
}

0 commit comments

Comments
 (0)