Skip to content

Commit 4e58f1b

Browse files
committed
Fix fresh multisite database setup
Avoid querying the WordPress blogs table until it exists during information schema reconstruction. Cover empty, partial, and existing multisite databases.
1 parent 8063adb commit 4e58f1b

3 files changed

Lines changed: 76 additions & 10 deletions

File tree

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,25 @@ private function get_wp_create_table_statements(): array {
211211
if ( is_multisite() ) {
212212
/*
213213
* We need to use a database query over the "get_sites()" function,
214-
* as WPDB may not yet initialized. Moreover, we need to get the IDs
214+
* as WPDB may not yet be initialized. Moreover, we need to get the IDs
215215
* of all existing blogs, independent of any filters and actions that
216216
* could possibly alter the results of a "get_sites()" call.
217217
*/
218-
$blog_ids = $this->driver->execute_sqlite_query(
219-
sprintf(
220-
'SELECT blog_id FROM %s',
221-
$this->connection->quote_identifier( $wpdb->blogs )
222-
)
223-
)->fetchAll( PDO::FETCH_COLUMN );
218+
try {
219+
$blog_ids = $this->driver->execute_sqlite_query(
220+
sprintf(
221+
'SELECT blog_id FROM %s',
222+
$this->connection->quote_identifier( $wpdb->blogs )
223+
)
224+
)->fetchAll( PDO::FETCH_COLUMN );
225+
} catch ( PDOException $e ) {
226+
if ( ! str_contains( $e->getMessage(), 'no such table' ) ) {
227+
throw $e;
228+
}
229+
230+
// A fresh multisite database does not have a blogs table yet.
231+
$blog_ids = array();
232+
}
224233
foreach ( $blog_ids as $blog_id ) {
225234
$schema .= wp_get_db_schema( 'blog', (int) $blog_id );
226235
}

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

Lines changed: 55 additions & 2 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,6 +43,9 @@ function wp_get_db_schema() {
4143

4244
// Before each test, we create a new database
4345
public function setUp(): void {
46+
$GLOBALS['wp_sqlite_is_multisite'] = false;
47+
$GLOBALS['wp_sqlite_db_schema_calls'] = array();
48+
4449
$pdo_class = PHP_VERSION_ID >= 80400 ? Pdo\Sqlite::class : PDO::class;
4550
$this->sqlite = new $pdo_class( 'sqlite::memory:' );
4651
$this->engine = new WP_MySQL_On_SQLite(
@@ -60,6 +65,54 @@ public function setUp(): void {
6065
$this->engine,
6166
$builder
6267
);
68+
69+
$GLOBALS['wp_sqlite_db_schema_calls'] = array();
70+
}
71+
72+
public function testEmptyMultisiteDatabase(): void {
73+
$GLOBALS['wp_sqlite_is_multisite'] = true;
74+
75+
$this->reconstructor->ensure_correct_information_schema();
76+
77+
$result = $this->assertQuery( 'SELECT * FROM information_schema.tables' );
78+
$this->assertSame( array(), $result );
79+
}
80+
81+
public function testReconstructTableInMultisiteWithoutBlogsTable(): void {
82+
$GLOBALS['wp_sqlite_is_multisite'] = true;
83+
$this->engine->get_connection()->query( 'CREATE TABLE t ( id INTEGER )' );
84+
85+
$this->reconstructor->ensure_correct_information_schema();
86+
87+
$result = $this->assertQuery( 'SELECT table_name FROM information_schema.tables WHERE table_name = "t"' );
88+
$this->assertCount( 1, $result );
89+
$this->assertSame( array( array( 'global', null ) ), $GLOBALS['wp_sqlite_db_schema_calls'] );
90+
}
91+
92+
public function testReconstructTablesInExistingMultisite(): void {
93+
$GLOBALS['wp_sqlite_is_multisite'] = true;
94+
$this->engine->get_connection()->query( 'CREATE TABLE WPTESTS_BLOGS ( blog_id INTEGER )' );
95+
$this->engine->get_connection()->query( 'INSERT INTO WPTESTS_BLOGS ( blog_id ) VALUES ( 2 )' );
96+
97+
$this->reconstructor->ensure_correct_information_schema();
98+
99+
$this->assertSame(
100+
array(
101+
array( 'global', null ),
102+
array( 'blog', 2 ),
103+
),
104+
$GLOBALS['wp_sqlite_db_schema_calls']
105+
);
106+
}
107+
108+
public function testReconstructTablesInInvalidMultisite(): void {
109+
$GLOBALS['wp_sqlite_is_multisite'] = true;
110+
$this->engine->get_connection()->query( 'CREATE TABLE wptests_blogs ( id INTEGER )' );
111+
112+
$this->expectException( PDOException::class );
113+
$this->expectExceptionMessage( 'no such column: blog_id' );
114+
115+
$this->reconstructor->ensure_correct_information_schema();
63116
}
64117

65118
public function testReconstructTable(): void {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
// Polyfill WPDB globals.
2222
$GLOBALS['table_prefix'] = 'wptests_';
2323
$GLOBALS['wpdb'] = new class() {
24-
public function set_prefix( string $prefix ): void {}
24+
public $blogs;
25+
26+
public function set_prefix( string $prefix ): void {
27+
$this->blogs = $prefix . 'blogs';
28+
}
2529
};
2630

2731
/**

0 commit comments

Comments
 (0)