Skip to content

Commit 1dc22cf

Browse files
committed
Make --dbname and --dbuser optional when SQLite is active
1 parent 271b902 commit 1dc22cf

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

features/config-create.feature

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,33 @@ 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 a WP install
296+
When I run `rm wp-config.php`
297+
When I run `wp config create --skip-salts`
298+
Then the return code should be 0
299+
And STDOUT should contain:
300+
"""
301+
Generated 'wp-config.php' file.
302+
"""
303+
304+
@skip-sqlite
305+
Scenario: Error when --dbname and --dbuser are missing without SQLite
306+
Given an empty directory
307+
And WP files
308+
309+
When I try `wp config create --skip-check --skip-salts`
310+
Then the return code should be 1
311+
And STDERR should contain:
312+
"""
313+
missing --dbname parameter
314+
"""
315+
And STDERR should contain:
316+
"""
317+
missing --dbuser parameter
318+
"""
319+
293320
@require-mysql @require-mysql-5.7
294321
Scenario: Configure with required SSL connection
295322
Given an empty directory

src/Config_Command.php

Lines changed: 39 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,24 @@ 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+
$errors = [];
227+
if ( ! isset( $assoc_args['dbname'] ) ) {
228+
$errors[] = 'missing --dbname parameter (Set the database name.)';
229+
}
230+
if ( ! isset( $assoc_args['dbuser'] ) ) {
231+
$errors[] = 'missing --dbuser parameter (Set the database user.)';
232+
}
233+
if ( ! empty( $errors ) ) {
234+
WP_CLI::error(
235+
'Parameter errors:' . "\n " . implode( "\n ", $errors )
236+
);
237+
}
238+
}
239+
222240
if ( empty( $assoc_args['dbprefix'] ) ) {
223241
WP_CLI::error( '--dbprefix cannot be empty' );
224242
}
@@ -228,7 +246,7 @@ public function create( $_, $assoc_args ) {
228246

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

@@ -1506,4 +1524,22 @@ private function escape_config_value( $key, $value ) {
15061524

15071525
return $value;
15081526
}
1527+
1528+
/**
1529+
* Check if the SQLite integration drop-in is active.
1530+
*
1531+
* @return bool
1532+
*/
1533+
private static function is_sqlite_integration_active() {
1534+
$wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content';
1535+
$db_dropin_path = $wp_content_dir . '/db.php';
1536+
1537+
if ( ! is_readable( $db_dropin_path ) ) {
1538+
return false;
1539+
}
1540+
1541+
$contents = file_get_contents( $db_dropin_path );
1542+
1543+
return false !== $contents && false !== strpos( $contents, 'SQLITE_DB_DROPIN_VERSION' );
1544+
}
15091545
}

0 commit comments

Comments
 (0)