Skip to content

Commit 99188aa

Browse files
authored
Merge pull request #123 from abhijitrakas/feature/mysql-flags
Support all `mysql` flags while importing database
2 parents d35634a + bbb476a commit 99188aa

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed

features/db-import.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ Feature: Import a WordPress database
4444
"""
4545
And STDOUT should be empty
4646

47+
Scenario: Import database with passed-in options
48+
Given a WP install
49+
50+
Given a debug.sql file:
51+
"""
52+
INSERT INTO `wp_options` (`option_id`, `option_name`, `option_value`, `autoload`) VALUES (999, 'testoption', 'testval', 'yes'),(999, 'testoption', 'testval', 'yes');
53+
"""
54+
55+
When I try `wp db import debug.sql --force`
56+
Then STDOUT should be:
57+
"""
58+
Success: Imported from 'debug.sql'.
59+
"""
60+
4761
Scenario: Help runs properly at various points of a functional WP install
4862
Given an empty directory
4963

src/DB_Command.php

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ public function export( $args, $assoc_args ) {
539539
* [--dbpass=<value>]
540540
* : Password to pass to mysql. Defaults to DB_PASSWORD.
541541
*
542+
* [--<field>=<value>]
543+
* : Extra arguments to pass to mysql. [Refer to mysql binary docs](https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html).
544+
*
542545
* [--skip-optimization]
543546
* : When using an SQL file, do not include speed optimization such as disabling auto-commit and key checks.
544547
*
@@ -571,6 +574,8 @@ public function import( $args, $assoc_args ) {
571574

572575
$mysql_args['execute'] = sprintf( $query, $result_file );
573576
}
577+
// Check if any mysql option pass.
578+
$mysql_args = array_merge( $mysql_args, self::get_mysql_args( $assoc_args ) );
574579

575580
self::run( '/usr/bin/env mysql --no-defaults --no-auto-rehash', $mysql_args );
576581

@@ -1025,10 +1030,10 @@ public function prefix() {
10251030
* | 98 | foo_options | a:1:{s:12:"_multiwidget";i:1;} | yes |
10261031
* | 99 | foo_settings | a:0:{} | yes |
10271032
* +----+--------------+--------------------------------+-----+
1028-
*
1033+
*
10291034
* # SQL search and delete records from database table 'wp_options' where 'option_name' match 'foo'
10301035
* wp db query "DELETE from wp_options where option_id in ($(wp db query "SELECT GROUP_CONCAT(option_id SEPARATOR ',') from wp_options where option_name like '%foo%';" --silent --skip-column-names))"
1031-
*
1036+
*
10321037
* @when after_wp_load
10331038
*/
10341039
public function search( $args, $assoc_args ) {
@@ -1429,4 +1434,113 @@ private function get_colors( $assoc_args, $colors ) {
14291434

14301435
return $colors;
14311436
}
1437+
1438+
/**
1439+
* Helper to pluck `mysql` options from associative args array.
1440+
*
1441+
* @param array $assoc_args Associative args array.
1442+
* @return array Array with `mysql` options set if in passed-in associative args array.
1443+
*/
1444+
private static function get_mysql_args( $assoc_args ) {
1445+
1446+
$allowed_mysql_options = [
1447+
'auto-rehash',
1448+
'auto-vertical-output',
1449+
'batch',
1450+
'binary-as-hex',
1451+
'binary-mode',
1452+
'bind-address',
1453+
'character-sets-dir',
1454+
'column-names',
1455+
'column-type-info',
1456+
'comments',
1457+
'compress',
1458+
'connect-expired-password',
1459+
'connect_timeout',
1460+
'database',
1461+
'debug',
1462+
'debug-check',
1463+
'debug-info',
1464+
'default-auth',
1465+
'default-character-set',
1466+
'defaults-extra-file',
1467+
'defaults-file',
1468+
'defaults-group-suffix',
1469+
'delimiter',
1470+
'enable-cleartext-plugin',
1471+
'execute',
1472+
'force',
1473+
'get-server-public-key',
1474+
'help',
1475+
'histignore',
1476+
'host',
1477+
'html',
1478+
'ignore-spaces',
1479+
'init-command',
1480+
'line-numbers',
1481+
'local-infile',
1482+
'login-path',
1483+
'max_allowed_packet',
1484+
'max_join_size',
1485+
'named-commands',
1486+
'net_buffer_length',
1487+
'no-beep',
1488+
'one-database',
1489+
'pager',
1490+
'pipe',
1491+
'plugin-dir',
1492+
'port',
1493+
'print-defaults',
1494+
'protocol',
1495+
'quick',
1496+
'raw',
1497+
'reconnect',
1498+
'i-am-a-dummy',
1499+
'safe-updates',
1500+
'secure-auth',
1501+
'select_limit',
1502+
'server-public-key-path',
1503+
'shared-memory-base-name',
1504+
'show-warnings',
1505+
'sigint-ignore',
1506+
'silent',
1507+
'skip-auto-rehash',
1508+
'skip-column-names',
1509+
'skip-line-numbers',
1510+
'skip-named-commands',
1511+
'skip-pager',
1512+
'skip-reconnect',
1513+
'socket',
1514+
'ssl-ca',
1515+
'ssl-capath',
1516+
'ssl-cert',
1517+
'ssl-cipher',
1518+
'ssl-crl',
1519+
'ssl-crlpath',
1520+
'ssl-fips-mode',
1521+
'ssl-key',
1522+
'ssl-mode',
1523+
'syslog',
1524+
'table',
1525+
'tee',
1526+
'tls-version',
1527+
'unbuffered',
1528+
'verbose',
1529+
'version',
1530+
'vertical',
1531+
'wait',
1532+
'xml'
1533+
];
1534+
1535+
$mysql_args = array();
1536+
1537+
foreach ( $assoc_args as $mysql_option_key => $mysql_option_value ) {
1538+
// Check flags to make sure they only contain valid options.
1539+
if ( in_array( $mysql_option_key, $allowed_mysql_options, true ) && ! empty( $mysql_option_value ) ) {
1540+
$mysql_args[$mysql_option_key] = $mysql_option_value;
1541+
}
1542+
}
1543+
1544+
return $mysql_args;
1545+
}
14321546
}

0 commit comments

Comments
 (0)