Skip to content
This repository was archived by the owner on Sep 11, 2026. It is now read-only.

Commit bd6ebfa

Browse files
authored
Merge upstream trunk into d1-support
Automated upstream sync (#8).
2 parents 2fd5830 + 9a69dc4 commit bd6ebfa

6 files changed

Lines changed: 190 additions & 57 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-reconstructor.php‎

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -64,40 +64,41 @@ public function ensure_correct_information_schema(): void {
6464
$sqlite_tables = $this->get_sqlite_table_names();
6565
$information_schema_tables = $this->get_information_schema_table_names();
6666

67+
$tables_missing_from_information_schema = array_diff( $sqlite_tables, $information_schema_tables );
68+
$tables_missing_from_sqlite = array_diff( $information_schema_tables, $sqlite_tables );
69+
6770
// In WordPress, use "wp_get_db_schema()" to reconstruct WordPress tables.
68-
$wp_tables = $this->get_wp_create_table_statements();
71+
$wp_tables = count( $tables_missing_from_information_schema ) > 0
72+
? $this->get_wp_create_table_statements()
73+
: array();
6974

7075
// Reconstruct information schema records for tables that don't have them.
71-
foreach ( $sqlite_tables as $table ) {
72-
if ( ! in_array( $table, $information_schema_tables, true ) ) {
73-
if ( isset( $wp_tables[ $table ] ) ) {
74-
// WordPress core table (as returned by "wp_get_db_schema()").
75-
$ast = $wp_tables[ $table ];
76-
} else {
77-
// Other table (a WordPress plugin or unrelated to WordPress).
78-
$sql = $this->generate_create_table_statement( $table );
79-
$ast = $this->driver->create_parser( $sql )->parse();
80-
if ( null === $ast ) {
81-
throw new WP_MySQL_On_SQLite_Exception( $this->driver, 'Failed to parse the MySQL query.' );
82-
}
76+
foreach ( $tables_missing_from_information_schema as $table ) {
77+
if ( isset( $wp_tables[ $table ] ) ) {
78+
// WordPress core table (as returned by "wp_get_db_schema()").
79+
$ast = $wp_tables[ $table ];
80+
} else {
81+
// Other table (a WordPress plugin or unrelated to WordPress).
82+
$sql = $this->generate_create_table_statement( $table );
83+
$ast = $this->driver->create_parser( $sql )->parse();
84+
if ( null === $ast ) {
85+
throw new WP_MySQL_On_SQLite_Exception( $this->driver, 'Failed to parse the MySQL query.' );
8386
}
87+
}
8488

85-
/*
86-
* First, let's make sure we clean up all related data. This fixes
87-
* partial data corruption, such as when a table record is missing,
88-
* but some related column, index, or constraint records are stored.
89-
*/
90-
$this->record_drop_table( $table );
89+
/*
90+
* First, let's make sure we clean up all related data. This fixes
91+
* partial data corruption, such as when a table record is missing,
92+
* but some related column, index, or constraint records are stored.
93+
*/
94+
$this->record_drop_table( $table );
9195

92-
$this->schema_builder->record_create_table( $ast );
93-
}
96+
$this->schema_builder->record_create_table( $ast );
9497
}
9598

9699
// Remove information schema records for tables that don't exist.
97-
foreach ( $information_schema_tables as $table ) {
98-
if ( ! in_array( $table, $sqlite_tables, true ) ) {
99-
$this->record_drop_table( $table );
100-
}
100+
foreach ( $tables_missing_from_sqlite as $table ) {
101+
$this->record_drop_table( $table );
101102
}
102103
}
103104

@@ -205,31 +206,46 @@ private function get_wp_create_table_statements(): array {
205206
* the "$table_prefix" global so we can get correct table names.
206207
*/
207208
global $table_prefix;
208-
$wpdb->set_prefix( $table_prefix );
209+
$set_prefix_result = $wpdb->set_prefix( $table_prefix );
210+
if ( $set_prefix_result instanceof WP_Error ) {
211+
throw new Exception( $set_prefix_result->get_error_message() );
212+
}
209213

210214
// Get schema for global tables.
211215
$schema = wp_get_db_schema( 'global' );
212216

213-
// For multisite installs, add schema definitions for all sites.
217+
// For multisite installs, get all blog IDs.
218+
$blog_ids = array();
214219
if ( is_multisite() ) {
215220
/*
216221
* We need to use a database query over the "get_sites()" function,
217-
* as WPDB may not yet initialized. Moreover, we need to get the IDs
222+
* as WPDB may not yet be initialized. Moreover, we need to get the IDs
218223
* of all existing blogs, independent of any filters and actions that
219224
* could possibly alter the results of a "get_sites()" call.
220225
*/
221-
$blog_ids = $this->driver->execute_sqlite_query(
222-
sprintf(
223-
'SELECT blog_id FROM %s',
224-
$this->connection->quote_identifier( $wpdb->blogs )
225-
)
226-
)->fetchAll( PDO::FETCH_COLUMN );
226+
try {
227+
$blog_ids = $this->driver->execute_sqlite_query(
228+
sprintf(
229+
'SELECT blog_id FROM %s',
230+
$this->connection->quote_identifier( $wpdb->blogs )
231+
)
232+
)->fetchAll( PDO::FETCH_COLUMN );
233+
} catch ( PDOException $e ) {
234+
if ( ! str_contains( $e->getMessage(), 'no such table' ) ) {
235+
throw $e;
236+
}
237+
}
238+
}
239+
240+
// Get schema for blog tables.
241+
if ( 0 === count( $blog_ids ) ) {
242+
// Single site or no blog IDs: Add schema for the main site.
243+
$schema .= wp_get_db_schema( 'blog' );
244+
} else {
245+
// Multisite: Add schema definitions for all sites.
227246
foreach ( $blog_ids as $blog_id ) {
228247
$schema .= wp_get_db_schema( 'blog', (int) $blog_id );
229248
}
230-
} else {
231-
// For single site installs, add schema for the main site.
232-
$schema .= wp_get_db_schema( 'blog' );
233249
}
234250

235251
// Parse the schema.

‎packages/mysql-on-sqlite/tests/WP_SQLite_Information_Schema_Reconstructor_Tests.php‎

Lines changed: 109 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ public static function setUpBeforeClass(): void {
2727
}
2828
if ( ! function_exists( 'is_multisite' ) ) {
2929
function is_multisite() {
30-
return false;
30+
return $GLOBALS['wp_sqlite_is_multisite'];
3131
}
3232
}
3333
if ( ! function_exists( 'wp_get_db_schema' ) ) {
34-
function wp_get_db_schema() {
34+
function wp_get_db_schema( $scope = 'all', $blog_id = null ) {
35+
$GLOBALS['wp_sqlite_db_schema_calls'][] = array( $scope, $blog_id );
36+
3537
// Output from "wp_get_db_schema" as of WordPress 6.8.0.
3638
// See: https://github.com/WordPress/wordpress-develop/blob/6.8.0/src/wp-admin/includes/schema.php#L36
3739
return "CREATE TABLE wp_users ( ID bigint(20) unsigned NOT NULL auto_increment, user_login varchar(60) NOT NULL default '', user_pass varchar(255) NOT NULL default '', user_nicename varchar(50) NOT NULL default '', user_email varchar(100) NOT NULL default '', user_url varchar(100) NOT NULL default '', user_registered datetime NOT NULL default '0000-00-00 00:00:00', user_activation_key varchar(255) NOT NULL default '', user_status int(11) NOT NULL default '0', display_name varchar(250) NOT NULL default '', PRIMARY KEY (ID), KEY user_login_key (user_login), KEY user_nicename (user_nicename), KEY user_email (user_email) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_usermeta ( umeta_id bigint(20) unsigned NOT NULL auto_increment, user_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (umeta_id), KEY user_id (user_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_termmeta ( meta_id bigint(20) unsigned NOT NULL auto_increment, term_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (meta_id), KEY term_id (term_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_terms ( term_id bigint(20) unsigned NOT NULL auto_increment, name varchar(200) NOT NULL default '', slug varchar(200) NOT NULL default '', term_group bigint(10) NOT NULL default 0, PRIMARY KEY (term_id), KEY slug (slug(191)), KEY name (name(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_term_taxonomy ( term_taxonomy_id bigint(20) unsigned NOT NULL auto_increment, term_id bigint(20) unsigned NOT NULL default 0, taxonomy varchar(32) NOT NULL default '', description longtext NOT NULL, parent bigint(20) unsigned NOT NULL default 0, count bigint(20) NOT NULL default 0, PRIMARY KEY (term_taxonomy_id), UNIQUE KEY term_id_taxonomy (term_id,taxonomy), KEY taxonomy (taxonomy) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_term_relationships ( object_id bigint(20) unsigned NOT NULL default 0, term_taxonomy_id bigint(20) unsigned NOT NULL default 0, term_order int(11) NOT NULL default 0, PRIMARY KEY (object_id,term_taxonomy_id), KEY term_taxonomy_id (term_taxonomy_id) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_commentmeta ( meta_id bigint(20) unsigned NOT NULL auto_increment, comment_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (meta_id), KEY comment_id (comment_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_comments ( comment_ID bigint(20) unsigned NOT NULL auto_increment, comment_post_ID bigint(20) unsigned NOT NULL default '0', comment_author tinytext NOT NULL, comment_author_email varchar(100) NOT NULL default '', comment_author_url varchar(200) NOT NULL default '', comment_author_IP varchar(100) NOT NULL default '', comment_date datetime NOT NULL default '0000-00-00 00:00:00', comment_date_gmt datetime NOT NULL default '0000-00-00 00:00:00', comment_content text NOT NULL, comment_karma int(11) NOT NULL default '0', comment_approved varchar(20) NOT NULL default '1', comment_agent varchar(255) NOT NULL default '', comment_type varchar(20) NOT NULL default 'comment', comment_parent bigint(20) unsigned NOT NULL default '0', user_id bigint(20) unsigned NOT NULL default '0', PRIMARY KEY (comment_ID), KEY comment_post_ID (comment_post_ID), KEY comment_approved_date_gmt (comment_approved,comment_date_gmt), KEY comment_date_gmt (comment_date_gmt), KEY comment_parent (comment_parent), KEY comment_author_email (comment_author_email(10)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_links ( link_id bigint(20) unsigned NOT NULL auto_increment, link_url varchar(255) NOT NULL default '', link_name varchar(255) NOT NULL default '', link_image varchar(255) NOT NULL default '', link_target varchar(25) NOT NULL default '', link_description varchar(255) NOT NULL default '', link_visible varchar(20) NOT NULL default 'Y', link_owner bigint(20) unsigned NOT NULL default '1', link_rating int(11) NOT NULL default '0', link_updated datetime NOT NULL default '0000-00-00 00:00:00', link_rel varchar(255) NOT NULL default '', link_notes mediumtext NOT NULL, link_rss varchar(255) NOT NULL default '', PRIMARY KEY (link_id), KEY link_visible (link_visible) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_options ( option_id bigint(20) unsigned NOT NULL auto_increment, option_name varchar(191) NOT NULL default '', option_value longtext NOT NULL, autoload varchar(20) NOT NULL default 'yes', PRIMARY KEY (option_id), UNIQUE KEY option_name (option_name), KEY autoload (autoload) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_postmeta ( meta_id bigint(20) unsigned NOT NULL auto_increment, post_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (meta_id), KEY post_id (post_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_posts ( ID bigint(20) unsigned NOT NULL auto_increment, post_author bigint(20) unsigned NOT NULL default '0', post_date datetime NOT NULL default '0000-00-00 00:00:00', post_date_gmt datetime NOT NULL default '0000-00-00 00:00:00', post_content longtext NOT NULL, post_title text NOT NULL, post_excerpt text NOT NULL, post_status varchar(20) NOT NULL default 'publish', comment_status varchar(20) NOT NULL default 'open', ping_status varchar(20) NOT NULL default 'open', post_password varchar(255) NOT NULL default '', post_name varchar(200) NOT NULL default '', to_ping text NOT NULL, pinged text NOT NULL, post_modified datetime NOT NULL default '0000-00-00 00:00:00', post_modified_gmt datetime NOT NULL default '0000-00-00 00:00:00', post_content_filtered longtext NOT NULL, post_parent bigint(20) unsigned NOT NULL default '0', guid varchar(255) NOT NULL default '', menu_order int(11) NOT NULL default '0', post_type varchar(20) NOT NULL default 'post', post_mime_type varchar(100) NOT NULL default '', comment_count bigint(20) NOT NULL default '0', PRIMARY KEY (ID), KEY post_name (post_name(191)), KEY type_status_date (post_type,post_status,post_date,ID), KEY post_parent (post_parent), KEY post_author (post_author) ) DEFAULT CHARACTER SET utf8mb4;";
@@ -41,27 +43,95 @@ function wp_get_db_schema() {
4143

4244
// Before each test, we create a new database
4345
public function setUp(): void {
44-
$pdo_class = PHP_VERSION_ID >= 80400 ? Pdo\Sqlite::class : PDO::class;
45-
$this->sqlite = new $pdo_class( 'sqlite::memory:' );
46-
$this->engine = new WP_MySQL_On_SQLite(
47-
'mysql-on-sqlite:dbname=wp',
48-
null,
49-
null,
50-
array( 'sqlite_pdo' => $this->sqlite )
51-
);
52-
$this->engine->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
46+
$GLOBALS['wp_sqlite_is_multisite'] = false;
47+
$GLOBALS['table_prefix'] = 'wptests_';
5348

54-
$builder = new WP_SQLite_Information_Schema_Builder(
55-
WP_MySQL_On_SQLite::RESERVED_PREFIX,
56-
$this->engine->get_connection()
49+
$this->initializeDatabase();
50+
51+
$GLOBALS['wp_sqlite_db_schema_calls'] = array();
52+
}
53+
54+
public function tearDown(): void {
55+
$GLOBALS['wp_sqlite_is_multisite'] = false;
56+
$GLOBALS['wp_sqlite_db_schema_calls'] = array();
57+
$GLOBALS['table_prefix'] = 'wptests_';
58+
$GLOBALS['wpdb']->blogs = 'wptests_blogs';
59+
}
60+
61+
public function testInvalidWpTablePrefix(): void {
62+
$GLOBALS['wp_sqlite_is_multisite'] = true;
63+
$GLOBALS['table_prefix'] = 'invalid-prefix';
64+
$GLOBALS['wpdb']->blogs = null;
65+
$this->engine->get_connection()->query( 'CREATE TABLE t ( id INTEGER )' );
66+
67+
$this->expectException( Exception::class );
68+
$this->expectExceptionMessage( 'Invalid database prefix' );
69+
70+
$this->reconstructor->ensure_correct_information_schema();
71+
}
72+
73+
public function testEmptyMultisiteDatabase(): void {
74+
$GLOBALS['wp_sqlite_is_multisite'] = true;
75+
76+
$this->initializeDatabase();
77+
78+
$result = $this->assertQuery( 'SELECT * FROM information_schema.tables' );
79+
$this->assertSame( array(), $result );
80+
$this->assertSame( array(), $GLOBALS['wp_sqlite_db_schema_calls'] );
81+
}
82+
83+
public function testSkipWpSchemaWhenNoTablesAreMissing(): void {
84+
$this->engine->query( 'CREATE TABLE t ( id INTEGER )' );
85+
$GLOBALS['wp_sqlite_is_multisite'] = true;
86+
87+
$this->reconstructor->ensure_correct_information_schema();
88+
89+
$this->assertSame( array(), $GLOBALS['wp_sqlite_db_schema_calls'] );
90+
}
91+
92+
public function testReconstructTableInMultisiteWithoutBlogsTable(): void {
93+
$GLOBALS['wp_sqlite_is_multisite'] = true;
94+
$this->engine->get_connection()->query( 'CREATE TABLE t ( id INTEGER )' );
95+
96+
$this->reconstructor->ensure_correct_information_schema();
97+
98+
$result = $this->assertQuery( 'SELECT table_name FROM information_schema.tables WHERE table_name = "t"' );
99+
$this->assertCount( 1, $result );
100+
$this->assertSame(
101+
array(
102+
array( 'global', null ),
103+
array( 'blog', null ),
104+
),
105+
$GLOBALS['wp_sqlite_db_schema_calls']
57106
);
107+
}
58108

59-
$this->reconstructor = new WP_SQLite_Information_Schema_Reconstructor(
60-
$this->engine,
61-
$builder
109+
public function testReconstructTablesInExistingMultisite(): void {
110+
$GLOBALS['wp_sqlite_is_multisite'] = true;
111+
$this->engine->get_connection()->query( 'CREATE TABLE wptests_blogs ( blog_id INTEGER )' );
112+
$this->engine->get_connection()->query( 'INSERT INTO wptests_blogs ( blog_id ) VALUES ( 2 )' );
113+
114+
$this->reconstructor->ensure_correct_information_schema();
115+
116+
$this->assertSame(
117+
array(
118+
array( 'global', null ),
119+
array( 'blog', 2 ),
120+
),
121+
$GLOBALS['wp_sqlite_db_schema_calls']
62122
);
63123
}
64124

125+
public function testReconstructTablesInInvalidMultisite(): void {
126+
$GLOBALS['wp_sqlite_is_multisite'] = true;
127+
$this->engine->get_connection()->query( 'CREATE TABLE wptests_blogs ( id INTEGER )' );
128+
129+
$this->expectException( PDOException::class );
130+
$this->expectExceptionMessage( 'no such column: blog_id' );
131+
132+
$this->reconstructor->ensure_correct_information_schema();
133+
}
134+
65135
public function testReconstructTable(): void {
66136
$this->engine->get_connection()->query(
67137
'
@@ -464,4 +534,26 @@ private function assertQuery( $sql ) {
464534
$this->assertNotFalse( $retval );
465535
return $retval;
466536
}
537+
538+
private function initializeDatabase(): void {
539+
$pdo_class = PHP_VERSION_ID >= 80400 ? Pdo\Sqlite::class : PDO::class;
540+
$this->sqlite = new $pdo_class( 'sqlite::memory:' );
541+
$this->engine = new WP_MySQL_On_SQLite(
542+
'mysql-on-sqlite:dbname=wp',
543+
null,
544+
null,
545+
array( 'sqlite_pdo' => $this->sqlite )
546+
);
547+
$this->engine->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
548+
549+
$builder = new WP_SQLite_Information_Schema_Builder(
550+
WP_MySQL_On_SQLite::RESERVED_PREFIX,
551+
$this->engine->get_connection()
552+
);
553+
554+
$this->reconstructor = new WP_SQLite_Information_Schema_Reconstructor(
555+
$this->engine,
556+
$builder
557+
);
558+
}
467559
}

‎packages/mysql-on-sqlite/tests/bootstrap.php‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,32 @@
2222
// Polyfill WPDB globals.
2323
$GLOBALS['table_prefix'] = 'wptests_';
2424
$GLOBALS['wpdb'] = new class() {
25-
public function set_prefix( string $prefix ): void {}
25+
public $blogs;
26+
27+
public function set_prefix( string $prefix ) {
28+
if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) {
29+
return new WP_Error( 'invalid_db_prefix', 'Invalid database prefix' );
30+
}
31+
32+
$this->blogs = $prefix . 'blogs';
33+
return $prefix;
34+
}
2635
};
2736

37+
if ( ! class_exists( 'WP_Error' ) ) {
38+
class WP_Error {
39+
private $message;
40+
41+
public function __construct( $code, $message ) {
42+
$this->message = $message;
43+
}
44+
45+
public function get_error_message() {
46+
return $this->message;
47+
}
48+
}
49+
}
50+
2851
/**
2952
* Polyfills for WordPress functions
3053
*/

‎packages/plugin-sqlite-database-integration/load.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Version: 3.0.0
77
* Requires PHP: 7.2
88
* Network: true
9+
* License: GPL-2.0-or-later
10+
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
911
* Textdomain: sqlite-database-integration
1012
*
1113
* This feature plugin allows WordPress to use SQLite instead of MySQL as its database.

‎packages/plugin-sqlite-database-integration/readme.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Contributors: wordpressdotorg, aristath, janjakes, zieladam, berislav.grgicak, bpayton, zaerl
44
Requires at least: 6.4
5-
Tested up to: 7.0
5+
Tested up to: 7.1
66
Requires PHP: 7.2
77
Stable tag: 3.0.0
88
License: GPLv2 or later

‎wp-setup.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
set -e
1010

11-
WP_VERSION="7.0.1"
11+
WP_VERSION="7.1"
1212

1313
DIR="$(dirname "$0")"
1414
WP_DIR="$DIR/wordpress"

0 commit comments

Comments
 (0)