1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
mod analyze;
mod convert;
mod merge;
mod sample;
mod shard;
mod split;
mod validate;
use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::{generate, Shell};
use std::io;
use std::path::PathBuf;
#[derive(Parser)]
#[command(name = "sql-splitter")]
#[command(author = "Helge Sverre <helge.sverre@gmail.com>")]
#[command(version)]
#[command(about = "Split large SQL dump files into individual table files", long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand)]
pub enum Commands {
/// Split a SQL file into individual table files
Split {
/// Input SQL file (supports .gz, .bz2, .xz, .zst compression)
file: PathBuf,
/// Output directory for split files
#[arg(short, long, default_value = "output")]
output: PathBuf,
/// SQL dialect: mysql, postgres, or sqlite (auto-detected if not specified)
#[arg(short, long)]
dialect: Option<String>,
/// Verbose output
#[arg(short, long)]
verbose: bool,
/// Preview without writing files (dry run)
#[arg(long)]
dry_run: bool,
/// Show progress during processing
#[arg(short, long)]
progress: bool,
/// Only split specific tables (comma-separated)
#[arg(short, long)]
tables: Option<String>,
/// Only include schema statements (CREATE TABLE, CREATE INDEX, ALTER TABLE, DROP TABLE)
#[arg(long, conflicts_with = "data_only")]
schema_only: bool,
/// Only include data statements (INSERT, COPY)
#[arg(long, conflicts_with = "schema_only")]
data_only: bool,
},
/// Analyze a SQL file and display statistics
Analyze {
/// Input SQL file (supports .gz, .bz2, .xz, .zst compression)
file: PathBuf,
/// SQL dialect: mysql, postgres, or sqlite (auto-detected if not specified)
#[arg(short, long)]
dialect: Option<String>,
/// Show progress during analysis
#[arg(short, long)]
progress: bool,
},
/// Merge split SQL files back into a single file
Merge {
/// Directory containing split SQL files
input_dir: PathBuf,
/// Output SQL file (default: stdout)
#[arg(short, long)]
output: Option<PathBuf>,
/// SQL dialect: mysql, postgres, or sqlite
#[arg(short, long, default_value = "mysql")]
dialect: Option<String>,
/// Only merge specific tables (comma-separated)
#[arg(short, long)]
tables: Option<String>,
/// Exclude specific tables (comma-separated)
#[arg(short, long)]
exclude: Option<String>,
/// Wrap output in a transaction
#[arg(long)]
transaction: bool,
/// Skip header comments
#[arg(long)]
no_header: bool,
/// Show progress during merging
#[arg(short, long)]
progress: bool,
/// Preview without writing files (dry run)
#[arg(long)]
dry_run: bool,
},
/// Sample a subset of rows from a SQL dump while preserving FK integrity
Sample {
/// Input SQL file (supports .gz, .bz2, .xz, .zst compression)
file: PathBuf,
/// Output SQL file (default: stdout)
#[arg(short, long)]
output: Option<PathBuf>,
/// SQL dialect: mysql, postgres, sqlite (auto-detected if not specified)
#[arg(short, long)]
dialect: Option<String>,
/// Sample percentage (1-100) - mutually exclusive with --rows
#[arg(long, conflicts_with = "rows")]
percent: Option<u32>,
/// Sample fixed number of rows per table - mutually exclusive with --percent
#[arg(long, conflicts_with = "percent")]
rows: Option<usize>,
/// Preserve foreign key relationships (filter rows that reference missing parents)
#[arg(long)]
preserve_relations: bool,
/// Only sample specific tables (comma-separated)
#[arg(short, long)]
tables: Option<String>,
/// Exclude specific tables (comma-separated)
#[arg(short, long)]
exclude: Option<String>,
/// Explicit root tables for sampling (comma-separated)
#[arg(long)]
root_tables: Option<String>,
/// How to handle global/lookup tables: none, lookups, all
#[arg(long, default_value = "lookups")]
include_global: Option<String>,
/// Random seed for reproducibility
#[arg(long)]
seed: Option<u64>,
/// YAML config file for per-table settings
#[arg(short, long)]
config: Option<PathBuf>,
/// Maximum total rows to sample (explosion guard)
#[arg(long)]
max_total_rows: Option<usize>,
/// Fail if any FK integrity issues detected
#[arg(long)]
strict_fk: bool,
/// Exclude CREATE TABLE statements from output
#[arg(long)]
no_schema: bool,
/// Show progress during sampling
#[arg(short, long)]
progress: bool,
/// Preview without writing files (dry run)
#[arg(long)]
dry_run: bool,
},
/// Extract tenant-specific data from a multi-tenant SQL dump
Shard {
/// Input SQL file (supports .gz, .bz2, .xz, .zst compression)
file: PathBuf,
/// Output SQL file (default: stdout)
#[arg(short, long)]
output: Option<PathBuf>,
/// SQL dialect: mysql, postgres, sqlite (auto-detected if not specified)
#[arg(short, long)]
dialect: Option<String>,
/// Column name for tenant identification (auto-detected if not specified)
#[arg(long)]
tenant_column: Option<String>,
/// Tenant value to extract (use this OR --tenant-values)
#[arg(long, conflicts_with = "tenant_values")]
tenant_value: Option<String>,
/// Multiple tenant values to extract (comma-separated, outputs to directory)
#[arg(long, conflicts_with = "tenant_value")]
tenant_values: Option<String>,
/// Explicit root tables that have the tenant column (comma-separated)
#[arg(long)]
root_tables: Option<String>,
/// How to handle global/lookup tables: none, lookups, all
#[arg(long, default_value = "lookups")]
include_global: Option<String>,
/// YAML config file for table classification overrides
#[arg(short, long)]
config: Option<PathBuf>,
/// Maximum rows to select (memory guard)
#[arg(long)]
max_selected_rows: Option<usize>,
/// Fail if any FK integrity issues detected
#[arg(long)]
strict_fk: bool,
/// Exclude CREATE TABLE statements from output
#[arg(long)]
no_schema: bool,
/// Show progress during sharding
#[arg(short, long)]
progress: bool,
/// Preview without writing files (dry run)
#[arg(long)]
dry_run: bool,
},
/// Convert a SQL dump between dialects (MySQL, PostgreSQL, SQLite)
Convert {
/// Input SQL file (supports .gz, .bz2, .xz, .zst compression)
file: PathBuf,
/// Output SQL file (default: stdout)
#[arg(short, long)]
output: Option<PathBuf>,
/// Source dialect: mysql, postgres, sqlite (auto-detected if not specified)
#[arg(long)]
from: Option<String>,
/// Target dialect: mysql, postgres, sqlite (required)
#[arg(long)]
to: String,
/// Strict mode: fail on any unsupported feature
#[arg(long)]
strict: bool,
/// Show progress during conversion
#[arg(short, long)]
progress: bool,
/// Preview without writing files (dry run)
#[arg(long)]
dry_run: bool,
},
/// Validate a SQL dump for structural and data integrity issues
Validate {
/// Input SQL file (supports .gz, .bz2, .xz, .zst compression)
file: PathBuf,
/// SQL dialect: mysql, postgres, sqlite (auto-detected if not specified)
#[arg(short, long)]
dialect: Option<String>,
/// Show progress during validation
#[arg(short, long)]
progress: bool,
/// Treat warnings as errors (non-zero exit on any warning)
#[arg(long)]
strict: bool,
/// Output results as JSON instead of human-readable text
#[arg(long)]
json: bool,
/// Maximum rows per table for heavy checks (PK/FK)
#[arg(long, default_value = "1000000")]
max_rows_per_table: usize,
/// Disable PK/FK data integrity checks
#[arg(long)]
no_fk_checks: bool,
},
/// Generate shell completions
Completions {
/// Shell to generate completions for
#[arg(value_enum)]
shell: Shell,
},
}
pub fn run(cli: Cli) -> anyhow::Result<()> {
match cli.command {
Commands::Split {
file,
output,
dialect,
verbose,
dry_run,
progress,
tables,
schema_only,
data_only,
} => split::run(
file,
output,
dialect,
verbose,
dry_run,
progress,
tables,
schema_only,
data_only,
),
Commands::Analyze {
file,
dialect,
progress,
} => analyze::run(file, dialect, progress),
Commands::Merge {
input_dir,
output,
dialect,
tables,
exclude,
transaction,
no_header,
progress,
dry_run,
} => merge::run(
input_dir,
output,
dialect,
tables,
exclude,
transaction,
no_header,
progress,
dry_run,
),
Commands::Sample {
file,
output,
dialect,
percent,
rows,
preserve_relations,
tables,
exclude,
root_tables,
include_global,
seed,
config,
max_total_rows,
strict_fk,
no_schema,
progress,
dry_run,
} => sample::run(
file,
output,
dialect,
percent,
rows,
preserve_relations,
tables,
exclude,
root_tables,
include_global,
seed,
config,
max_total_rows,
strict_fk,
no_schema,
progress,
dry_run,
),
Commands::Shard {
file,
output,
dialect,
tenant_column,
tenant_value,
tenant_values,
root_tables,
include_global,
config,
max_selected_rows,
strict_fk,
no_schema,
progress,
dry_run,
} => shard::run(
file,
output,
dialect,
tenant_column,
tenant_value,
tenant_values,
root_tables,
include_global,
config,
max_selected_rows,
strict_fk,
no_schema,
progress,
dry_run,
),
Commands::Convert {
file,
output,
from,
to,
strict,
progress,
dry_run,
} => convert::run(file, output, from, to, strict, progress, dry_run),
Commands::Validate {
file,
dialect,
progress,
strict,
json,
max_rows_per_table,
no_fk_checks,
} => validate::run(
file,
dialect,
progress,
strict,
json,
max_rows_per_table,
no_fk_checks,
),
Commands::Completions { shell } => {
generate(
shell,
&mut Cli::command(),
"sql-splitter",
&mut io::stdout(),
);
Ok(())
}
}
}