Skip to content

Commit c388578

Browse files
authored
Merge pull request #246 from wp-cli/fix/mariadb-support
Improve MariaDB detection
2 parents c42c5db + 39b4c37 commit c388578

File tree

5 files changed

+133
-49
lines changed

5 files changed

+133
-49
lines changed

bin/install-package-tests

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

63-
echo 'Checking if MySQL is ready...'
64-
while ! mysql ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
65-
do
66-
echo 'Waiting for MySQL...'
67-
sleep 5
68-
i=$((i+1))
69-
if [ $i -gt 36 ]; then
70-
echo 'MySQL failed to start. Aborting.'
71-
exit 1
72-
fi
73-
done
63+
echo "Detecting database version..."
64+
65+
TYPE="MySQL"
66+
CLIENT_VERSION=$(mysql --version 2>/dev/null)
67+
68+
case "${CLIENT_VERSION}" in
69+
*"MariaDB"*)
70+
TYPE="MariaDB"
71+
;;
72+
esac
73+
74+
if [ "${TYPE}" = "MySQL" ]; then
75+
SERVER_VERSION=$(mysql -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
76+
else
77+
SERVER_VERSION=$(mariadb -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
78+
fi
79+
80+
VERSION=$(echo "${SERVER_VERSION}" | grep -o '^[^-]*')
81+
MAJOR=$(echo "${VERSION}" | cut -d. -f1)
82+
MINOR=$(echo "${VERSION}" | cut -d. -f2)
83+
84+
echo "Detected ${TYPE} at version ${MAJOR}.${MINOR}"
85+
86+
echo 'Checking if database is ready...'
87+
88+
if [ "${TYPE}" = "MySQL" ]; then
89+
while ! mysql ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
90+
do
91+
echo 'Waiting for database...'
92+
sleep 5
93+
i=$((i+1))
94+
if [ $i -gt 36 ]; then
95+
echo 'Database failed to start. Aborting.'
96+
exit 1
97+
fi
98+
done
99+
else
100+
while ! mariadb ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
101+
do
102+
echo 'Waiting for database...'
103+
sleep 5
104+
i=$((i+1))
105+
if [ $i -gt 36 ]; then
106+
echo 'Database failed to start. Aborting.'
107+
exit 1
108+
fi
109+
done
110+
fi
74111

75112
# Prepare the database for running the tests with a MySQL version 8.0 or higher.
76113
install_mysql_db_8_0_plus() {
@@ -89,21 +126,18 @@ install_mysql_db_lower_than_8_0() {
89126
mysql -e "GRANT ALL ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
90127
}
91128

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-
129+
# Prepare the database for running the tests with MariaDB
130+
install_mariadb() {
131+
set -ex
132+
mariadb -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
133+
mariadb -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
134+
mariadb -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
135+
mariadb -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
136+
}
105137

106-
if [ "${TYPE}" != "MariaDB" ] && [ "${MAJOR}" -ge 8 ]; then
138+
if [ "${TYPE}" = "MariaDB" ]; then
139+
install_mariadb
140+
elif [ "${MAJOR}" -ge 8 ]; then
107141
install_mysql_db_8_0_plus
108142
else
109143
install_mysql_db_lower_than_8_0

features/steps.feature

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,11 @@ Feature: Make sure "Given", "When", "Then" steps work as expected
6767
When I run `echo {WP_VERSION-latest}`
6868
Then STDOUT should match /\d\.\d/
6969
And STDERR should be empty
70+
71+
@require-mysql-or-mariadb
72+
Scenario: SQL related variables
73+
When I run `echo {MYSQL_BINARY}`
74+
Then STDOUT should match /(mysql|mariadb)/
75+
76+
When I run `echo {SQL_DUMP_COMMAND}`
77+
Then STDOUT should match /(mysqldump|mariadb-dump)/

src/Context/FeatureContext.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ class FeatureContext implements SnippetAcceptingContext {
9292
*/
9393
private static $db_type = 'mysql';
9494

95+
/**
96+
* Name of mysql binary to use (mysql or mariadb). Default to mysql
97+
*/
98+
private static $mysql_binary = 'mysql';
99+
95100
/**
96101
* Array of background process ids started by the current scenario. Used to terminate them at the end of the scenario.
97102
*/
@@ -552,6 +557,7 @@ public static function prepare( BeforeSuiteScope $scope ) {
552557
self::log_run_times_before_suite( $scope );
553558
}
554559
self::$behat_run_dir = getcwd();
560+
self::$mysql_binary = Utils\get_mysql_binary_path();
555561

556562
$result = Process::create( 'wp cli info', null, self::get_process_env_variables() )->run_check();
557563
echo "{$result->stdout}\n";
@@ -603,6 +609,12 @@ public function beforeScenario( BeforeScenarioScope $scope ) {
603609
self::get_behat_internal_variables()
604610
);
605611

612+
$mysql_binary = Utils\get_mysql_binary_path();
613+
$sql_dump_command = Utils\get_sql_dump_command();
614+
615+
$this->variables['MYSQL_BINARY'] = $mysql_binary;
616+
$this->variables['SQL_DUMP_COMMAND'] = $sql_dump_command;
617+
606618
// Used in the names of the RUN_DIR and SUITE_CACHE_DIR directories.
607619
self::$temp_dir_infix = null;
608620
$file = self::get_event_file( $scope, $line );
@@ -980,15 +992,15 @@ public function create_db() {
980992
}
981993

982994
$dbname = self::$db_settings['dbname'];
983-
self::run_sql( 'mysql --no-defaults', [ 'execute' => "CREATE DATABASE IF NOT EXISTS $dbname" ] );
995+
self::run_sql( self::$mysql_binary . ' --no-defaults', [ 'execute' => "CREATE DATABASE IF NOT EXISTS $dbname" ] );
984996
}
985997

986998
public function drop_db() {
987999
if ( 'sqlite' === self::$db_type ) {
9881000
return;
9891001
}
9901002
$dbname = self::$db_settings['dbname'];
991-
self::run_sql( 'mysql --no-defaults', [ 'execute' => "DROP DATABASE IF EXISTS $dbname" ] );
1003+
self::run_sql( self::$mysql_binary . ' --no-defaults', [ 'execute' => "DROP DATABASE IF EXISTS $dbname" ] );
9921004
}
9931005

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

1152-
if ( 'mysql' === self::$db_type ) {
1164+
if ( 'sqlite' !== self::$db_type ) {
11531165
$this->create_db();
11541166
}
11551167
$this->create_run_dir();
@@ -1179,7 +1191,7 @@ public function install_wp( $subdir = '' ) {
11791191
if ( 'sqlite' === self::$db_type ) {
11801192
copy( "{$install_cache_path}.sqlite", "$run_dir/wp-content/database/.ht.sqlite" );
11811193
} else {
1182-
self::run_sql( 'mysql --no-defaults', [ 'execute' => "source {$install_cache_path}.sql" ], true /*add_database*/ );
1194+
self::run_sql( self::$mysql_binary . ' --no-defaults', [ 'execute' => "source {$install_cache_path}.sql" ], true /*add_database*/ );
11831195
}
11841196
} else {
11851197
$this->proc( 'wp core install', $install_args, $subdir )->run_check();
@@ -1189,8 +1201,9 @@ public function install_wp( $subdir = '' ) {
11891201

11901202
self::dir_diff_copy( $run_dir, self::$cache_dir, $install_cache_path );
11911203

1192-
if ( 'mysql' === self::$db_type ) {
1193-
$mysqldump_binary = Utils\force_env_on_nix_systems( 'mysqldump' );
1204+
if ( 'sqlite' !== self::$db_type ) {
1205+
$mysqldump_binary = Utils\get_sql_dump_command();
1206+
$mysqldump_binary = Utils\force_env_on_nix_systems( $mysqldump_binary );
11941207
$support_column_statistics = exec( "{$mysqldump_binary} --help | grep 'column-statistics'" );
11951208
$command = "{$mysqldump_binary} --no-defaults --no-tablespaces";
11961209
if ( $support_column_statistics ) {

tests/tests/TestBehatTags.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,21 @@ public function test_behat_tags_wp_version_github_token( $env, $expected ) {
5151
$expected .= '&&~@broken-trunk';
5252
}
5353

54-
if ( 'sqlite' !== $db_type ) {
55-
$expected .= '&&~@require-sqlite';
56-
}
57-
if ( 'sqlite' === $db_type ) {
58-
$expected .= '&&~@require-mysql';
54+
switch ( $db_type ) {
55+
case 'mariadb':
56+
$expected .= '&&~@require-mysql';
57+
$expected .= '&&~@require-sqlite';
58+
break;
59+
case 'sqlite':
60+
$expected .= '&&~@require-mariadb';
61+
$expected .= '&&~@require-mysql';
62+
$expected .= '&&~@require-mysql-or-mariadb';
63+
break;
64+
case 'mysql':
65+
default:
66+
$expected .= '&&~@require-mariadb';
67+
$expected .= '&&~@require-sqlite';
68+
break;
5969
}
6070

6171
$this->assertSame( '--tags=' . $expected, $output );
@@ -131,7 +141,7 @@ public function test_behat_tags_php_version() {
131141
file_put_contents( $this->temp_dir . '/features/php_version.feature', $contents );
132142

133143
$output = exec( "cd {$this->temp_dir}; php $behat_tags" );
134-
$this->assertSame( '--tags=' . $expected . '&&~@github-api&&~@broken&&~@require-sqlite', $output );
144+
$this->assertSame( '--tags=' . $expected . '&&~@github-api&&~@broken&&~@require-mariadb&&~@require-sqlite', $output );
135145

136146
putenv( false === $env_github_token ? 'GITHUB_TOKEN' : "GITHUB_TOKEN=$env_github_token" );
137147
}
@@ -148,12 +158,23 @@ public function test_behat_tags_extension() {
148158

149159
$expecteds = array();
150160

151-
if ( 'sqlite' !== $db_type ) {
152-
$expecteds[] = '~@require-sqlite';
153-
}
154-
if ( 'sqlite' === $db_type ) {
155-
$expecteds[] = '~@require-mysql';
161+
switch ( $db_type ) {
162+
case 'mariadb':
163+
$expecteds[] = '~@require-mysql';
164+
$expecteds[] = '~@require-sqlite';
165+
break;
166+
case 'sqlite':
167+
$expecteds[] = '~@require-mariadb';
168+
$expecteds[] = '~@require-mysql';
169+
$expecteds[] = '~@require-mysql-or-mariadb';
170+
break;
171+
case 'mysql':
172+
default:
173+
$expecteds[] = '~@require-mariadb';
174+
$expecteds[] = '~@require-sqlite';
175+
break;
156176
}
177+
157178
if ( ! extension_loaded( 'imagick' ) ) {
158179
$expecteds[] = '~@require-extension-imagick';
159180
}

utils/behat-tags.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,23 @@ 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 'mariadb':
84+
$skip_tags[] = '@require-mysql';
85+
$skip_tags[] = '@require-sqlite';
86+
break;
87+
case 'sqlite':
88+
$skip_tags[] = '@require-mariadb';
89+
$skip_tags[] = '@require-mysql';
90+
$skip_tags[] = '@require-mysql-or-mariadb';
91+
break;
92+
case 'mysql':
93+
default:
94+
$skip_tags[] = '@require-mariadb';
95+
$skip_tags[] = '@require-sqlite';
96+
break;
8497
}
8598

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

0 commit comments

Comments
 (0)