@@ -145,20 +145,33 @@ private function get_wp_create_table_statements(): array {
145145 throw new Exception ( 'The "wp_get_db_schema()" function was not defined. ' );
146146 }
147147
148- // Get schema for global tables and the main site.
149- $ schema = wp_get_db_schema ();
148+ /*
149+ * At this point, WPDB may not yet be initialized, as we're configuring
150+ * the database connection. Let's only populate the table names using
151+ * the "$table_prefix" global so we can get correct table names.
152+ */
153+ global $ wpdb , $ table_prefix ;
154+ $ wpdb ->set_prefix ( $ table_prefix );
155+
156+ // Get schema for global tables.
157+ $ schema = wp_get_db_schema ( 'global ' );
150158
151159 // For multisite installs, add schema definitions for all sites.
152160 if ( is_multisite () ) {
153- $ site_ids = get_sites (
154- array (
155- 'fields ' => 'ids ' ,
156- 'number ' => PHP_INT_MAX ,
157- )
158- );
159- foreach ( $ site_ids as $ site_id ) {
160- $ schema .= wp_get_db_schema ( 'blog ' , $ site_id );
161+ /*
162+ * We need to use a database query over the "get_sites()" function,
163+ * as WPDB may not yet initialized. Moreover, we need to get the IDs
164+ * of all existing blogs, independent of any filters and actions that
165+ * could possibly alter the results of a "get_sites()" call.
166+ */
167+ $ stmt = $ this ->driver ->execute_sqlite_query ( "SELECT blog_id FROM {$ wpdb ->blogs }" );
168+ $ blog_ids = $ stmt ->fetchAll ( PDO ::FETCH_COLUMN );
169+ foreach ( $ blog_ids as $ blog_id ) {
170+ $ schema .= wp_get_db_schema ( 'blog ' , (int ) $ blog_id );
161171 }
172+ } else {
173+ // For single site installs, add schema for the main site.
174+ $ schema .= wp_get_db_schema ( 'blog ' );
162175 }
163176
164177 // Parse the schema.
0 commit comments