Skip to content

Conversation

@JanJakes
Copy link
Contributor

@JanJakes JanJakes commented Aug 6, 2025

When the DB_NAME constant is not defined in wp-config.php, using the new SQLite driver with the CLI commands would fail with the following error:

[2025-08-06T07:09:58.483Z][warn][wp-cli-process] PHP.run() output was: #!/usr/bin/env php
<br />
<b>Fatal error</b>:  Uncaught Error: Undefined constant &quot;Automattic\WP_CLI\SQLite\DB_NAME&quot; in /tmp/sqlite-command/src/SQLiteDriverFactory.php:24
Stack trace:
#0 /tmp/sqlite-command/src/Import.php(21): Automattic\WP_CLI\SQLite\SQLiteDriverFactory::create_driver()
#1 /tmp/sqlite-command/src/SQLite_Command.php(44): Automattic\WP_CLI\SQLite\Import-&gt;__construct()
#2 [internal function]: Automattic\WP_CLI\SQLite\SQLite_Command-&gt;import(Array, Array)
#3 /tmp/sqlite-command/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php(100): call_user_func(Array, Array, Array)
#4 [internal function]: WP_CLI\Dispatcher\CommandFactory::WP_CLI\Dispatcher\{closure}(Array, Array)
#5 /tmp/sqlite-command/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php(497): call_user_func(Object(Closure), Array, Array)
#6 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(470): WP_CLI\Dispatcher\Subcommand-&gt;invoke(Array, Array, Array)
#7 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(493): WP_CLI\Runner-&gt;run_command(Array, Array)
#8 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(136): WP_CLI\Runner-&gt;run_command_and_exit()
#9 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1349): WP_CLI\Runner-&gt;do_early_invoke('after_wp_config...')
#10 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1293): WP_CLI\Runner-&gt;load_wordpress()
#11 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php(28): WP_CLI\Runner-&gt;start()
#12 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/bootstrap.php(84): WP_CLI\Bootstrap\LaunchRunner-&gt;process(Object(WP_CLI\Bootstrap\BootstrapState))
#13 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/wp-cli.php(35): WP_CLI\bootstrap()
#14 phar:///tmp/wp-cli.phar/php/boot-phar.php(20): include('phar:///tmp/wp-...')
#15 /tmp/wp-cli.phar(4): include('phar:///tmp/wp-...')
#16 /tmp/run-cli.php(37): require('/tmp/wp-cli.pha...')
#17 {main}
  thrown in <b>/tmp/sqlite-command/src/SQLiteDriverFactory.php</b> on line <b>24</b><br />

This fix ensures a database name is provided, using the same fallback that Studio relies on.

@JanJakes JanJakes requested a review from wojtekn August 6, 2025 07:38
@wojtekn
Copy link
Contributor

wojtekn commented Aug 6, 2025

I tested it using following steps:

  1. Clone the PR locally
  2. Run composer install --no-dev --optimize-autoloader --ignore-platform-reqs in the wp-cli-sqlite-command directory
  3. Replace wp-files/sqlite-command in the Studio code with a symlink to your local wp-cli-sqlite-command directory
 rm -rf sqlite-command
 ln -s ../../wp-cli-sqlite-command sqlite-command
  1. Apply the patch to the Studio repo:
diff --git a/src/setup-wp-server-files.ts b/src/setup-wp-server-files.ts
index a81b7026..536c14a1 100644
--- a/src/setup-wp-server-files.ts
+++ b/src/setup-wp-server-files.ts
@@ -86,17 +86,11 @@ async function copyBundledWPCLI() {
 
 async function copyBundledSQLiteCommand() {
        const bundledSqliteCommandPath = path.join( getResourcesPath(), 'wp-files', 'sqlite-command' );
-       const bundledSqliteCommandVersion = await getSQLiteCommandVersion( bundledSqliteCommandPath );
-       if ( ! bundledSqliteCommandVersion ) {
-               return;
-       }
+       const bundledSqliteCommandVersion = '1.1.1';
        const installedSqliteCommandPath = getSqliteCommandPath();
        const isSqliteCommandInstalled = await fs.pathExists( installedSqliteCommandPath );
 
-       const installedSqliteCommandVersion = await getSQLiteCommandVersion( installedSqliteCommandPath );
-       const isBundledVersionNewer =
-               installedSqliteCommandVersion &&
-               semver.gt( bundledSqliteCommandVersion, installedSqliteCommandVersion );
+       const isBundledVersionNewer = true;
        if ( ! isSqliteCommandInstalled || isBundledVersionNewer ) {
                console.log( `Copying bundled SQLite command version ${ bundledSqliteCommandVersion }…` );
                await recursiveCopyDirectory( bundledSqliteCommandPath, installedSqliteCommandPath );
@@ -114,5 +108,5 @@ export async function updateWPServerFiles() {
        await updateLatestWordPressVersion();
        await updateLatestSqliteVersion();
        await updateLatestWPCliVersion();
-       await updateLatestSQLiteCommandVersion();
+       //await updateLatestSQLiteCommandVersion();
 }

  1. Remove current command files:
rm -rf ~/Library/Application\ Support/Studio/server-files/sqlite-command 
  1. Run npm start. It will copy the symlinked command files to directory under server-files/
  2. Click 'Add site'
  3. Choose a backup that includes a fresh Jetpack export with wp-config.php that misses database constants
  4. Confirm import is completed successfully

The PR fixes the reported issue.

However, such a site still doesn't load and shows the "Error establishing a database connection" error.

@wojtekn
Copy link
Contributor

wojtekn commented Aug 6, 2025

However, such a site still doesn't load and shows the "Error establishing a database connection" error.

We fixed this issue in the SQLite plugin 2.2.6.

@wojtekn wojtekn merged commit b7593cf into main Aug 6, 2025
34 checks passed
@wojtekn wojtekn deleted the fix-missing-db-name-constant branch August 6, 2025 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants