Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support all mysql flags while importing database #123

Merged
merged 4 commits into from Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions features/db-import.feature
Expand Up @@ -44,6 +44,20 @@ Feature: Import a WordPress database
"""
And STDOUT should be empty

Scenario: Import database with passed-in options
Given a WP install

Given a debug.sql file:
"""
INSERT INTO `wp_options` (`option_id`, `option_name`, `option_value`, `autoload`) VALUES (999, 'testoption', 'testval', 'yes'),(999, 'testoption', 'testval', 'yes');
"""

When I try `wp db import debug.sql --force`
Then STDOUT should be:
"""
Success: Imported from 'debug.sql'.
"""

Scenario: Help runs properly at various points of a functional WP install
Given an empty directory

Expand Down
32 changes: 30 additions & 2 deletions src/DB_Command.php
Expand Up @@ -539,6 +539,9 @@ public function export( $args, $assoc_args ) {
* [--dbpass=<value>]
* : Password to pass to mysql. Defaults to DB_PASSWORD.
*
* [--<field>=<value>]
* : Extra arguments to pass to mysql. [Refer to mysql binary docs](https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html).
*
* [--skip-optimization]
* : When using an SQL file, do not include speed optimization such as disabling auto-commit and key checks.
*
Expand Down Expand Up @@ -571,6 +574,8 @@ public function import( $args, $assoc_args ) {

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

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

Expand Down Expand Up @@ -1025,10 +1030,10 @@ public function prefix() {
* | 98 | foo_options | a:1:{s:12:"_multiwidget";i:1;} | yes |
* | 99 | foo_settings | a:0:{} | yes |
* +----+--------------+--------------------------------+-----+
*
*
* # SQL search and delete records from database table 'wp_options' where 'option_name' match 'foo'
* 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))"
*
*
* @when after_wp_load
*/
public function search( $args, $assoc_args ) {
Expand Down Expand Up @@ -1429,4 +1434,27 @@ private function get_colors( $assoc_args, $colors ) {

return $colors;
}

/**
* Helper to pluck `mysql` opitons from associative args array.
schlessera marked this conversation as resolved.
Show resolved Hide resolved
*
* @param array $assoc_args Associative args array.
* @return array Array with `mysql` opitons set if in passed-in associative args array.
schlessera marked this conversation as resolved.
Show resolved Hide resolved
*/
private static function get_mysql_args( $assoc_args ) {

$allowed_mysql_option = [ 'auto-rehash', 'auto-vertical-output', 'batch', 'binary-as-hex', 'binary-mode', 'bind-address', 'character-sets-dir', 'column-names', 'column-type-info', 'comments', 'compress', 'connect-expired-password', 'connect_timeout', 'database', 'debug', 'debug-check', 'debug-info', 'default-auth', 'default-character-set', 'defaults-extra-file', 'defaults-file', 'defaults-group-suffix', 'delimiter', 'enable-cleartext-plugin', 'execute', 'force', 'get-server-public-key', 'help', 'histignore', 'host', 'html', 'ignore-spaces', 'init-command', 'line-numbers', 'local-infile', 'login-path', 'max_allowed_packet', 'max_join_size', 'named-commands', 'net_buffer_length', 'no-beep', 'one-database', 'pager', 'pipe', 'plugin-dir', 'port', 'print-defaults', 'protocol', 'quick', 'raw', 'reconnect', 'i-am-a-dummy', 'safe-updates', 'secure-auth', 'select_limit', 'server-public-key-path', 'shared-memory-base-name', 'show-warnings', 'sigint-ignore', 'silent', 'skip-auto-rehash', 'skip-column-names', 'skip-line-numbers', 'skip-named-commands', 'skip-pager', 'skip-reconnect', 'socket', 'ssl-ca', 'ssl-capath', 'ssl-cert', 'ssl-cipher', 'ssl-crl', 'ssl-crlpath', 'ssl-fips-mode', 'ssl-key', 'ssl-mode', 'syslog', 'table', 'tee', 'tls-version', 'unbuffered', 'verbose', 'version', 'vertical', 'wait', 'xml' ];
schlessera marked this conversation as resolved.
Show resolved Hide resolved

$mysql_args = array();

foreach ( $assoc_args as $mysql_option_key => $mysql_option_value ) {
// check flags is valid flag or not.
schlessera marked this conversation as resolved.
Show resolved Hide resolved
if ( in_array( $mysql_option_key, $allowed_mysql_option, true ) && ! empty( $mysql_option_value ) ) {
schlessera marked this conversation as resolved.
Show resolved Hide resolved
$mysql_args[$mysql_option_key] = $mysql_option_value;

schlessera marked this conversation as resolved.
Show resolved Hide resolved
}
}

return $mysql_args;
}
}