|
6 | 6 | "log" |
7 | 7 | "os" |
8 | 8 |
|
| 9 | + "github.com/gobwas/glob" |
9 | 10 | "github.com/spf13/cobra" |
10 | 11 |
|
11 | 12 | "github.com/skpr/mtk/internal/mysql" |
@@ -89,6 +90,14 @@ func (o *Options) Run(w io.Writer, logger *log.Logger, conn *mysql.Connection, d |
89 | 90 |
|
90 | 91 | client := mysql.NewClient(db, logger) |
91 | 92 |
|
| 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 { |
92 | 101 | // Get a list of tables to nodata, passed through a globber. |
93 | 102 | nodata, err := client.ListTablesByGlob(cfg.NoData) |
94 | 103 | if err != nil { |
@@ -137,9 +146,71 @@ func (o *Options) Run(w io.Writer, logger *log.Logger, conn *mysql.Connection, d |
137 | 146 |
|
138 | 147 | params.WhereMap = where |
139 | 148 |
|
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, |
142 | 159 | } |
143 | 160 |
|
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) |
145 | 216 | } |
0 commit comments