Skip to content

Commit 26cade0

Browse files
katinthehatsiteKateryna Kodonenko
andauthored
Load polyfills correctly for the new sqlite version (v2.2.22) (#24)
Co-authored-by: Kateryna Kodonenko <kateryna@automattic.com>
1 parent 2636429 commit 26cade0

3 files changed

Lines changed: 60 additions & 61 deletions

File tree

‎composer.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
"email": "jeroen.pfeil@automattic.com"
2424
}
2525
],
26+
"minimum-stability": "dev",
27+
"prefer-stable": true,
2628
"require": {
2729
"php": ">=7.4",
28-
"wp-cli/wp-cli": "^2.5"
30+
"wp-cli/wp-cli": "^2.13"
2931
},
3032
"require-dev": {
3133
"wp-cli/wp-cli-tests": "^5.0",

‎features/bootstrap/SQLiteFeatureContext.php‎

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
namespace Automattic\WP_CLI\SQLite;
44

55
use Behat\Behat\Context\Context;
6-
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
76
use Behat\Gherkin\Node\PyStringNode;
87
use WP_CLI\Tests\Context\FeatureContext as WPCLIFeatureContext;
9-
use SQLite3;
8+
use PDO;
109
use Exception;
1110

1211
class SQLiteFeatureContext extends WPCLIFeatureContext implements Context {
@@ -25,8 +24,9 @@ public function aSqlDumpFileNamedWithContent( $filename, PyStringNode $content )
2524
*/
2625
public function theSqliteDatabaseShouldContainATableNamed( $table_name ) {
2726
$this->connectToDatabase();
28-
$result = $this->db->query( "SELECT name FROM sqlite_master WHERE type='table' AND name='" . $this->db->escapeString( $table_name ) . "'" );
29-
$row = $result->fetchArray();
27+
$stmt = $this->db->prepare( "SELECT name FROM sqlite_master WHERE type='table' AND name=?" );
28+
$stmt->execute( [ $table_name ] );
29+
$row = $stmt->fetch();
3030
if ( ! $row ) {
3131
throw new Exception( "Table '$table_name' not found in the database." );
3232
}
@@ -38,8 +38,9 @@ public function theSqliteDatabaseShouldContainATableNamed( $table_name ) {
3838
*/
3939
public function theTableShouldContainARowWithName( $table_name, $name ) {
4040
$this->connectToDatabase();
41-
$result = $this->db->query( "SELECT * FROM $table_name WHERE name='" . $this->db->escapeString( $name ) . "'" );
42-
$row = $result->fetchArray();
41+
$stmt = $this->db->prepare( "SELECT * FROM $table_name WHERE name=?" );
42+
$stmt->execute( [ $name ] );
43+
$row = $stmt->fetch();
4344
if ( ! $row ) {
4445
throw new Exception( "Row with name '$name' not found in table '$table_name'." );
4546
}
@@ -51,14 +52,7 @@ public function theTableShouldContainARowWithName( $table_name, $name ) {
5152
public function theSqliteDatabaseShouldContainTheImportedData() {
5253
$this->connectToDatabase();
5354
$result = $this->db->query( "SELECT name FROM sqlite_master WHERE type='table'" );
54-
$tables = [];
55-
while ( true ) {
56-
$row = $result->fetchArray();
57-
if ( false === $row ) {
58-
break;
59-
}
60-
$tables[] = $row['name'];
61-
}
55+
$tables = $result->fetchAll( PDO::FETCH_COLUMN );
6256
if ( empty( $tables ) ) {
6357
throw new Exception( 'No tables found in the database after import.' );
6458
}
@@ -78,7 +72,8 @@ private function connectToDatabase() {
7872
if ( ! $this->db ) {
7973
$run_dir = $this->variables['RUN_DIR'];
8074
$db_file = $run_dir . '/wp-content/database/.ht.sqlite';
81-
$this->db = new SQLite3( $db_file );
75+
$this->db = new PDO( 'sqlite:' . $db_file );
76+
$this->db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
8277
}
8378
}
8479

@@ -105,45 +100,43 @@ protected function create_file( $filename, $content ) {
105100
file_put_contents( $full_path, $content );
106101
}
107102

108-
109-
////
110103
/**
111104
* @Given /^the SQLite database contains some sample data$/
112105
*/
113106
public function theSqliteDatabaseContainsSomeSampleData() {
114-
$this->connectToDatabase();
115-
$this->db->exec(
116-
"
117-
INSERT OR REPLACE INTO wp_posts (ID, post_title, post_content, post_type, post_status)
118-
VALUES (1, 'Sample Post', 'This is a sample post content.', 'post', 'publish');
119-
"
120-
);
121-
122-
// Insert or update sample data in wp_users
123-
$this->db->exec(
124-
"
125-
INSERT OR REPLACE INTO wp_users (ID, user_login, user_pass, user_nicename, user_email)
126-
VALUES (1, 'testuser', 'password_hash', 'Test User', 'testuser@example.com');
127-
"
128-
);
129-
130-
// Insert or update sample data in wp_options
131-
$this->db->exec(
132-
"
133-
INSERT OR REPLACE INTO wp_options (option_id, option_name, option_value, autoload)
134-
VALUES
135-
(1, 'siteurl', 'http://example.com', 'yes'),
136-
(2, 'blogname', 'Test Blog', 'yes'),
137-
(3, 'blogdescription', 'Just another WordPress site', 'yes'),
138-
(4, 'users_can_register', '0', 'yes'),
139-
(5, 'admin_email', 'admin@example.com', 'yes'),
140-
(6, 'start_of_week', '1', 'yes'),
141-
(7, 'use_balanceTags', '0', 'yes'),
142-
(8, 'use_smilies', '1', 'yes'),
143-
(9, 'require_name_email', '1', 'yes'),
144-
(10, 'comments_notify', '1', 'yes');
145-
"
146-
);
107+
$this->connectToDatabase();
108+
$this->db->exec(
109+
"
110+
INSERT OR REPLACE INTO wp_posts (ID, post_title, post_content, post_type, post_status)
111+
VALUES (1, 'Sample Post', 'This is a sample post content.', 'post', 'publish');
112+
"
113+
);
114+
115+
// Insert or update sample data in wp_users
116+
$this->db->exec(
117+
"
118+
INSERT OR REPLACE INTO wp_users (ID, user_login, user_pass, user_nicename, user_email)
119+
VALUES (1, 'testuser', 'password_hash', 'Test User', 'testuser@example.com');
120+
"
121+
);
122+
123+
// Insert or update sample data in wp_options
124+
$this->db->exec(
125+
"
126+
INSERT OR REPLACE INTO wp_options (option_id, option_name, option_value, autoload)
127+
VALUES
128+
(1, 'siteurl', 'http://example.com', 'yes'),
129+
(2, 'blogname', 'Test Blog', 'yes'),
130+
(3, 'blogdescription', 'Just another WordPress site', 'yes'),
131+
(4, 'users_can_register', '0', 'yes'),
132+
(5, 'admin_email', 'admin@example.com', 'yes'),
133+
(6, 'start_of_week', '1', 'yes'),
134+
(7, 'use_balanceTags', '0', 'yes'),
135+
(8, 'use_smilies', '1', 'yes'),
136+
(9, 'require_name_email', '1', 'yes'),
137+
(10, 'comments_notify', '1', 'yes');
138+
"
139+
);
147140
}
148141

149142
/**

‎src/SQLiteDatabaseIntegrationLoader.php‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,23 @@ public static function load_plugin() {
7676
if ( version_compare( $sqlite_plugin_version, '2.1.11', '<' ) ) {
7777
WP_CLI::error( 'The SQLite integration plugin must be version 2.1.11 or higher.' );
7878
}
79-
8079
// Load the translator class from the plugin.
8180
if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) {
8281
define( 'SQLITE_DB_DROPIN_VERSION', $sqlite_plugin_version ); // phpcs:ignore
8382
}
8483

85-
// We also need to selectively load the necessary classes from the plugin.
86-
require_once $plugin_directory . '/php-polyfills.php';
87-
require_once $plugin_directory . '/constants.php';
88-
8984
$new_driver_enabled = defined( 'WP_SQLITE_AST_DRIVER' ) && WP_SQLITE_AST_DRIVER;
85+
$old_structure = file_exists( $plugin_directory . '/php-polyfills.php' );
86+
87+
if ( $old_structure ) {
88+
require_once $plugin_directory . '/php-polyfills.php';
89+
}
90+
require_once $plugin_directory . '/constants.php';
9091

9192
if ( $new_driver_enabled && file_exists( $plugin_directory . '/wp-pdo-mysql-on-sqlite.php' ) ) {
9293
require_once $plugin_directory . '/wp-pdo-mysql-on-sqlite.php';
94+
} elseif ( $new_driver_enabled && file_exists( $plugin_directory . '/wp-includes/database/load.php' ) ) {
95+
require_once $plugin_directory . '/wp-includes/database/load.php';
9396
} elseif ( $new_driver_enabled ) {
9497
require_once $plugin_directory . '/version.php';
9598
require_once $plugin_directory . '/wp-includes/parser/class-wp-parser-grammar.php';
@@ -108,11 +111,12 @@ public static function load_plugin() {
108111
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-exception.php';
109112
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-reconstructor.php';
110113
} else {
111-
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-lexer.php';
112-
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-query-rewriter.php';
113-
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-translator.php';
114-
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-token.php';
115-
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
114+
$sqlite = $plugin_directory . '/wp-includes/sqlite';
115+
require_once "$sqlite/class-wp-sqlite-lexer.php";
116+
require_once "$sqlite/class-wp-sqlite-query-rewriter.php";
117+
require_once "$sqlite/class-wp-sqlite-translator.php";
118+
require_once "$sqlite/class-wp-sqlite-token.php";
119+
require_once "$sqlite/class-wp-sqlite-pdo-user-defined-functions.php";
116120
}
117121
}
118122
}

0 commit comments

Comments
 (0)