Skip to content

Commit 890b6e3

Browse files
authored
Merge pull request #171 from wojtekn/add/mysql-sock-support
2 parents c2a04ca + 10677fe commit 890b6e3

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"require-dev": {
2424
"wp-cli/db-command": "^1.3 || ^2",
25-
"wp-cli/wp-cli-tests": "^4"
25+
"wp-cli/wp-cli-tests": "^4.2.8"
2626
},
2727
"config": {
2828
"process-timeout": 7200,

features/config-create.feature

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,62 @@ Feature: Create a wp-config file
160160
Error: Database connection error
161161
"""
162162
163+
@require-mysql
164+
Scenario: Configure with database credentials using socket path
165+
Given an empty directory
166+
And WP files
167+
And a find-socket.php file:
168+
"""
169+
<?php
170+
// The WP_CLI_TEST_DBSOCKET variable can be set in the environment to
171+
// override the default locations and will take precedence.
172+
if ( ! empty( getenv( 'WP_CLI_TEST_DBSOCKET' ) ) ) {
173+
echo getenv( 'WP_CLI_TEST_DBSOCKET' );
174+
exit(0);
175+
}
176+
// From within Behat, the WP_CLI_TEST_DBSOCKET will be mapped to the internal
177+
// DB_SOCKET variable, as Behat pushes a new environment context.
178+
$locations = [
179+
'{DB_SOCKET}',
180+
'/var/run/mysqld/mysqld.sock',
181+
'/tmp/mysql.sock',
182+
];
183+
foreach ( $locations as $location ) {
184+
if ( ! empty( $location ) && file_exists( $location ) ) {
185+
echo $location;
186+
exit(0);
187+
}
188+
}
189+
echo 'No socket found';
190+
exit(1);
191+
"""
192+
193+
When I run `php find-socket.php`
194+
Then save STDOUT as {SOCKET}
195+
And STDOUT should not be empty
196+
197+
When I try `wget -O {RUN_DIR}/install-package-tests https://raw.githubusercontent.com/wp-cli/wp-cli-tests/main/bin/install-package-tests`
198+
Then STDERR should contain:
199+
"""
200+
install-package-tests' saved
201+
"""
202+
203+
When I run `chmod +x {RUN_DIR}/install-package-tests`
204+
Then STDERR should be empty
205+
206+
# We try to account for the warnings we get for passing the password on the command line.
207+
When I try `MYSQL_HOST=localhost WP_CLI_TEST_DBHOST='localhost:{SOCKET}' WP_CLI_TEST_DBROOTPASS='root' {RUN_DIR}/install-package-tests`
208+
Then STDOUT should contain:
209+
"""
210+
Detected MySQL
211+
"""
212+
213+
When I run `wp config create --dbname='{DB_NAME}' --dbuser='{DB_USER}' --dbpass='{DB_PASSWORD}' --dbhost='localhost:{SOCKET}'`
214+
Then the wp-config.php file should contain:
215+
"""
216+
define( 'DB_HOST', 'localhost:{SOCKET}' );
217+
"""
218+
163219
@require-php-7.0
164220
Scenario: Configure with salts generated
165221
Given an empty directory

src/Config_Command.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,22 @@ public function create( $_, $assoc_args ) {
163163
$mysql = mysqli_init();
164164
mysqli_report( MYSQLI_REPORT_STRICT );
165165
try {
166-
mysqli_real_connect( $mysql, $assoc_args['dbhost'], $assoc_args['dbuser'], $assoc_args['dbpass'] );
166+
// Accept similar format to one used by parse_db_host() e.g. 'localhost:/tmp/mysql.sock'
167+
$socket = '';
168+
$host = $assoc_args['dbhost'];
169+
$socket_pos = strpos( $host, ':/' );
170+
if ( false !== $socket_pos ) {
171+
$socket = substr( $host, $socket_pos + 1 );
172+
$host = substr( $host, 0, $socket_pos );
173+
}
174+
175+
if ( file_exists( $socket ) ) {
176+
// If dbhost is a path to a socket
177+
mysqli_real_connect( $mysql, null, $assoc_args['dbuser'], $assoc_args['dbpass'], null, null, $socket );
178+
} else {
179+
// If dbhost is a hostname or IP address
180+
mysqli_real_connect( $mysql, $host, $assoc_args['dbuser'], $assoc_args['dbpass'] );
181+
}
167182
} catch ( mysqli_sql_exception $exception ) {
168183
WP_CLI::error( 'Database connection error (' . $exception->getCode() . ') ' . $exception->getMessage() );
169184
}

0 commit comments

Comments
 (0)