Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Don't hardcode MySQL command names, part 2
  • Loading branch information
swissspidy committed Mar 11, 2025
commit ef928eaf744cb1f1c40acf205153bb7f94963ed9
36 changes: 30 additions & 6 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,11 @@
*/
public function cli( $_, $assoc_args ) {

$command = sprintf( '/usr/bin/env mysql%s --no-auto-rehash', $this->get_defaults_flag_string( $assoc_args ) );
$command = sprintf(
'/usr/bin/env %s%s --no-auto-rehash',
$this->get_mysql_command(),
$this->get_defaults_flag_string( $assoc_args )
);

Check warning on line 404 in src/DB_Command.php

View check run for this annotation

Codecov / codecov/patch

src/DB_Command.php#L400-L404

Added lines #L400 - L404 were not covered by tests
WP_CLI::debug( "Running shell command: {$command}", 'db' );

if ( ! isset( $assoc_args['database'] ) ) {
Expand Down Expand Up @@ -496,7 +500,11 @@
*/
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(
'/usr/bin/env %s%s --no-auto-rehash',
$this->get_mysql_command(),
$this->get_defaults_flag_string( $assoc_args )
);
WP_CLI::debug( "Running shell command: {$command}", 'db' );

$assoc_args['database'] = DB_NAME;
Expand Down Expand Up @@ -732,7 +740,8 @@

list( $stdout, $stderr, $exit_code ) = self::run(
sprintf(
'/usr/bin/env mysql%s --no-auto-rehash --batch --skip-column-names',
'/usr/bin/env %s%s --no-auto-rehash --batch --skip-column-names',
$this->get_mysql_command(),
$this->get_defaults_flag_string( $assoc_args )
),
[ 'execute' => $query ],
Expand Down Expand Up @@ -819,7 +828,11 @@
$result_file = 'STDIN';
}

$command = sprintf( '/usr/bin/env mysql%s --no-auto-rehash', $this->get_defaults_flag_string( $assoc_args ) );
$command = sprintf(
'/usr/bin/env %s%s --no-auto-rehash',
$this->get_mysql_command(),
$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 @@ -1752,7 +1765,8 @@

self::run(
sprintf(
'/usr/bin/env mysql%s --no-auto-rehash',
'/usr/bin/env %s%s --no-auto-rehash',
$this->get_mysql_command(),
$this->get_defaults_flag_string( $assoc_args )
),
array_merge( [ 'execute' => $query ], $mysql_args )
Expand Down Expand Up @@ -2152,7 +2166,8 @@

list( $stdout, $stderr, $exit_code ) = self::run(
sprintf(
'/usr/bin/env mysql%s --no-auto-rehash --batch --skip-column-names',
'/usr/bin/env %s%s --no-auto-rehash --batch --skip-column-names',
$this->get_mysql_command(),
$this->get_defaults_flag_string( $assoc_args )
),
array_merge( $args, [ 'execute' => 'SELECT @@SESSION.sql_mode' ] ),
Expand Down Expand Up @@ -2184,6 +2199,15 @@
return $modes;
}

/**
* Returns the correct `mysql` command based on the detected database type.
*
* @return string The appropriate check command.
*/
private function get_mysql_command() {
return ( strpos( Utils\get_mysql_version(), 'MariaDB' ) !== false ) ? 'mariadb' : 'mysql';
}

/**
* Returns the correct `check` command based on the detected database type.
*
Expand Down
Loading