Skip to content

Commit 73d8833

Browse files
committed
Improve MariaDB detection
1 parent c42c5db commit 73d8833

File tree

3 files changed

+52
-32
lines changed

3 files changed

+52
-32
lines changed

bin/install-package-tests

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,42 @@ if [ -n "${WP_CLI_TEST_DBPASS}" ]; then
6060
TEST_PASSWORD="${WP_CLI_TEST_DBPASS}"
6161
fi
6262

63-
echo 'Checking if MySQL is ready...'
63+
TYPE="MySQL"
64+
CLIENT_VERSION=$(/usr/bin/env mysql --version 2>/dev/null)
65+
case "${CLIENT_VERSION}" in
66+
*"MariaDB"*)
67+
TYPE="MariaDB"
68+
;;
69+
esac
70+
71+
SERVER_VERSION=$(mysql -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
72+
VERSION=$(echo "${SERVER_VERSION}" | grep -o '^[^-]*')
73+
MAJOR=$(echo "${VERSION}" | cut -d. -f1)
74+
MINOR=$(echo "${VERSION}" | cut -d. -f2)
75+
76+
echo "Detected ${TYPE} at version ${MAJOR}.${MINOR}"
77+
78+
mysql() {
79+
if [ "${TYPE}" == "MySQL" ]; then
80+
/usr/bin/env mysql "$@"
81+
else
82+
/usr/bin/env mariadb "$@"
83+
fi
84+
}
85+
86+
echo 'Checking if database is ready...'
6487
while ! mysql ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
6588
do
66-
echo 'Waiting for MySQL...'
89+
echo 'Waiting for database...'
6790
sleep 5
6891
i=$((i+1))
6992
if [ $i -gt 36 ]; then
70-
echo 'MySQL failed to start. Aborting.'
93+
echo 'Database failed to start. Aborting.'
7194
exit 1
7295
fi
7396
done
7497

75-
# Prepare the database for running the tests with a MySQL version 8.0 or higher.
98+
# Prepare the database for running the tests with MariaDB, or MySQL 8.0+
7699
install_mysql_db_8_0_plus() {
77100
set -ex
78101
mysql -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
@@ -89,21 +112,7 @@ install_mysql_db_lower_than_8_0() {
89112
mysql -e "GRANT ALL ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
90113
}
91114

92-
VERSION_STRING=$(mysql -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
93-
VERSION=$(echo "${VERSION_STRING}" | grep -o '^[^-]*')
94-
MAJOR=$(echo "${VERSION}" | cut -d. -f1)
95-
MINOR=$(echo "${VERSION}" | cut -d. -f2)
96-
TYPE="MySQL"
97-
case "${VERSION_STRING}" in
98-
*"MariaDB"*)
99-
TYPE="MariaDB"
100-
;;
101-
esac
102-
103-
echo "Detected ${TYPE} at version ${MAJOR}.${MINOR}"
104-
105-
106-
if [ "${TYPE}" != "MariaDB" ] && [ "${MAJOR}" -ge 8 ]; then
115+
if [ "${TYPE}" == "MariaDB" ] || [ "${MAJOR}" -ge 8 ]; then
107116
install_mysql_db_8_0_plus
108117
else
109118
install_mysql_db_lower_than_8_0

src/Context/FeatureContext.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,9 @@ private function set_cache_dir() {
959959
* @param bool $add_database Optional. Whether to add dbname to the $sql_cmd. Default false.
960960
*/
961961
private static function run_sql( $sql_cmd, $assoc_args = [], $add_database = false ) {
962+
$binary = 'mariadb' === Utils\get_db_type() ? 'mariadb' : 'mysql';
963+
$sql_cmd = "$binary $sql_cmd";
964+
962965
$default_assoc_args = [
963966
'host' => self::$db_settings['dbhost'],
964967
'user' => self::$db_settings['dbuser'],
@@ -980,15 +983,15 @@ public function create_db() {
980983
}
981984

982985
$dbname = self::$db_settings['dbname'];
983-
self::run_sql( 'mysql --no-defaults', [ 'execute' => "CREATE DATABASE IF NOT EXISTS $dbname" ] );
986+
self::run_sql( '--no-defaults', [ 'execute' => "CREATE DATABASE IF NOT EXISTS $dbname" ] );
984987
}
985988

986989
public function drop_db() {
987990
if ( 'sqlite' === self::$db_type ) {
988991
return;
989992
}
990993
$dbname = self::$db_settings['dbname'];
991-
self::run_sql( 'mysql --no-defaults', [ 'execute' => "DROP DATABASE IF EXISTS $dbname" ] );
994+
self::run_sql( '--no-defaults', [ 'execute' => "DROP DATABASE IF EXISTS $dbname" ] );
992995
}
993996

994997
public function proc( $command, $assoc_args = [], $path = '' ) {
@@ -1149,7 +1152,7 @@ public function install_wp( $subdir = '' ) {
11491152
// Disable WP Cron by default to avoid bogus HTTP requests in CLI context.
11501153
$config_extra_php = "if ( ! defined( 'DISABLE_WP_CRON' ) ) { define( 'DISABLE_WP_CRON', true ); }\n";
11511154

1152-
if ( 'mysql' === self::$db_type ) {
1155+
if ( 'sqlite' !== self::$db_type ) {
11531156
$this->create_db();
11541157
}
11551158
$this->create_run_dir();
@@ -1179,7 +1182,7 @@ public function install_wp( $subdir = '' ) {
11791182
if ( 'sqlite' === self::$db_type ) {
11801183
copy( "{$install_cache_path}.sqlite", "$run_dir/wp-content/database/.ht.sqlite" );
11811184
} else {
1182-
self::run_sql( 'mysql --no-defaults', [ 'execute' => "source {$install_cache_path}.sql" ], true /*add_database*/ );
1185+
self::run_sql( '--no-defaults', [ 'execute' => "source {$install_cache_path}.sql" ], true /*add_database*/ );
11831186
}
11841187
} else {
11851188
$this->proc( 'wp core install', $install_args, $subdir )->run_check();
@@ -1189,8 +1192,9 @@ public function install_wp( $subdir = '' ) {
11891192

11901193
self::dir_diff_copy( $run_dir, self::$cache_dir, $install_cache_path );
11911194

1192-
if ( 'mysql' === self::$db_type ) {
1193-
$mysqldump_binary = Utils\force_env_on_nix_systems( 'mysqldump' );
1195+
if ( 'sqlite' !== self::$db_type ) {
1196+
$mysqldump_binary = 'mariadb' === Utils\get_db_type() ? 'mariadb-dump' : 'mysqldump';
1197+
$mysqldump_binary = Utils\force_env_on_nix_systems( $mysqldump_binary );
11941198
$support_column_statistics = exec( "{$mysqldump_binary} --help | grep 'column-statistics'" );
11951199
$command = "{$mysqldump_binary} --no-defaults --no-tablespaces";
11961200
if ( $support_column_statistics ) {

utils/behat-tags.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,22 @@ function version_tags(
7979
$skip_tags[] = '@broken-trunk';
8080
}
8181

82-
if ( 'sqlite' === getenv( 'WP_CLI_TEST_DBTYPE' ) ) {
83-
$skip_tags[] = '@require-mysql';
82+
switch ( getenv( 'WP_CLI_TEST_DBTYPE' ) ) {
83+
case 'mysql':
84+
$skip_tags[] = '@require-mariadb';
85+
$skip_tags[] = '@require-sqlite';
86+
break;
87+
case 'mariadb':
88+
$skip_tags[] = '@require-mysql';
89+
$skip_tags[] = '@require-sqlite';
90+
break;
91+
case 'sqlite':
92+
$skip_tags[] = '@require-mariadb';
93+
$skip_tags[] = '@require-mysql';
94+
$skip_tags[] = '@require-mysql-or-mariadb';
95+
break;
8496
}
8597

86-
if ( 'sqlite' !== getenv( 'WP_CLI_TEST_DBTYPE' ) ) {
87-
$skip_tags[] = '@require-sqlite';
88-
}
89-
90-
9198
# Require PHP extension, eg 'imagick'.
9299
function extension_tags( $features_folder = 'features' ) {
93100
$extension_tags = array();

0 commit comments

Comments
 (0)