From afd8f16446344324f318dff6aaacf5be80005731 Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Fri, 8 Nov 2024 13:07:24 +0200 Subject: [PATCH] Handle MySQL commands without deprecation warnings With this fix: ``` % ./vendor/bin/wp db check --path=/tmp/wordpress mariadb-check: ... ``` Issue: https://github.com/wp-cli/db-command/issues/271 Signed-off-by: Donatas Abraitis --- src/DB_Command.php | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index 8738a284..afcf8929 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -250,7 +250,7 @@ public function clean( $_, $assoc_args ) { */ public function check( $_, $assoc_args ) { - $command = sprintf( '/usr/bin/env mysqlcheck%s %s', $this->get_defaults_flag_string( $assoc_args ), '%s' ); + $command = sprintf( '%s %s', $this->sanitize_mysql_command( 'mysqlcheck', $this->get_defaults_flag_string( $assoc_args ) ), '%s' ); WP_CLI::debug( "Running shell command: {$command}", 'db' ); $assoc_args['check'] = true; @@ -294,7 +294,7 @@ public function check( $_, $assoc_args ) { */ public function optimize( $_, $assoc_args ) { - $command = sprintf( '/usr/bin/env mysqlcheck%s %s', $this->get_defaults_flag_string( $assoc_args ), '%s' ); + $command = sprintf( '%s %s', $this->sanitize_mysql_command( 'mysqlcheck', $this->get_defaults_flag_string( $assoc_args ) ), '%s' ); WP_CLI::debug( "Running shell command: {$command}", 'db' ); $assoc_args['optimize'] = true; @@ -338,7 +338,7 @@ public function optimize( $_, $assoc_args ) { */ public function repair( $_, $assoc_args ) { - $command = sprintf( '/usr/bin/env mysqlcheck%s %s', $this->get_defaults_flag_string( $assoc_args ), '%s' ); + $command = sprintf( '%s %s', $this->sanitize_mysql_command( 'mysqlcheck', $this->get_defaults_flag_string( $assoc_args ) ), '%s' ); WP_CLI::debug( "Running shell command: {$command}", 'db' ); $assoc_args['repair'] = true; @@ -384,7 +384,7 @@ public function repair( $_, $assoc_args ) { */ public function cli( $_, $assoc_args ) { - $command = sprintf( '/usr/bin/env mysql%s --no-auto-rehash', $this->get_defaults_flag_string( $assoc_args ) ); + $command = sprintf( '%s --no-auto-rehash', $this->sanitize_mysql_command( 'mysql', $this->get_defaults_flag_string( $assoc_args ) ) ); WP_CLI::debug( "Running shell command: {$command}", 'db' ); if ( ! isset( $assoc_args['database'] ) ) { @@ -483,7 +483,7 @@ public function cli( $_, $assoc_args ) { */ public function query( $args, $assoc_args ) { - $command = sprintf( '/usr/bin/env mysql%s --no-auto-rehash', $this->get_defaults_flag_string( $assoc_args ) ); + $command = sprintf( '%s --no-auto-rehash', $this->sanitize_mysql_command( 'mysql', $this->get_defaults_flag_string( $assoc_args ) ) ); WP_CLI::debug( "Running shell command: {$command}", 'db' ); $assoc_args['database'] = DB_NAME; @@ -611,7 +611,7 @@ public function export( $args, $assoc_args ) { $assoc_args['result-file'] = $result_file; } - $mysqldump_binary = Utils\force_env_on_nix_systems( 'mysqldump' ); + $mysqldump_binary = $this->sanitize_mysql_command( 'mysqldump' ); $support_column_statistics = exec( $mysqldump_binary . ' --help | grep "column-statistics"' ); @@ -701,8 +701,8 @@ private function get_posts_table_charset( $assoc_args ) { list( $stdout, $stderr, $exit_code ) = self::run( sprintf( - '/usr/bin/env mysql%s --no-auto-rehash --batch --skip-column-names', - $this->get_defaults_flag_string( $assoc_args ) + '%s --no-auto-rehash --batch --skip-column-names', + $this->sanitize_mysql_command( 'mysql', $this->get_defaults_flag_string( $assoc_args ) ), ), [ 'execute' => $query ], false @@ -788,7 +788,7 @@ public function import( $args, $assoc_args ) { $result_file = 'STDIN'; } - $command = sprintf( '/usr/bin/env mysql%s --no-auto-rehash', $this->get_defaults_flag_string( $assoc_args ) ); + $command = sprintf( '%s --no-auto-rehash', $this->sanitize_mysql_command( 'mysql', $this->get_defaults_flag_string( $assoc_args ) ) ); WP_CLI::debug( "Running shell command: {$command}", 'db' ); WP_CLI::debug( 'Associative arguments: ' . json_encode( $assoc_args ), 'db' ); @@ -1721,8 +1721,8 @@ protected function run_query( $query, $assoc_args = [] ) { self::run( sprintf( - '/usr/bin/env mysql%s --no-auto-rehash', - $this->get_defaults_flag_string( $assoc_args ) + '%s --no-auto-rehash', + $this->sanitize_mysql_command( 'mysql', $this->get_defaults_flag_string( $assoc_args ) ), ), array_merge( [ 'execute' => $query ], $mysql_args ) ); @@ -2121,8 +2121,8 @@ protected function get_current_sql_modes( $assoc_args ) { list( $stdout, $stderr, $exit_code ) = self::run( sprintf( - '/usr/bin/env mysql%s --no-auto-rehash --batch --skip-column-names', - $this->get_defaults_flag_string( $assoc_args ) + '%s --no-auto-rehash --batch --skip-column-names', + $this->sanitize_mysql_command( 'mysql', $this->get_defaults_flag_string( $assoc_args ) ), ), array_merge( $args, [ 'execute' => 'SELECT @@SESSION.sql_mode' ] ), false @@ -2152,4 +2152,21 @@ protected function get_current_sql_modes( $assoc_args ) { return $modes; } + + /** + * Helper to sanitize `mysql` command. + * If the system has MariaDB installed, the user get the warning message: + * /usr/bin/mysqldump: Deprecated program name. + * It will be removed in a future release, use '/usr/bin/mariadb-dump' instead + * + * This helper will sanitize the `mysql` command to use `mariadb-dump` instead + * of `mysqldump` if the system has MariaDB installed. + * + * @param string mysql command + * @param string default flags + * @return string + */ + private static function sanitize_mysql_command( $command, $default_flags ) { + return sprintf( '/usr/bin/env $(/usr/bin/readlink $(command -v %s))%s', $command, $default_flags ); + } }