Skip to content

Commit eb9ef6a

Browse files
committed
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. WordPress/sqlite-database-integration#512
1 parent 23dddd7 commit eb9ef6a

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎features/db-sqlite-path.feature‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
@require-sqlite
2+
Feature: Select the configured SQLite database for file operations
3+
4+
Scenario Outline: Use the effective database path without changing another database
5+
Given a WP install
6+
When I run `wp eval 'copy( defined( "DB_PATH" ) ? DB_PATH : FQDB, "selected.sqlite" ); copy( "selected.sqlite", "other.sqlite" );'`
7+
And I run `sqlite3 selected.sqlite "CREATE TABLE path_marker (value TEXT); INSERT INTO path_marker VALUES ('selected');"`
8+
And I run `sqlite3 other.sqlite "CREATE TABLE path_marker (value TEXT); INSERT INTO path_marker VALUES ('other');"`
9+
10+
Given a paths.php file:
11+
"""
12+
<?php
13+
if ( '' !== '<db_path>' ) {
14+
define( 'DB_PATH', __DIR__ . '/<db_path>' );
15+
}
16+
if ( '' !== '<fqdb>' ) {
17+
define( 'FQDB', __DIR__ . '/<fqdb>' );
18+
}
19+
if ( '' !== '<db_dir>' ) {
20+
define( 'DB_DIR', __DIR__ . '/<db_dir>' );
21+
}
22+
if ( '' !== '<db_file>' ) {
23+
define( 'DB_FILE', '<db_file>' );
24+
}
25+
if ( '' !== '<fqdbdir>' ) {
26+
define( 'FQDBDIR', __DIR__ . '/<fqdbdir>' );
27+
}
28+
"""
29+
When I run `wp config set path_constants "require __DIR__ . '/paths.php'" --type=variable --raw`
30+
And I try `wp eval 'echo "ready";'`
31+
Then the return code should be 0
32+
33+
When I run `php -r 'echo md5_file( "other.sqlite" );'`
34+
Then save STDOUT as {OTHER_HASH}
35+
36+
When I try `wp db export selected.sql`
37+
Then the return code should be 0
38+
And the selected.sql file should contain:
39+
"""
40+
INSERT INTO path_marker VALUES('selected');
41+
"""
42+
And the selected.sql file should not contain:
43+
"""
44+
INSERT INTO path_marker VALUES('other');
45+
"""
46+
47+
When I try `wp db drop --yes`
48+
Then the return code should be 0
49+
And the selected.sqlite file should not exist
50+
51+
When I run `php -r 'echo md5_file( "other.sqlite" );'`
52+
Then STDOUT should be:
53+
"""
54+
{OTHER_HASH}
55+
"""
56+
57+
Examples:
58+
| db_path | fqdb | db_dir | db_file | fqdbdir |
59+
| selected.sqlite | other.sqlite | other | other.sqlite | other/ |
60+
| selected.sqlite | selected.sqlite | / | selected.sqlite | / |
61+
| | selected.sqlite | | | |
62+
| | | | selected.sqlite | / |

‎src/DB_Command_SQLite.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ protected function is_sqlite() {
6363
* @return string|false Path to SQLite database file, or false if not found.
6464
*/
6565
protected function get_sqlite_db_path() {
66+
if ( defined( 'DB_PATH' ) ) {
67+
return DB_PATH;
68+
}
69+
6670
// Check for FQDB constant (fully qualified database path).
6771
if ( defined( 'FQDB' ) ) {
6872
return FQDB;

0 commit comments

Comments
 (0)