Skip to content

Commit

Permalink
Handle MySQL commands without deprecation warnings
Browse files Browse the repository at this point in the history
First look for a symlink and use it if it exists, otherwise use a given command.

With this fix:

```
% ./vendor/bin/wp db check --path=/tmp/wordpress
mariadb-check: ...
```

Issue: #271

Signed-off-by: Donatas Abraitis <[email protected]>
Signed-off-by: Donatas Abraitis <[email protected]>
  • Loading branch information
ton31337 committed Nov 8, 2024
1 parent d4c0dd2 commit b35ba84
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'] ) ) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"' );

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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' );

Expand Down Expand Up @@ -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 )
);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2152,4 +2152,27 @@ 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 $(test -L $(command -v %s) && /usr/bin/readlink -f $(command -v %s) || command -v %s)%s',
$command,
$command,
$command,
$default_flags
);
}
}

0 comments on commit b35ba84

Please sign in to comment.