Skip to content
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

Don't hardcode MySQL command names #275

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 4 additions & 12 deletions features/db-check.feature
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,11 @@ Feature: Check the database
Given a WP install

When I try `wp db check --defaults --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysqlcheck %s
"""
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysqlcheck|mariadb-check) %s#

When I try `wp db check --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysqlcheck --no-defaults %s
"""
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysqlcheck|mariadb-check) --no-defaults %s#

When I try `wp db check --no-defaults --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysqlcheck --no-defaults %s
"""
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysqlcheck|mariadb-check) --no-defaults %s#

15 changes: 3 additions & 12 deletions features/db-export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,10 @@ Feature: Export a WordPress database
Given a WP install

When I try `wp db export --defaults --debug`
Then STDERR should contain:
"""
Debug (db): Running initial shell command: /usr/bin/env mysqldump
"""
Then STDERR should match #Debug \(db\): Running initial shell command: /usr/bin/env (mysqldump|mariadb-dump)#

When I try `wp db export --debug`
Then STDERR should contain:
"""
Debug (db): Running initial shell command: /usr/bin/env mysqldump --no-defaults
"""
Then STDERR should match #Debug \(db\): Running initial shell command: /usr/bin/env (mysqldump|mariadb-dump) --no-defaults#

When I try `wp db export --no-defaults --debug`
Then STDERR should contain:
"""
Debug (db): Running initial shell command: /usr/bin/env mysqldump --no-defaults
"""
Then STDERR should match #Debug \(db\): Running initial shell command: /usr/bin/env (mysqldump|mariadb-dump) --no-defaults#
43 changes: 37 additions & 6 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,12 @@ 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(
'/usr/bin/env %s%s %s',
$this->get_check_command(),
$this->get_defaults_flag_string( $assoc_args ),
'%s'
);
WP_CLI::debug( "Running shell command: {$command}", 'db' );

$assoc_args['check'] = true;
Expand Down Expand Up @@ -293,8 +298,12 @@ public function check( $_, $assoc_args ) {
* Success: Database optimized.
*/
public function optimize( $_, $assoc_args ) {

$command = sprintf( '/usr/bin/env mysqlcheck%s %s', $this->get_defaults_flag_string( $assoc_args ), '%s' );
$command = sprintf(
'/usr/bin/env %s%s %s',
$this->get_check_command(),
$this->get_defaults_flag_string( $assoc_args ),
'%s'
);
WP_CLI::debug( "Running shell command: {$command}", 'db' );

$assoc_args['optimize'] = true;
Expand Down Expand Up @@ -337,8 +346,12 @@ public function optimize( $_, $assoc_args ) {
* Success: Database repaired.
*/
public function repair( $_, $assoc_args ) {

$command = sprintf( '/usr/bin/env mysqlcheck%s %s', $this->get_defaults_flag_string( $assoc_args ), '%s' );
$command = sprintf(
'/usr/bin/env %s%s %s',
$this->get_check_command(),
$this->get_defaults_flag_string( $assoc_args ),
'%s'
);
WP_CLI::debug( "Running shell command: {$command}", 'db' );

$assoc_args['repair'] = true;
Expand Down Expand Up @@ -611,7 +624,7 @@ public function export( $args, $assoc_args ) {
$assoc_args['result-file'] = $result_file;
}

$mysqldump_binary = Utils\force_env_on_nix_systems( 'mysqldump' );
$mysqldump_binary = Utils\force_env_on_nix_systems( $this->get_dump_command() );

$support_column_statistics = exec( $mysqldump_binary . ' --help | grep "column-statistics"' );

Expand Down Expand Up @@ -2152,4 +2165,22 @@ protected function get_current_sql_modes( $assoc_args ) {

return $modes;
}

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

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