Skip to content

Commit 7fc493c

Browse files
Copilotswissspidy
andauthored
Make --dbname/--dbuser optional in config create; add SQLite detection and skip MySQL check for SQLite
Agent-Logs-Url: https://github.com/wp-cli/config-command/sessions/8f9ece6b-b44b-4ac7-87e2-35bad2b2afff Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent d0751b6 commit 7fc493c

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

features/config-create.feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,23 @@ Feature: Create a wp-config file
290290
Then the return code should be 0
291291
And the subdir/wp-config.php file should exist
292292
293+
@require-sqlite
294+
Scenario: Configure without --dbname and --dbuser when SQLite integration is active
295+
Given an empty directory
296+
And WP files
297+
And a wp-content/db.php file:
298+
"""
299+
<?php
300+
define( 'SQLITE_DB_DROPIN_VERSION', '1.0.0' );
301+
"""
302+
303+
When I run `wp config create --skip-salts --skip-check`
304+
Then the return code should be 0
305+
And STDOUT should contain:
306+
"""
307+
Generated 'wp-config.php' file.
308+
"""
309+
293310
@require-mysql @require-mysql-5.7
294311
Scenario: Configure with required SSL connection
295312
Given an empty directory

src/Config_Command.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ private static function get_initial_locale() {
120120
*
121121
* ## OPTIONS
122122
*
123-
* --dbname=<dbname>
123+
* [--dbname=<dbname>]
124124
* : Set the database name.
125125
*
126-
* --dbuser=<dbuser>
126+
* [--dbuser=<dbuser>]
127127
* : Set the database user.
128128
*
129129
* [--dbpass=<dbpass>]
@@ -219,6 +219,18 @@ public function create( $_, $assoc_args ) {
219219
'ssl' => false,
220220
];
221221
$assoc_args = array_merge( $defaults, $assoc_args );
222+
223+
$is_sqlite = self::is_sqlite_integration_active();
224+
225+
if ( ! $is_sqlite ) {
226+
if ( empty( $assoc_args['dbname'] ) ) {
227+
WP_CLI::error( 'Parameter errors:' . PHP_EOL . 'missing --dbname parameter (Set the database name.)' );
228+
}
229+
if ( empty( $assoc_args['dbuser'] ) ) {
230+
WP_CLI::error( 'Parameter errors:' . PHP_EOL . 'missing --dbuser parameter (Set the database user.)' );
231+
}
232+
}
233+
222234
if ( empty( $assoc_args['dbprefix'] ) ) {
223235
WP_CLI::error( '--dbprefix cannot be empty' );
224236
}
@@ -228,7 +240,7 @@ public function create( $_, $assoc_args ) {
228240

229241
// Check DB connection. To make command more portable, we are not using MySQL CLI and using
230242
// mysqli directly instead, as $wpdb is not accessible in this context.
231-
if ( ! Utils\get_flag_value( $assoc_args, 'skip-check' ) ) {
243+
if ( ! $is_sqlite && ! Utils\get_flag_value( $assoc_args, 'skip-check' ) ) {
232244
// phpcs:disable WordPress.DB.RestrictedFunctions
233245
$mysql = mysqli_init();
234246

@@ -1480,6 +1492,25 @@ private function print_dotenv( array $value ) {
14801492
WP_CLI::line( "{$name}={$variable_value}" );
14811493
}
14821494

1495+
/**
1496+
* Check if the SQLite integration drop-in is active.
1497+
*
1498+
* @return bool True if SQLite integration is detected, false otherwise.
1499+
*/
1500+
private static function is_sqlite_integration_active() {
1501+
$wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content';
1502+
$db_dropin_path = $wp_content_dir . '/db.php';
1503+
1504+
if ( file_exists( $db_dropin_path ) ) {
1505+
$db_dropin_contents = file_get_contents( $db_dropin_path, false, null, 0, 8192 );
1506+
if ( false !== $db_dropin_contents && false !== strpos( $db_dropin_contents, 'SQLITE_DB_DROPIN_VERSION' ) ) {
1507+
return true;
1508+
}
1509+
}
1510+
1511+
return false;
1512+
}
1513+
14831514
/**
14841515
* Escape a config value so it can be safely used within single quotes.
14851516
*

0 commit comments

Comments
 (0)