Skip to content

Commit e188856

Browse files
authored
Merge pull request #280 from wp-cli/fix/mysql-command
Improve MariaDB detection/compatibility
2 parents 19b2555 + dc7431d commit e188856

File tree

3 files changed

+34
-46
lines changed

3 files changed

+34
-46
lines changed

features/db-import.feature

+3-12
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,13 @@ Feature: Import a WordPress database
135135
Then the wp_cli_test.sql file should exist
136136

137137
When I try `wp db import --defaults --debug`
138-
Then STDERR should contain:
139-
"""
140-
Debug (db): Running shell command: /usr/bin/env mysql --no-auto-rehash
141-
"""
138+
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-auto-rehash#
142139

143140
When I try `wp db import --debug`
144-
Then STDERR should contain:
145-
"""
146-
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
147-
"""
141+
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults --no-auto-rehash#
148142

149143
When I try `wp db import --no-defaults --debug`
150-
Then STDERR should contain:
151-
"""
152-
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
153-
"""
144+
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults --no-auto-rehash#
154145

155146
@require-wp-4.2
156147
Scenario: Import db that has emoji in post

features/db-query.feature

+3-12
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,13 @@ Feature: Query the database with WordPress' MySQL config
7676
Given a WP install
7777

7878
When I try `wp db query --defaults --debug`
79-
Then STDERR should contain:
80-
"""
81-
Debug (db): Running shell command: /usr/bin/env mysql --no-auto-rehash
82-
"""
79+
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-auto-rehash#
8380

8481
When I try `wp db query --debug`
85-
Then STDERR should contain:
86-
"""
87-
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
88-
"""
82+
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults --no-auto-rehash#
8983

9084
When I try `wp db query --no-defaults --debug`
91-
Then STDERR should contain:
92-
"""
93-
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
94-
"""
85+
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults --no-auto-rehash#
9586

9687
Scenario: SQL modes do not include any of the modes incompatible with WordPress
9788
Given a WP install

src/DB_Command.php

+28-22
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public function check( $_, $assoc_args ) {
252252

253253
$command = sprintf(
254254
'/usr/bin/env %s%s %s',
255-
$this->get_check_command(),
255+
Utils\get_sql_check_command(),
256256
$this->get_defaults_flag_string( $assoc_args ),
257257
'%s'
258258
);
@@ -300,7 +300,7 @@ public function check( $_, $assoc_args ) {
300300
public function optimize( $_, $assoc_args ) {
301301
$command = sprintf(
302302
'/usr/bin/env %s%s %s',
303-
$this->get_check_command(),
303+
Utils\get_sql_check_command(),
304304
$this->get_defaults_flag_string( $assoc_args ),
305305
'%s'
306306
);
@@ -348,7 +348,7 @@ public function optimize( $_, $assoc_args ) {
348348
public function repair( $_, $assoc_args ) {
349349
$command = sprintf(
350350
'/usr/bin/env %s%s %s',
351-
$this->get_check_command(),
351+
Utils\get_sql_check_command(),
352352
$this->get_defaults_flag_string( $assoc_args ),
353353
'%s'
354354
);
@@ -397,7 +397,11 @@ public function repair( $_, $assoc_args ) {
397397
*/
398398
public function cli( $_, $assoc_args ) {
399399

400-
$command = sprintf( '/usr/bin/env mysql%s --no-auto-rehash', $this->get_defaults_flag_string( $assoc_args ) );
400+
$command = sprintf(
401+
'/usr/bin/env %s%s --no-auto-rehash',
402+
$this->get_mysql_command(),
403+
$this->get_defaults_flag_string( $assoc_args )
404+
);
401405
WP_CLI::debug( "Running shell command: {$command}", 'db' );
402406

403407
if ( ! isset( $assoc_args['database'] ) ) {
@@ -496,7 +500,11 @@ public function cli( $_, $assoc_args ) {
496500
*/
497501
public function query( $args, $assoc_args ) {
498502

499-
$command = sprintf( '/usr/bin/env mysql%s --no-auto-rehash', $this->get_defaults_flag_string( $assoc_args ) );
503+
$command = sprintf(
504+
'/usr/bin/env %s%s --no-auto-rehash',
505+
$this->get_mysql_command(),
506+
$this->get_defaults_flag_string( $assoc_args )
507+
);
500508
WP_CLI::debug( "Running shell command: {$command}", 'db' );
501509

502510
$assoc_args['database'] = DB_NAME;
@@ -642,7 +650,7 @@ public function export( $args, $assoc_args ) {
642650
$assoc_args['result-file'] = $result_file;
643651
}
644652

645-
$mysqldump_binary = Utils\force_env_on_nix_systems( $this->get_dump_command() );
653+
$mysqldump_binary = Utils\force_env_on_nix_systems( Utils\get_sql_dump_command() );
646654

647655
$support_column_statistics = exec( $mysqldump_binary . ' --help | grep "column-statistics"' );
648656

@@ -732,7 +740,8 @@ private function get_posts_table_charset( $assoc_args ) {
732740

733741
list( $stdout, $stderr, $exit_code ) = self::run(
734742
sprintf(
735-
'/usr/bin/env mysql%s --no-auto-rehash --batch --skip-column-names',
743+
'%s%s --no-auto-rehash --batch --skip-column-names',
744+
$this->get_mysql_command(),
736745
$this->get_defaults_flag_string( $assoc_args )
737746
),
738747
[ 'execute' => $query ],
@@ -819,7 +828,11 @@ public function import( $args, $assoc_args ) {
819828
$result_file = 'STDIN';
820829
}
821830

822-
$command = sprintf( '/usr/bin/env mysql%s --no-auto-rehash', $this->get_defaults_flag_string( $assoc_args ) );
831+
$command = sprintf(
832+
'/usr/bin/env %s%s --no-auto-rehash',
833+
$this->get_mysql_command(),
834+
$this->get_defaults_flag_string( $assoc_args )
835+
);
823836
WP_CLI::debug( "Running shell command: {$command}", 'db' );
824837
WP_CLI::debug( 'Associative arguments: ' . json_encode( $assoc_args ), 'db' );
825838

@@ -1752,7 +1765,8 @@ protected function run_query( $query, $assoc_args = [] ) {
17521765

17531766
self::run(
17541767
sprintf(
1755-
'/usr/bin/env mysql%s --no-auto-rehash',
1768+
'%s%s --no-auto-rehash',
1769+
$this->get_mysql_command(),
17561770
$this->get_defaults_flag_string( $assoc_args )
17571771
),
17581772
array_merge( [ 'execute' => $query ], $mysql_args )
@@ -2152,7 +2166,8 @@ protected function get_current_sql_modes( $assoc_args ) {
21522166

21532167
list( $stdout, $stderr, $exit_code ) = self::run(
21542168
sprintf(
2155-
'/usr/bin/env mysql%s --no-auto-rehash --batch --skip-column-names',
2169+
'%s%s --no-auto-rehash --batch --skip-column-names',
2170+
$this->get_mysql_command(),
21562171
$this->get_defaults_flag_string( $assoc_args )
21572172
),
21582173
array_merge( $args, [ 'execute' => 'SELECT @@SESSION.sql_mode' ] ),
@@ -2185,20 +2200,11 @@ protected function get_current_sql_modes( $assoc_args ) {
21852200
}
21862201

21872202
/**
2188-
* Returns the correct `check` command based on the detected database type.
2203+
* Returns the correct `mysql` command based on the detected database type.
21892204
*
21902205
* @return string The appropriate check command.
21912206
*/
2192-
private function get_check_command() {
2193-
return ( strpos( Utils\get_mysql_version(), 'MariaDB' ) !== false ) ? 'mariadb-check' : 'mysqlcheck';
2194-
}
2195-
2196-
/**
2197-
* Returns the correct `dump` command based on the detected database type.
2198-
*
2199-
* @return string The appropriate dump command.
2200-
*/
2201-
private function get_dump_command() {
2202-
return ( strpos( Utils\get_mysql_version(), 'MariaDB' ) !== false ) ? 'mariadb-dump' : 'mysqldump';
2207+
private function get_mysql_command() {
2208+
return 'mariadb' === Utils\get_db_type() ? 'mariadb' : 'mysql';
22032209
}
22042210
}

0 commit comments

Comments
 (0)