From 1dc22cf2b540d18b5d6e828ecf1189d01be6b0e4 Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Thu, 9 Apr 2026 16:27:26 +0530 Subject: [PATCH 1/2] Make --dbname and --dbuser optional when SQLite is active --- features/config-create.feature | 27 ++++++++++++++++++++++ src/Config_Command.php | 42 +++++++++++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/features/config-create.feature b/features/config-create.feature index c93bb604..de7113b6 100644 --- a/features/config-create.feature +++ b/features/config-create.feature @@ -290,6 +290,33 @@ Feature: Create a wp-config file Then the return code should be 0 And the subdir/wp-config.php file should exist + @require-sqlite + Scenario: Configure without --dbname and --dbuser when SQLite integration is active + Given a WP install + When I run `rm wp-config.php` + When I run `wp config create --skip-salts` + Then the return code should be 0 + And STDOUT should contain: + """ + Generated 'wp-config.php' file. + """ + + @skip-sqlite + Scenario: Error when --dbname and --dbuser are missing without SQLite + Given an empty directory + And WP files + + When I try `wp config create --skip-check --skip-salts` + Then the return code should be 1 + And STDERR should contain: + """ + missing --dbname parameter + """ + And STDERR should contain: + """ + missing --dbuser parameter + """ + @require-mysql @require-mysql-5.7 Scenario: Configure with required SSL connection Given an empty directory diff --git a/src/Config_Command.php b/src/Config_Command.php index 63ac1f55..946461ae 100644 --- a/src/Config_Command.php +++ b/src/Config_Command.php @@ -120,10 +120,10 @@ private static function get_initial_locale() { * * ## OPTIONS * - * --dbname= + * [--dbname=] * : Set the database name. * - * --dbuser= + * [--dbuser=] * : Set the database user. * * [--dbpass=] @@ -219,6 +219,24 @@ public function create( $_, $assoc_args ) { 'ssl' => false, ]; $assoc_args = array_merge( $defaults, $assoc_args ); + + $is_sqlite = self::is_sqlite_integration_active(); + + if ( ! $is_sqlite ) { + $errors = []; + if ( ! isset( $assoc_args['dbname'] ) ) { + $errors[] = 'missing --dbname parameter (Set the database name.)'; + } + if ( ! isset( $assoc_args['dbuser'] ) ) { + $errors[] = 'missing --dbuser parameter (Set the database user.)'; + } + if ( ! empty( $errors ) ) { + WP_CLI::error( + 'Parameter errors:' . "\n " . implode( "\n ", $errors ) + ); + } + } + if ( empty( $assoc_args['dbprefix'] ) ) { WP_CLI::error( '--dbprefix cannot be empty' ); } @@ -228,7 +246,7 @@ public function create( $_, $assoc_args ) { // Check DB connection. To make command more portable, we are not using MySQL CLI and using // mysqli directly instead, as $wpdb is not accessible in this context. - if ( ! Utils\get_flag_value( $assoc_args, 'skip-check' ) ) { + if ( ! $is_sqlite && ! Utils\get_flag_value( $assoc_args, 'skip-check' ) ) { // phpcs:disable WordPress.DB.RestrictedFunctions $mysql = mysqli_init(); @@ -1506,4 +1524,22 @@ private function escape_config_value( $key, $value ) { return $value; } + + /** + * Check if the SQLite integration drop-in is active. + * + * @return bool + */ + private static function is_sqlite_integration_active() { + $wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content'; + $db_dropin_path = $wp_content_dir . '/db.php'; + + if ( ! is_readable( $db_dropin_path ) ) { + return false; + } + + $contents = file_get_contents( $db_dropin_path ); + + return false !== $contents && false !== strpos( $contents, 'SQLITE_DB_DROPIN_VERSION' ); + } } From 90c59319ce004bdafd618107798777739477f5a7 Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Thu, 9 Apr 2026 16:41:52 +0530 Subject: [PATCH 2/2] Address code review feedback --- src/Config_Command.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Config_Command.php b/src/Config_Command.php index 946461ae..e361a34d 100644 --- a/src/Config_Command.php +++ b/src/Config_Command.php @@ -225,10 +225,10 @@ public function create( $_, $assoc_args ) { if ( ! $is_sqlite ) { $errors = []; if ( ! isset( $assoc_args['dbname'] ) ) { - $errors[] = 'missing --dbname parameter (Set the database name.)'; + $errors[] = 'missing --dbname parameter'; } if ( ! isset( $assoc_args['dbuser'] ) ) { - $errors[] = 'missing --dbuser parameter (Set the database user.)'; + $errors[] = 'missing --dbuser parameter'; } if ( ! empty( $errors ) ) { WP_CLI::error( @@ -1531,7 +1531,7 @@ private function escape_config_value( $key, $value ) { * @return bool */ private static function is_sqlite_integration_active() { - $wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content'; + $wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : rtrim( ABSPATH, '/\\' ) . '/wp-content'; $db_dropin_path = $wp_content_dir . '/db.php'; if ( ! is_readable( $db_dropin_path ) ) {