Skip to content

Commit

Permalink
Merge pull request #171 from wojtekn/add/mysql-sock-support
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera authored Dec 21, 2023
2 parents c2a04ca + 10677fe commit 890b6e3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"require-dev": {
"wp-cli/db-command": "^1.3 || ^2",
"wp-cli/wp-cli-tests": "^4"
"wp-cli/wp-cli-tests": "^4.2.8"
},
"config": {
"process-timeout": 7200,
Expand Down
56 changes: 56 additions & 0 deletions features/config-create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,62 @@ Feature: Create a wp-config file
Error: Database connection error
"""
@require-mysql
Scenario: Configure with database credentials using socket path
Given an empty directory
And WP files
And a find-socket.php file:
"""
<?php
// The WP_CLI_TEST_DBSOCKET variable can be set in the environment to
// override the default locations and will take precedence.
if ( ! empty( getenv( 'WP_CLI_TEST_DBSOCKET' ) ) ) {
echo getenv( 'WP_CLI_TEST_DBSOCKET' );
exit(0);
}
// From within Behat, the WP_CLI_TEST_DBSOCKET will be mapped to the internal
// DB_SOCKET variable, as Behat pushes a new environment context.
$locations = [
'{DB_SOCKET}',
'/var/run/mysqld/mysqld.sock',
'/tmp/mysql.sock',
];
foreach ( $locations as $location ) {
if ( ! empty( $location ) && file_exists( $location ) ) {
echo $location;
exit(0);
}
}
echo 'No socket found';
exit(1);
"""
When I run `php find-socket.php`
Then save STDOUT as {SOCKET}
And STDOUT should not be empty
When I try `wget -O {RUN_DIR}/install-package-tests https://raw.githubusercontent.com/wp-cli/wp-cli-tests/main/bin/install-package-tests`
Then STDERR should contain:
"""
install-package-tests' saved
"""
When I run `chmod +x {RUN_DIR}/install-package-tests`
Then STDERR should be empty
# We try to account for the warnings we get for passing the password on the command line.
When I try `MYSQL_HOST=localhost WP_CLI_TEST_DBHOST='localhost:{SOCKET}' WP_CLI_TEST_DBROOTPASS='root' {RUN_DIR}/install-package-tests`
Then STDOUT should contain:
"""
Detected MySQL
"""
When I run `wp config create --dbname='{DB_NAME}' --dbuser='{DB_USER}' --dbpass='{DB_PASSWORD}' --dbhost='localhost:{SOCKET}'`
Then the wp-config.php file should contain:
"""
define( 'DB_HOST', 'localhost:{SOCKET}' );
"""
@require-php-7.0
Scenario: Configure with salts generated
Given an empty directory
Expand Down
17 changes: 16 additions & 1 deletion src/Config_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,22 @@ public function create( $_, $assoc_args ) {
$mysql = mysqli_init();
mysqli_report( MYSQLI_REPORT_STRICT );
try {
mysqli_real_connect( $mysql, $assoc_args['dbhost'], $assoc_args['dbuser'], $assoc_args['dbpass'] );
// Accept similar format to one used by parse_db_host() e.g. 'localhost:/tmp/mysql.sock'
$socket = '';
$host = $assoc_args['dbhost'];
$socket_pos = strpos( $host, ':/' );
if ( false !== $socket_pos ) {
$socket = substr( $host, $socket_pos + 1 );
$host = substr( $host, 0, $socket_pos );
}

if ( file_exists( $socket ) ) {
// If dbhost is a path to a socket
mysqli_real_connect( $mysql, null, $assoc_args['dbuser'], $assoc_args['dbpass'], null, null, $socket );
} else {
// If dbhost is a hostname or IP address
mysqli_real_connect( $mysql, $host, $assoc_args['dbuser'], $assoc_args['dbpass'] );
}
} catch ( mysqli_sql_exception $exception ) {
WP_CLI::error( 'Database connection error (' . $exception->getCode() . ') ' . $exception->getMessage() );
}
Expand Down

0 comments on commit 890b6e3

Please sign in to comment.