Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d18bb5f
Add support for MySQL socket connection
wojtekn Nov 10, 2023
31157da
Fix formatting
wojtekn Nov 10, 2023
7b792ed
Add test for the socket connection
wojtekn Nov 10, 2023
ff8be33
Skip using real database and check if DB_HOST is written
wojtekn Nov 10, 2023
976b7c6
Get the socket from MySQL from the valid connection
danielbachhuber Nov 10, 2023
9468056
Use a file-based socket finder
danielbachhuber Nov 10, 2023
74e0401
Verify STDOUT is not empty
danielbachhuber Nov 10, 2023
7ebb9de
Adjust accepted socket path format to include the host
wojtekn Nov 13, 2023
3a99ee9
Use individual DB variables to avoid defining host twice
wojtekn Nov 13, 2023
f23ed8b
Add quotes to DBHOST argument
schlessera Dec 18, 2023
63cc209
Try preparing the tests against the socket
schlessera Dec 18, 2023
e9264e5
Try using an IP address instead of localhost
schlessera Dec 18, 2023
219fd4f
Revert IP address back to localhost
schlessera Dec 18, 2023
da3b084
Ensure latest wp-cli-tests is pulled in
schlessera Dec 18, 2023
f233cbf
Prepare tests upfront
schlessera Dec 18, 2023
96ee898
Use RUN_DIR variable
schlessera Dec 18, 2023
83cf9ec
Download install-package-tests manually
schlessera Dec 18, 2023
933f20f
Add missing host
schlessera Dec 18, 2023
eea7a6a
Try using IP address instead of localhost
schlessera Dec 18, 2023
d9517e6
Bump required wp-cli-tests version
schlessera Dec 18, 2023
c4a6ee5
Socket protocol only works with localhost
schlessera Dec 18, 2023
700da25
Add root password
schlessera Dec 19, 2023
fc6207e
Account for warnings
schlessera Dec 19, 2023
0f096aa
Remove password for bundled MySQL
schlessera Dec 19, 2023
490535c
Use custom filename first to disambiguate
schlessera Dec 19, 2023
a55018c
Use latest wp-cli-tests
schlessera Dec 20, 2023
90d0465
Adapt socket test
schlessera Dec 20, 2023
5525e65
Remove vendor folder socket dir
schlessera Dec 20, 2023
025fd47
Add comment to test script
schlessera Dec 20, 2023
040008e
Do not check the env-provided socket for validity
schlessera Dec 20, 2023
852c3a0
Use Behat internal var DB_SOCKET
schlessera Dec 20, 2023
aa82b62
Add MYSQL_HOST for socket connection
schlessera Dec 21, 2023
c8e1c33
Simplify MYSQL_HOST
schlessera Dec 21, 2023
10677fe
Add root password
schlessera Dec 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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