Skip to content

Handle MySQL commands without deprecation warnings #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 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,25 @@ 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 -f $(command -v %s))%s',
$command,
$default_flags
);
}
}
Loading