From eb9ef6a83e7444501c920e177f7e547a3f08dc55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jakes=CC=8C?= Date: Thu, 1 Oct 2026 20:48:42 +0200 Subject: [PATCH] Prefer DB_PATH for SQLite database commands Use the database selected by WordPress when legacy constants conflict, while retaining the existing fallback for older plugin versions. Cover file operations with conflicting, matching, and legacy settings, including checks that the other database remains unchanged. https://github.com/WordPress/sqlite-database-integration/pull/512 --- features/db-sqlite-path.feature | 62 +++++++++++++++++++++++++++++++++ src/DB_Command_SQLite.php | 4 +++ 2 files changed, 66 insertions(+) create mode 100644 features/db-sqlite-path.feature diff --git a/features/db-sqlite-path.feature b/features/db-sqlite-path.feature new file mode 100644 index 00000000..60da84b5 --- /dev/null +++ b/features/db-sqlite-path.feature @@ -0,0 +1,62 @@ +@require-sqlite +Feature: Select the configured SQLite database for file operations + + Scenario Outline: Use the effective database path without changing another database + Given a WP install + When I run `wp eval 'copy( defined( "DB_PATH" ) ? DB_PATH : FQDB, "selected.sqlite" ); copy( "selected.sqlite", "other.sqlite" );'` + And I run `sqlite3 selected.sqlite "CREATE TABLE path_marker (value TEXT); INSERT INTO path_marker VALUES ('selected');"` + And I run `sqlite3 other.sqlite "CREATE TABLE path_marker (value TEXT); INSERT INTO path_marker VALUES ('other');"` + + Given a paths.php file: + """ + ' ) { + define( 'DB_PATH', __DIR__ . '/' ); + } + if ( '' !== '' ) { + define( 'FQDB', __DIR__ . '/' ); + } + if ( '' !== '' ) { + define( 'DB_DIR', __DIR__ . '/' ); + } + if ( '' !== '' ) { + define( 'DB_FILE', '' ); + } + if ( '' !== '' ) { + define( 'FQDBDIR', __DIR__ . '/' ); + } + """ + When I run `wp config set path_constants "require __DIR__ . '/paths.php'" --type=variable --raw` + And I try `wp eval 'echo "ready";'` + Then the return code should be 0 + + When I run `php -r 'echo md5_file( "other.sqlite" );'` + Then save STDOUT as {OTHER_HASH} + + When I try `wp db export selected.sql` + Then the return code should be 0 + And the selected.sql file should contain: + """ + INSERT INTO path_marker VALUES('selected'); + """ + And the selected.sql file should not contain: + """ + INSERT INTO path_marker VALUES('other'); + """ + + When I try `wp db drop --yes` + Then the return code should be 0 + And the selected.sqlite file should not exist + + When I run `php -r 'echo md5_file( "other.sqlite" );'` + Then STDOUT should be: + """ + {OTHER_HASH} + """ + + Examples: + | db_path | fqdb | db_dir | db_file | fqdbdir | + | selected.sqlite | other.sqlite | other | other.sqlite | other/ | + | selected.sqlite | selected.sqlite | / | selected.sqlite | / | + | | selected.sqlite | | | | + | | | | selected.sqlite | / | diff --git a/src/DB_Command_SQLite.php b/src/DB_Command_SQLite.php index 4ffe93a3..4ea185a7 100644 --- a/src/DB_Command_SQLite.php +++ b/src/DB_Command_SQLite.php @@ -63,6 +63,10 @@ protected function is_sqlite() { * @return string|false Path to SQLite database file, or false if not found. */ protected function get_sqlite_db_path() { + if ( defined( 'DB_PATH' ) ) { + return DB_PATH; + } + // Check for FQDB constant (fully qualified database path). if ( defined( 'FQDB' ) ) { return FQDB;