Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions features/bootstrap/SQLiteFeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,60 @@ class SQLiteFeatureContext extends WPCLIFeatureContext implements Context {
private $db;
private $comment_injection_table_name;

/**
* Install WordPress with the integration plugin under test.
*
* @param string $subdir Installation subdirectory.
* @param string $version WordPress version.
*/
public function install_wp( $subdir = '', $version = '' ): void {
if ( 'sqlite' !== getenv( 'WP_CLI_TEST_DBTYPE' ) ) {
parent::install_wp( $subdir, $version );
return;
}

// The upstream installation cache copies only the fixed .ht.sqlite path.
// Install SQLite sites directly so managed storage is also preserved.
$subdir = $this->replace_variables( $subdir );
$this->create_run_dir();
$this->download_wp( $subdir, $version );

$plugin_directory = getenv( 'WP_CLI_TEST_SQLITE_PLUGIN_DIR' );
if ( $plugin_directory ) {
if ( ! is_file( $plugin_directory . '/db.copy' ) ) {
throw new Exception( 'WP_CLI_TEST_SQLITE_PLUGIN_DIR must point to an integration plugin directory containing db.copy.' );
}
$run_dir = $this->variables['RUN_DIR'] . '/' . $subdir;
$target = $run_dir . '/wp-content/mu-plugins/sqlite-database-integration';
self::remove_dir( $target );
mkdir( $target );
// Plugin source checkouts link to the shared driver package.
$this->proc( \WP_CLI\Utils\esc_cmd( 'cp -RL %s/. %s', $plugin_directory, $target ) )->run_check();
file_put_contents(
$run_dir . '/wp-content/db.php',
str_replace(
[ "'{SQLITE_IMPLEMENTATION_FOLDER_PATH}'", '{SQLITE_PLUGIN}', '/plugins/' ],
[ "__DIR__ . '/mu-plugins/sqlite-database-integration'", 'sqlite-database-integration/load.php', '/mu-plugins/' ],
file_get_contents( $target . '/db.copy' )
)
);
}

$this->create_config( $subdir, "define( 'DISABLE_WP_CRON', true );" );
$this->proc(
'wp core install',
[
'url' => 'https://example.com',
'title' => 'WP CLI Site',
'admin_user' => 'admin',
'admin_email' => 'admin@example.com',
'admin_password' => 'password1',
'skip-email' => true,
],
$subdir
)->run_check();
}

/**
* @Given /^a SQL dump file named "([^"]*)" with content:$/
*/
Expand Down Expand Up @@ -71,8 +125,11 @@ public function theFileShouldExist( $filename ) {

private function connectToDatabase() {
if ( ! $this->db ) {
$run_dir = $this->variables['RUN_DIR'];
$db_file = $run_dir . '/wp-content/database/.ht.sqlite';
$result = $this->proc( 'wp eval "echo FQDB;"' )->run_check();
$db_file = trim( $result->stdout );
if ( ! is_file( $db_file ) ) {
throw new Exception( 'The WordPress SQLite database does not exist.' );
}
$this->db = new PDO( 'sqlite:' . $db_file );
$this->db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
Expand Down
15 changes: 7 additions & 8 deletions features/sqlite-import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ Feature: WP-CLI SQLite Import Command
And the SQLite database should contain the imported data

@require-sqlite
Scenario: Import a file with escape sequences
Scenario: Import a file with MySQL backslash escape sequences
Given a SQL dump file named "test_import.sql" with content:
"""
SET sql_mode='NO_BACKSLASH_ESCAPES';
CREATE TABLE test_table (id INTEGER PRIMARY KEY AUTO_INCREMENT, name TEXT);
INSERT INTO test_table (name) VALUES ('Test that escaping a backslash \\ works');
INSERT INTO test_table (name) VALUES ('Test that escaping multiple backslashes \\\\\\ works');
Expand All @@ -62,11 +61,11 @@ Feature: WP-CLI SQLite Import Command
Success: Imported from 'test_import.sql'.
"""
And the SQLite database should contain a table named "test_table"
And the "test_table" should contain a row with name "Test that escaping a backslash \\ works"
And the "test_table" should contain a row with name "Test that escaping multiple backslashes \\\\\\ works"
And the "test_table" should contain a row with name "Test that escaping a character \a works"
And the "test_table" should contain a row with name "Test that escaping a backslash followed by a character \\a works"
And the "test_table" should contain a row with name "Test that escaping a backslash and a character \\\a works"
And the "test_table" should contain a row with name "Test that escaping a backslash \ works"
And the "test_table" should contain a row with name "Test that escaping multiple backslashes \\\ works"
And the "test_table" should contain a row with name "Test that escaping a character a works"
And the "test_table" should contain a row with name "Test that escaping a backslash followed by a character \a works"
And the "test_table" should contain a row with name "Test that escaping a backslash and a character \a works"

@require-sqlite
Scenario: Import a file with newlines in strings
Expand Down Expand Up @@ -150,7 +149,7 @@ Feature: WP-CLI SQLite Import Command
Success: Imported from 'test_import.sql'.
"""

And the SQLite database should contain a table named "a'strange`identifier\name"
And the SQLite database should contain a table named "a'strange`identifier\\name"

@require-sqlite
Scenario: Import a file with whitespace and empty lines
Expand Down
164 changes: 164 additions & 0 deletions features/sqlite-storage.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
@require-sqlite
Feature: SQLite database storage
Commands use the same database as WordPress before its database drop-in loads.

Background:
Given a WP installation

Scenario: Use an existing legacy database without migrating storage
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE test_table (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO test_table VALUES (1, 'Imported data');
"""
When I run `wp eval 'echo FQDB;'`
Then save STDOUT as {DATABASE_PATH}
When I run `cp "{DATABASE_PATH}" original.sqlite`
And I run `rm -r wp-content/database`
And I run `mkdir wp-content/database`
And I run `mv original.sqlite wp-content/database/.ht.sqlite`
And I run `wp sqlite export backup.sql`
Then the backup.sql file should contain:
"""
WP CLI Site
"""
When I run `wp sqlite tables`
Then STDOUT should contain:
"""
wp_options
"""
When I run `wp sqlite import test_import.sql`
And I run `wp sqlite export -`
Then STDOUT should contain:
"""
Imported data
"""
And the wp-content/database/.ht.sqlite file should exist
And the wp-content/database/db-path.php file should not exist

Scenario: Only import initializes fresh default storage
When I run `wp sqlite export backup.sql`
And I run `rm -r wp-content/database`
And I try `wp sqlite export missing.sql`
Then the return code should be 1
And the missing.sql file should not exist
When I try `wp sqlite tables`
Then the return code should be 1
And the wp-content/database directory should not exist
When I run `mkdir wp-content/database`
And I run `wp sqlite import backup.sql`
Then STDOUT should contain:
"""
Success: Imported from 'backup.sql'.
"""
When I run `wp sqlite tables`
Then STDOUT should contain:
"""
wp_options
"""
When I run `wp eval 'echo get_option( "blogname" );'`
Then STDOUT should be:
"""
WP CLI Site
"""

Scenario: Use DB_PATH without changing existing storage
When I run `wp eval 'echo FQDB;'`
Then save STDOUT as {DATABASE_PATH}
When I run `mkdir custom-database`
And I run `cp "{DATABASE_PATH}" 'custom-database/custom;database.sqlite'`
And I run `wp config set DB_PATH "dirname( __FILE__ ) . '/custom-database/custom;database.sqlite'" --raw`
And I run `wp config set FQDB '{RUN_DIR}/unused.sqlite'`
And I run `wp sqlite export backup.sql`
And I run `wp sqlite import backup.sql`
And I run `wp sqlite tables`
Then STDOUT should contain:
"""
wp_options
"""
And the unused.sqlite file should not exist
And the custom-database/db-path.php file should not exist
And the custom-database/.htaccess file should not exist
And the custom-database/.ht.sqlite.lock file should not exist
When I run `wp sqlite export -`
Then STDOUT should contain:
"""
WP CLI Site
"""

Scenario: Only import creates a missing explicit database
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE test_table (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO test_table VALUES (1, 'Imported data');
"""
When I run `wp config set DB_PATH '{RUN_DIR}/new.sqlite'`
And I try `wp sqlite export backup.sql`
Then the return code should be 1
And STDERR should contain:
"""
The SQLite database does not exist.
"""
And the new.sqlite file should not exist
And the backup.sql file should not exist
When I try `wp sqlite tables`
Then the return code should be 1
And the new.sqlite file should not exist
When I run `wp sqlite import test_import.sql`
And I run `wp sqlite tables`
Then STDOUT should contain:
"""
test_table
"""
When I run `wp sqlite export -`
Then STDOUT should contain:
"""
Imported data
"""

Scenario: Use the legacy FQDB override
When I run `wp eval 'echo FQDB;'`
Then save STDOUT as {DB_PATH}
When I run `cp "{DB_PATH}" custom.sqlite`
And I run `wp config set FQDB '{RUN_DIR}/custom.sqlite'`
And I run `wp sqlite export backup.sql`
And I run `wp sqlite import backup.sql`
And I run `wp sqlite tables`
Then STDOUT should contain:
"""
wp_options
"""
When I run `wp eval 'echo FQDB;'`
Then STDOUT should be:
"""
{RUN_DIR}/custom.sqlite
"""
When I run `wp eval 'echo get_option( "blogname" );'`
Then STDOUT should be:
"""
WP CLI Site
"""

Scenario: Reject an invalid DB_PATH without falling back to existing storage
Given a SQL dump file named "test_import.sql" with content:
"""
CREATE TABLE test_table (id INTEGER PRIMARY KEY);
"""
When I run `wp config set DB_PATH null --raw`
And I try `wp sqlite import test_import.sql`
Then the return code should be 1
And STDERR should contain:
"""
The SQLite database path is invalid.
"""
When I try `wp sqlite export backup.sql`
Then the return code should be 1
And the backup.sql file should not exist
When I try `wp sqlite tables`
Then the return code should be 1
When I run `wp config delete DB_PATH`
And I run `wp sqlite tables`
Then STDOUT should not contain:
"""
test_table
"""
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<!-- For help understanding the `testVersion` configuration setting:
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="5.6-"/>
<config name="testVersion" value="7.4-"/>

<!-- Verify that everything in the global namespace is either namespaced or prefixed.
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
Expand Down
9 changes: 6 additions & 3 deletions phpstan/stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound
// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
// phpcs:disable PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations
// phpcs:disable PHPCompatibility.FunctionDeclarations.NewParamTypeDeclarations

class WP_SQLite_Connection {
/**
Expand All @@ -32,7 +30,12 @@ public function get_connection(): WP_SQLite_Connection {}
}

class WP_SQLite_Translator {
public function __construct() {}
public function __construct( ?PDO $pdo = null ) {}
public function query( string $sql ) {}
public function get_pdo(): PDO {}
}

class WP_SQLite_Storage {
public function __construct( ?string $database_root = null, ?string $database_path = null ) {}
public function initialize(): string {}
}
2 changes: 1 addition & 1 deletion src/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Import {

public function __construct() {
SQLiteDatabaseIntegrationLoader::load_plugin();
$this->driver = SQLiteDriverFactory::create_driver();
$this->driver = SQLiteDriverFactory::create_driver( true );
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/SQLiteDatabaseIntegrationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public static function load_plugin() {
require_once $plugin_directory . '/php-polyfills.php';
}
require_once $plugin_directory . '/constants.php';
$storage_file = $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-storage.php';
if ( file_exists( $storage_file ) ) {
require_once $storage_file;
}

$mysql_on_sqlite_class_file = $plugin_directory . '/wp-includes/database/sqlite/class-wp-mysql-on-sqlite.php';
$mysql_on_sqlite_loader_file = $plugin_directory . '/wp-includes/database/load.php';
Expand Down
Loading
Loading