Skip to content

Commit 37e888b

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

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/DB_Command.php

+29-12
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', $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', $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', $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 --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 --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;
@@ -701,8 +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',
705-
$this->get_defaults_flag_string( $assoc_args )
704+
'%s --no-auto-rehash --batch --skip-column-names',
705+
$this->sanitize_mysql_command( 'mysql', $this->get_defaults_flag_string( $assoc_args ) )
706706
),
707707
[ 'execute' => $query ],
708708
false
@@ -788,7 +788,7 @@ public function import( $args, $assoc_args ) {
788788
$result_file = 'STDIN';
789789
}
790790

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

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

17221722
self::run(
17231723
sprintf(
1724-
'/usr/bin/env mysql%s --no-auto-rehash',
1725-
$this->get_defaults_flag_string( $assoc_args )
1724+
'%s --no-auto-rehash',
1725+
$this->sanitize_mysql_command( 'mysql', $this->get_defaults_flag_string( $assoc_args ) )
17261726
),
17271727
array_merge( [ 'execute' => $query ], $mysql_args )
17281728
);
@@ -2121,8 +2121,8 @@ protected function get_current_sql_modes( $assoc_args ) {
21212121

21222122
list( $stdout, $stderr, $exit_code ) = self::run(
21232123
sprintf(
2124-
'/usr/bin/env mysql%s --no-auto-rehash --batch --skip-column-names',
2125-
$this->get_defaults_flag_string( $assoc_args )
2124+
'%s --no-auto-rehash --batch --skip-column-names',
2125+
$this->sanitize_mysql_command( 'mysql', $this->get_defaults_flag_string( $assoc_args ) )
21262126
),
21272127
array_merge( $args, [ 'execute' => 'SELECT @@SESSION.sql_mode' ] ),
21282128
false
@@ -2152,4 +2152,21 @@ protected function get_current_sql_modes( $assoc_args ) {
21522152

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

0 commit comments

Comments
 (0)