Skip to content

Commit

Permalink
Optimise dump table (skpr#24)
Browse files Browse the repository at this point in the history
Co-authored-by: Nick Schuch <nick@myschuch.com>
  • Loading branch information
nickschuch and nickschuch authored Nov 25, 2023
1 parent 875fcb3 commit 9a3a037
Showing 1 changed file with 74 additions and 3 deletions.
77 changes: 74 additions & 3 deletions cmd/mtk/dump/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"

"github.com/gobwas/glob"
"github.com/spf13/cobra"

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

client := mysql.NewClient(db, logger)

if table != "" {
return o.runDumpTable(w, client, table, cfg)
}

return o.runDumpTables(w, client, cfg)
}

func (o *Options) runDumpTables(w io.Writer, client *mysql.Client, cfg config.Rules) error {
// Get a list of tables to nodata, passed through a globber.
nodata, err := client.ListTablesByGlob(cfg.NoData)
if err != nil {
Expand Down Expand Up @@ -137,9 +146,71 @@ func (o *Options) Run(w io.Writer, logger *log.Logger, conn *mysql.Connection, d

params.WhereMap = where

if table != "" {
return client.DumpTable(w, table, params)
return client.DumpTables(w, params)
}

// Helper function to dump a single table.
// This function builds a list of DumpParams to that are specific to this table to avoid any performance bottlenecks.
//
// eg. runDumpTables has to perform ListTablesByGlobal for each table, which is slow.
func (o *Options) runDumpTable(w io.Writer, client *mysql.Client, table string, cfg config.Rules) error {
params := mysql.DumpParams{
ExtendedInsertRows: o.ExtendedInsertRows,
}

return client.DumpTables(w, params)
// If this table matches an ignore glob, then skip it.
for _, query := range cfg.Ignore {
g := glob.MustCompile(query)

if !g.Match(table) {
continue
}

params.FilterMap = map[string]string{
table: "ignore",
}

return nil
}

// If this table matches a nodata glob, then add it to the nodata params.
for _, query := range cfg.NoData {
g := glob.MustCompile(query)

if !g.Match(table) {
continue
}

params.FilterMap = map[string]string{
table: "nodata",
}
}

// Add sanitization rules for this table if they match the glob.
for query, condition := range cfg.SanitizeMap() {
g := glob.MustCompile(query)

if !g.Match(table) {
continue
}

params.SelectMap = map[string]map[string]string{
table: condition,
}
}

// Add where conditions for this table if they match the glob.
for query, condition := range cfg.WhereMap() {
g := glob.MustCompile(query)

if !g.Match(table) {
continue
}

params.WhereMap = map[string]string{
table: condition,
}
}

return client.DumpTable(w, table, params)
}

0 comments on commit 9a3a037

Please sign in to comment.