Skip to content

Commit 87e3979

Browse files
committed
feat: add support for some direct mysqldump options
1 parent ae91014 commit 87e3979

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ php flarum db:dump ../backups/forum.sql
2222
# Dump with compression (based on extension)
2323
php flarum db:dump /backups/dump.sql.gz # gzip compression
2424
php flarum db:dump /backups/dump.sql.bz2 # bzip2 compression
25+
26+
# Create backup on live site without locking tables
27+
php flarum db:dump --all-tablespaces --single-transaction --quick --lock-tables=false
2528
```
2629

2730
### Options
@@ -35,6 +38,9 @@ php flarum db:dump /backups/dump.sql.bz2 # bzip2 compression
3538
- `--no-column-statistics`: Disable column statistics (for MySQL 8 compatibility)
3639
- `--binary-path=/path/to/binary`: Custom mysqldump binary location
3740

41+
Additionally, most of the standard mysqldump options are supported (like `--single-transaction`, `--quick`, `--lock-tables`, etc).
42+
Check mysqldump documentation for available options.
43+
3844
## Requirements
3945

4046
- `mysqldump` binary

src/DumbDbCommand.php

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,91 @@ class DumbDbCommand extends AbstractCommand
1818
'bz2' => Bzip2Compressor::class,
1919
];
2020

21+
/**
22+
* Inspired by WP-CLI's DB_Command
23+
* @see https://github.com/wp-cli/db-command/blob/e9c4e8ab61e99f7fa7e31e584c2b2b5d54d071db/src/DB_Command.php#L1937
24+
*/
25+
private const ALLOWED_MYSQLDUMP_OPTIONS = [
26+
'add-drop-table',
27+
'add-locks',
28+
'allow-keywords',
29+
'apply-slave-statements',
30+
'bind-address',
31+
'character-sets-dir',
32+
'comments',
33+
'compatible',
34+
'compact',
35+
'complete-insert',
36+
'create-options',
37+
'databases',
38+
'debug',
39+
'debug-check',
40+
'debug-info',
41+
'default-character-set',
42+
'delete-master-logs',
43+
'disable-keys',
44+
'dump-slave',
45+
'events',
46+
'extended-insert',
47+
'fields-terminated-by',
48+
'fields-enclosed-by',
49+
'fields-optionally-enclosed-by',
50+
'fields-escaped-by',
51+
'flush-logs',
52+
'flush-privileges',
53+
'force',
54+
'hex-blob',
55+
'host',
56+
'insert-ignore',
57+
'lines-terminated-by',
58+
'lock-all-tables',
59+
'lock-tables',
60+
'log-error',
61+
'master-data',
62+
'max-allowed-packet',
63+
'net-buffer-length',
64+
'no-autocommit',
65+
'no-create-db',
66+
'no-create-info',
67+
'no-set-names',
68+
'no-tablespaces',
69+
'opt',
70+
'order-by-primary',
71+
'port',
72+
'protocol',
73+
'quick',
74+
'quote-names',
75+
'replace',
76+
'routines',
77+
'set-charset',
78+
'single-transaction',
79+
'dump-date',
80+
'skip-comments',
81+
'skip-opt',
82+
'socket',
83+
'ssl',
84+
'ssl-ca',
85+
'ssl-capath',
86+
'ssl-cert',
87+
'ssl-cipher',
88+
'ssl-key',
89+
'ssl-verify-server-cert',
90+
'tab',
91+
'triggers',
92+
'tz-utc',
93+
'user',
94+
'where',
95+
'xml',
96+
];
97+
2198
public function __construct(protected Config $config, protected Paths $paths)
2299
{
23100
parent::__construct();
24101
}
25102

26103
protected function configure(): void
27104
{
28-
$this
105+
$command = $this
29106
->setName('db:dump')
30107
->setDescription('Dump the contents of a database')
31108
->addArgument(
@@ -81,6 +158,15 @@ protected function configure(): void
81158
InputOption::VALUE_NONE,
82159
'Do not use column statistics (for MySQL 8 compatibility with older versions)',
83160
);
161+
162+
foreach (self::ALLOWED_MYSQLDUMP_OPTIONS as $option) {
163+
$command->addOption(
164+
$option,
165+
null,
166+
InputOption::VALUE_OPTIONAL,
167+
"Pass --$option to mysqldump",
168+
);
169+
}
84170
}
85171

86172
/**
@@ -150,6 +236,17 @@ protected function fire(): int
150236
$dumper->doNotUseColumnStatistics();
151237
}
152238

239+
foreach (self::ALLOWED_MYSQLDUMP_OPTIONS as $option) {
240+
$value = $this->input->getOption($option);
241+
if ($value !== null) {
242+
if ($value === true || $value === '') {
243+
$dumper->addExtraOption("--$option");
244+
} else {
245+
$dumper->addExtraOption("--$option=".$value);
246+
}
247+
}
248+
}
249+
153250
try {
154251
$dumper->dumpToFile($path);
155252
$this->info("Database dumped successfully to: $path");

0 commit comments

Comments
 (0)