diff --git a/features/db-sqlite-path.feature b/features/db-sqlite-path.feature new file mode 100644 index 00000000..37e7de1d --- /dev/null +++ b/features/db-sqlite-path.feature @@ -0,0 +1,96 @@ +@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 `wp eval 'echo md5_file( "other.sqlite" );' --skip-wordpress` + 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 `wp eval 'echo md5_file( "other.sqlite" );' --skip-wordpress` + 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 | / | + + Scenario Outline: Reject file operations on an in-memory SQLite database + Given a WP install + And an input.sql file: + """ + SELECT 1; + """ + When I run `wp config set :memory:` + And I run `wp db query "SELECT 1 AS value" --skip-column-names` + Then STDOUT should be: + """ + 1 + """ + + When I try `wp db ` + Then the return code should be 1 + And STDERR should be: + """ + Error: This command does not support in-memory SQLite databases. + """ + And STDOUT should be empty + + Examples: + | constant | command | + | DB_PATH | create | + | DB_PATH | drop --yes | + | DB_PATH | reset --yes | + | DB_PATH | export output.sql | + | DB_PATH | import input.sql | + | FQDB | create | + | FQDB | drop --yes | + | FQDB | reset --yes | + | FQDB | export output.sql | + | FQDB | import input.sql | diff --git a/features/db.feature b/features/db.feature index 2fcbe899..07656dcb 100644 --- a/features/db.feature +++ b/features/db.feature @@ -387,8 +387,7 @@ Feature: Perform database operations Query succeeded. Rows affected: 1 """ - @require-sqlite @skip-windows - # Skipped on Windows due to persistent file locking issues when run via Behat. + @require-sqlite Scenario: SQLite DB CRUD operations Given a WP install And a session_yes file: @@ -425,8 +424,7 @@ Feature: Perform database operations total """ - @require-sqlite @skip-windows - # Skipped on Windows due to persistent file locking issues when run via Behat. + @require-sqlite Scenario: SQLite DB export/import Given a WP install And a session_yes file: diff --git a/src/DB_Command_SQLite.php b/src/DB_Command_SQLite.php index 4ffe93a3..b864bf19 100644 --- a/src/DB_Command_SQLite.php +++ b/src/DB_Command_SQLite.php @@ -63,8 +63,20 @@ 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' ) ) { + if ( ':memory:' === DB_PATH ) { + WP_CLI::error( 'This command does not support in-memory SQLite databases.' ); + } + + return DB_PATH; + } + // Check for FQDB constant (fully qualified database path). if ( defined( 'FQDB' ) ) { + if ( ':memory:' === FQDB ) { + WP_CLI::error( 'This command does not support in-memory SQLite databases.' ); + } + return FQDB; }