Skip to content

Commit 6e549e6

Browse files
CopilotswissspidyCopilot
authored
Make --dbname and --dbuser optional when SQLite integration is active (#219)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent 6df0642 commit 6e549e6

2 files changed

Lines changed: 83 additions & 5 deletions

File tree

features/config-create.feature

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,27 @@ Feature: Create a wp-config file
163163
Error: Database connection error
164164
"""
165165
166+
@require-mysql
167+
Scenario: Missing --dbname or --dbuser without SQLite integration
168+
Given an empty directory
169+
And WP files
170+
171+
When I try `wp config create --skip-check --dbuser=someuser`
172+
Then the return code should be 1
173+
And STDERR should contain:
174+
"""
175+
Error: Parameter errors:
176+
missing --dbname parameter (Set the database name.)
177+
"""
178+
179+
When I try `wp config create --skip-check --dbname=somedb`
180+
Then the return code should be 1
181+
And STDERR should contain:
182+
"""
183+
Error: Parameter errors:
184+
missing --dbuser parameter (Set the database user.)
185+
"""
186+
166187
@require-mysql
167188
Scenario: Configure with database credentials using socket path
168189
Given an empty directory
@@ -290,6 +311,23 @@ Feature: Create a wp-config file
290311
Then the return code should be 0
291312
And the subdir/wp-config.php file should exist
292313
314+
@require-sqlite
315+
Scenario: Configure without --dbname and --dbuser when SQLite integration is active
316+
Given an empty directory
317+
And WP files
318+
And a wp-content/db.php file:
319+
"""
320+
<?php
321+
define( 'SQLITE_DB_DROPIN_VERSION', '1.0.0' );
322+
"""
323+
324+
When I run `wp config create --skip-salts`
325+
Then the return code should be 0
326+
And STDOUT should contain:
327+
"""
328+
Generated 'wp-config.php' file.
329+
"""
330+
293331
@require-mysql @require-mysql-5.7
294332
Scenario: Configure with required SSL connection
295333
Given an empty directory

src/Config_Command.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ private static function get_initial_locale() {
120120
*
121121
* ## OPTIONS
122122
*
123-
* --dbname=<dbname>
124-
* : Set the database name.
123+
* [--dbname=<dbname>]
124+
* : Set the database name. Required unless the SQLite integration drop-in is detected.
125125
*
126-
* --dbuser=<dbuser>
127-
* : Set the database user.
126+
* [--dbuser=<dbuser>]
127+
* : Set the database user. Required unless the SQLite integration drop-in is detected.
128128
*
129129
* [--dbpass=<dbpass>]
130130
* : Set the database user password.
@@ -219,6 +219,22 @@ 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 ( empty( $assoc_args['dbname'] ) ) {
228+
$errors[] = 'missing --dbname parameter (Set the database name.)';
229+
}
230+
if ( empty( $assoc_args['dbuser'] ) ) {
231+
$errors[] = 'missing --dbuser parameter (Set the database user.)';
232+
}
233+
if ( ! empty( $errors ) ) {
234+
WP_CLI::error( 'Parameter errors:' . PHP_EOL . implode( PHP_EOL, $errors ) );
235+
}
236+
}
237+
222238
if ( empty( $assoc_args['dbprefix'] ) ) {
223239
WP_CLI::error( '--dbprefix cannot be empty' );
224240
}
@@ -228,7 +244,7 @@ public function create( $_, $assoc_args ) {
228244

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

@@ -1480,6 +1496,30 @@ private function print_dotenv( array $value ) {
14801496
WP_CLI::line( "{$name}={$variable_value}" );
14811497
}
14821498

1499+
/**
1500+
* Check if the SQLite integration drop-in is active.
1501+
*
1502+
* @return bool True if SQLite integration is detected, false otherwise.
1503+
*/
1504+
private static function is_sqlite_integration_active() {
1505+
$wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content';
1506+
$db_dropin_path = $wp_content_dir . '/db.php';
1507+
1508+
if ( ! is_file( $db_dropin_path ) || ! is_readable( $db_dropin_path ) ) {
1509+
return false;
1510+
}
1511+
1512+
$db_dropin_contents = file_get_contents( $db_dropin_path, false, null, 0, 8192 );
1513+
if ( false === $db_dropin_contents ) {
1514+
return false;
1515+
}
1516+
1517+
return 1 === preg_match(
1518+
'/\b(?:define\s*\(\s*[\'"]SQLITE_DB_DROPIN_VERSION[\'"]|const\s+SQLITE_DB_DROPIN_VERSION\b)/',
1519+
$db_dropin_contents
1520+
);
1521+
}
1522+
14831523
/**
14841524
* Escape a config value so it can be safely used within single quotes.
14851525
*

0 commit comments

Comments
 (0)