Skip to content

Commit

Permalink
Merge pull request #275 from mrsdizzie/fix-mariadb-warnings
Browse files Browse the repository at this point in the history
Don't hardcode MySQL command names
  • Loading branch information
swissspidy authored Dec 18, 2024
2 parents df19fde + d44bf0d commit e9c4e8a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
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';
}
}

0 comments on commit e9c4e8a

Please sign in to comment.