Skip to content

Commit 036b3c1

Browse files
authored
Support DB_PATH for SQLite database commands (#351)
1 parent e09eb5d commit 036b3c1

3 files changed

Lines changed: 110 additions & 4 deletions

File tree

‎features/db-sqlite-path.feature‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 `wp eval 'echo md5_file( "other.sqlite" );' --skip-wordpress`
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 `wp eval 'echo md5_file( "other.sqlite" );' --skip-wordpress`
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 | / |
63+
64+
Scenario Outline: Reject file operations on an in-memory SQLite database
65+
Given a WP install
66+
And an input.sql file:
67+
"""
68+
SELECT 1;
69+
"""
70+
When I run `wp config set <constant> :memory:`
71+
And I run `wp db query "SELECT 1 AS value" --skip-column-names`
72+
Then STDOUT should be:
73+
"""
74+
1
75+
"""
76+
77+
When I try `wp db <command>`
78+
Then the return code should be 1
79+
And STDERR should be:
80+
"""
81+
Error: This command does not support in-memory SQLite databases.
82+
"""
83+
And STDOUT should be empty
84+
85+
Examples:
86+
| constant | command |
87+
| DB_PATH | create |
88+
| DB_PATH | drop --yes |
89+
| DB_PATH | reset --yes |
90+
| DB_PATH | export output.sql |
91+
| DB_PATH | import input.sql |
92+
| FQDB | create |
93+
| FQDB | drop --yes |
94+
| FQDB | reset --yes |
95+
| FQDB | export output.sql |
96+
| FQDB | import input.sql |

‎features/db.feature‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,7 @@ Feature: Perform database operations
387387
Query succeeded. Rows affected: 1
388388
"""
389389
390-
@require-sqlite @skip-windows
391-
# Skipped on Windows due to persistent file locking issues when run via Behat.
390+
@require-sqlite
392391
Scenario: SQLite DB CRUD operations
393392
Given a WP install
394393
And a session_yes file:
@@ -425,8 +424,7 @@ Feature: Perform database operations
425424
total
426425
"""
427426
428-
@require-sqlite @skip-windows
429-
# Skipped on Windows due to persistent file locking issues when run via Behat.
427+
@require-sqlite
430428
Scenario: SQLite DB export/import
431429
Given a WP install
432430
And a session_yes file:

‎src/DB_Command_SQLite.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,20 @@ 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+
if ( ':memory:' === DB_PATH ) {
68+
WP_CLI::error( 'This command does not support in-memory SQLite databases.' );
69+
}
70+
71+
return DB_PATH;
72+
}
73+
6674
// Check for FQDB constant (fully qualified database path).
6775
if ( defined( 'FQDB' ) ) {
76+
if ( ':memory:' === FQDB ) {
77+
WP_CLI::error( 'This command does not support in-memory SQLite databases.' );
78+
}
79+
6880
return FQDB;
6981
}
7082

0 commit comments

Comments
 (0)