Skip to content

Commit 88ee855

Browse files
committed
Handle MySQL commands without deprecation warnings
With this fix: ``` % ./vendor/bin/wp db check --path=/tmp/wordpress mariadb-check: ... ``` Issue: wp-cli#271 Signed-off-by: Donatas Abraitis <[email protected]>
1 parent d4c0dd2 commit 88ee855

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/DB_Command.php

+29-10
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public function clean( $_, $assoc_args ) {
250250
*/
251251
public function check( $_, $assoc_args ) {
252252

253-
$command = sprintf( '/usr/bin/env mysqlcheck%s %s', $this->get_defaults_flag_string( $assoc_args ), '%s' );
253+
$command = sprintf( '%s%s %s', $this->sanitize_mysql_command( 'mysqlcheck' ), $this->get_defaults_flag_string( $assoc_args ), '%s' );
254254
WP_CLI::debug( "Running shell command: {$command}", 'db' );
255255

256256
$assoc_args['check'] = true;
@@ -294,7 +294,7 @@ public function check( $_, $assoc_args ) {
294294
*/
295295
public function optimize( $_, $assoc_args ) {
296296

297-
$command = sprintf( '/usr/bin/env mysqlcheck%s %s', $this->get_defaults_flag_string( $assoc_args ), '%s' );
297+
$command = sprintf( '%s%s %s', $this->sanitize_mysql_command( 'mysqlcheck' ), $this->get_defaults_flag_string( $assoc_args ), '%s' );
298298
WP_CLI::debug( "Running shell command: {$command}", 'db' );
299299

300300
$assoc_args['optimize'] = true;
@@ -338,7 +338,7 @@ public function optimize( $_, $assoc_args ) {
338338
*/
339339
public function repair( $_, $assoc_args ) {
340340

341-
$command = sprintf( '/usr/bin/env mysqlcheck%s %s', $this->get_defaults_flag_string( $assoc_args ), '%s' );
341+
$command = sprintf( '%s%s %s', $this->sanitize_mysql_command( 'mysqlcheck' ), $this->get_defaults_flag_string( $assoc_args ), '%s' );
342342
WP_CLI::debug( "Running shell command: {$command}", 'db' );
343343

344344
$assoc_args['repair'] = true;
@@ -384,7 +384,7 @@ public function repair( $_, $assoc_args ) {
384384
*/
385385
public function cli( $_, $assoc_args ) {
386386

387-
$command = sprintf( '/usr/bin/env mysql%s --no-auto-rehash', $this->get_defaults_flag_string( $assoc_args ) );
387+
$command = sprintf( '%s%s --no-auto-rehash', $this->sanitize_mysql_command( 'mysql' ), $this->get_defaults_flag_string( $assoc_args ) );
388388
WP_CLI::debug( "Running shell command: {$command}", 'db' );
389389

390390
if ( ! isset( $assoc_args['database'] ) ) {
@@ -483,7 +483,7 @@ public function cli( $_, $assoc_args ) {
483483
*/
484484
public function query( $args, $assoc_args ) {
485485

486-
$command = sprintf( '/usr/bin/env mysql%s --no-auto-rehash', $this->get_defaults_flag_string( $assoc_args ) );
486+
$command = sprintf( '%s%s --no-auto-rehash', $this->sanitize_mysql_command( 'mysql' ), $this->get_defaults_flag_string( $assoc_args ) );
487487
WP_CLI::debug( "Running shell command: {$command}", 'db' );
488488

489489
$assoc_args['database'] = DB_NAME;
@@ -611,7 +611,7 @@ public function export( $args, $assoc_args ) {
611611
$assoc_args['result-file'] = $result_file;
612612
}
613613

614-
$mysqldump_binary = Utils\force_env_on_nix_systems( 'mysqldump' );
614+
$mysqldump_binary = $this->sanitize_mysql_command( 'mysqldump' );
615615

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

@@ -701,7 +701,8 @@ private function get_posts_table_charset( $assoc_args ) {
701701

702702
list( $stdout, $stderr, $exit_code ) = self::run(
703703
sprintf(
704-
'/usr/bin/env mysql%s --no-auto-rehash --batch --skip-column-names',
704+
'%s%s --no-auto-rehash --batch --skip-column-names',
705+
$this->sanitize_mysql_command( 'mysql' ),
705706
$this->get_defaults_flag_string( $assoc_args )
706707
),
707708
[ 'execute' => $query ],
@@ -788,7 +789,7 @@ public function import( $args, $assoc_args ) {
788789
$result_file = 'STDIN';
789790
}
790791

791-
$command = sprintf( '/usr/bin/env mysql%s --no-auto-rehash', $this->get_defaults_flag_string( $assoc_args ) );
792+
$command = sprintf( '%s%s --no-auto-rehash', $this->sanitize_mysql_command( 'mysql' ), $this->get_defaults_flag_string( $assoc_args ) );
792793
WP_CLI::debug( "Running shell command: {$command}", 'db' );
793794
WP_CLI::debug( 'Associative arguments: ' . json_encode( $assoc_args ), 'db' );
794795

@@ -1721,7 +1722,8 @@ protected function run_query( $query, $assoc_args = [] ) {
17211722

17221723
self::run(
17231724
sprintf(
1724-
'/usr/bin/env mysql%s --no-auto-rehash',
1725+
'%s%s --no-auto-rehash',
1726+
$this->sanitize_mysql_command( 'mysql' ),
17251727
$this->get_defaults_flag_string( $assoc_args )
17261728
),
17271729
array_merge( [ 'execute' => $query ], $mysql_args )
@@ -2121,7 +2123,8 @@ protected function get_current_sql_modes( $assoc_args ) {
21212123

21222124
list( $stdout, $stderr, $exit_code ) = self::run(
21232125
sprintf(
2124-
'/usr/bin/env mysql%s --no-auto-rehash --batch --skip-column-names',
2126+
'%s%s --no-auto-rehash --batch --skip-column-names',
2127+
$this->sanitize_mysql_command( 'mysql' ),
21252128
$this->get_defaults_flag_string( $assoc_args )
21262129
),
21272130
array_merge( $args, [ 'execute' => 'SELECT @@SESSION.sql_mode' ] ),
@@ -2152,4 +2155,20 @@ protected function get_current_sql_modes( $assoc_args ) {
21522155

21532156
return $modes;
21542157
}
2158+
2159+
/**
2160+
* Helper to sanitize `mysql` command.
2161+
* If the system has MariaDB installed, the user get the warning message:
2162+
* /usr/bin/mysqldump: Deprecated program name.
2163+
* It will be removed in a future release, use '/usr/bin/mariadb-dump' instead
2164+
*
2165+
* This helper will sanitize the `mysql` command to use `mariadb-dump` instead
2166+
* of `mysqldump` if the system has MariaDB installed.
2167+
*
2168+
* @param string
2169+
* @return string
2170+
*/
2171+
private static function sanitize_mysql_command( $command ) {
2172+
return sprintf( '/usr/bin/env $(/usr/bin/readlink $(command -v %s))', $command );
2173+
}
21552174
}

0 commit comments

Comments
 (0)