From 89175c4ebc9404d6154c8a96017e0fe209623ff0 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Fri, 18 Jul 2025 11:54:59 +0200 Subject: [PATCH 1/2] fix(Builder) too early is_blog_installed run This changes the logic put in place in [this commit](https://github.com/stellarwp/schema/commit/fba6dc43e364b0212279ba13b6a2ee6fa1b09c75) to keep the spirit (avoid a db query on each run of the `Builder::up()` method, but avoid the call to `is_blog_installed` that, when fired too early in multisite installations, would kill the database. What is the issue? The `Builder::up` is called, by means of the `Builder::update_blog_tables` on `switch_blog`. The `switch_blog` function is called during the creation of blogs as well, in the `wp_initialize_site` function. The blog **is being installed**, it's not installed yet. Running `is_blog_installed()` in this phase will kill the site as it will find a broken db: the `users` table is there (it' common to all blogs), but the `options` table is not. This PR handles this scenario, delaying the creation of the tables to the `activate_blog` or `switch_blog` calls following the creation of the site or future requests. --- CHANGELOG.md | 4 ++ src/Schema/Builder.php | 20 +++++- tests/_data/wpunit/dump.sql | 130 +++++++++++++++++------------------ tests/wpunit/BuilderTest.php | 72 +++++++++++++++++++ 4 files changed, 160 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 200a617..ada6fa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. This projec ## [unreleased] Unreleased +## [1.1.10] 2025-07-18 + +* Fix - Avoid dead db in multisite installations with too early checks. + ## [1.1.9] 2025-02-26 * Tweak - Add @throws tags from the [stellarwp/db](https://github.com/stellarwp/db) library and better generics. diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 6bb7e1e..ec26ccc 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -263,7 +263,25 @@ public function register_custom_tables_names() { * @return array A list of each creation or update result. */ public function up( $force = false ) { - if ( ! is_blog_installed() || wp_installing() ) { + if ( doing_action( 'switch_blog' ) ) { + /* + * The `switch_blog` action can be called by the `wp_initialize_site` function, before the blog exists. + * Running `is_blog_installed()` in this case will kill the site with a dead db message since the + * `users` table will be found (it's common for all blogs), but the `options` table will not be found for + * the blog. + * If the blog does not exist, the value will not be cached yet and the value will be `false`. + * Else we can just use the value; this will be the case for a normal `switch_blog` action that does not + * fire while creating the site. + * + * When is the next chance to create the tables? Likely in the `activate_blog` action that will be fired + * in the same request following the site blog creation, or in the next `switch_blog` action. + */ + $is_blog_installed = wp_cache_get( 'is_blog_installed' ); + } else { + $is_blog_installed = is_blog_installed(); + } + + if ( ! $is_blog_installed || wp_installing() ) { return []; } diff --git a/tests/_data/wpunit/dump.sql b/tests/_data/wpunit/dump.sql index 6b43129..f0d636c 100644 --- a/tests/_data/wpunit/dump.sql +++ b/tests/_data/wpunit/dump.sql @@ -25,12 +25,12 @@ DROP TABLE IF EXISTS `wp_commentmeta`; 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) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, - `meta_value` longtext COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, + `meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `meta_value` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL, PRIMARY KEY (`meta_id`), KEY `comment_id` (`comment_id`), KEY `meta_key` (`meta_key`(191)) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -52,17 +52,17 @@ DROP TABLE IF EXISTS `wp_comments`; 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 COLLATE utf8mb4_unicode_520_ci NOT NULL, - `comment_author_email` varchar(100) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `comment_author_url` varchar(200) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `comment_author_IP` varchar(100) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', + `comment_author` tinytext COLLATE utf8mb4_unicode_ci NOT NULL, + `comment_author_email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `comment_author_url` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `comment_author_IP` varchar(100) COLLATE utf8mb4_unicode_ci 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 COLLATE utf8mb4_unicode_520_ci NOT NULL, + `comment_content` text COLLATE utf8mb4_unicode_ci NOT NULL, `comment_karma` int(11) NOT NULL DEFAULT 0, - `comment_approved` varchar(20) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '1', - `comment_agent` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `comment_type` varchar(20) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT 'comment', + `comment_approved` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '1', + `comment_agent` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `comment_type` varchar(20) COLLATE utf8mb4_unicode_ci 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`), @@ -71,7 +71,7 @@ CREATE TABLE `wp_comments` ( KEY `comment_date_gmt` (`comment_date_gmt`), KEY `comment_parent` (`comment_parent`), KEY `comment_author_email` (`comment_author_email`(10)) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -92,21 +92,21 @@ DROP TABLE IF EXISTS `wp_links`; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `wp_links` ( `link_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, - `link_url` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `link_name` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `link_image` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `link_target` varchar(25) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `link_description` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `link_visible` varchar(20) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT 'Y', + `link_url` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `link_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `link_image` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `link_target` varchar(25) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `link_description` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `link_visible` varchar(20) COLLATE utf8mb4_unicode_ci 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) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `link_notes` mediumtext COLLATE utf8mb4_unicode_520_ci NOT NULL, - `link_rss` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', + `link_rel` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `link_notes` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL, + `link_rss` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', PRIMARY KEY (`link_id`), KEY `link_visible` (`link_visible`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -127,13 +127,13 @@ DROP TABLE IF EXISTS `wp_options`; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `wp_options` ( `option_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, - `option_name` varchar(191) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `option_value` longtext COLLATE utf8mb4_unicode_520_ci NOT NULL, - `autoload` varchar(20) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT 'yes', + `option_name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `option_value` longtext COLLATE utf8mb4_unicode_ci NOT NULL, + `autoload` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'yes', PRIMARY KEY (`option_id`), UNIQUE KEY `option_name` (`option_name`), KEY `autoload` (`autoload`) -) ENGINE=InnoDB AUTO_INCREMENT=139 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; +) ENGINE=InnoDB AUTO_INCREMENT=139 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -156,12 +156,12 @@ DROP TABLE IF EXISTS `wp_postmeta`; 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) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, - `meta_value` longtext COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, + `meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `meta_value` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL, PRIMARY KEY (`meta_id`), KEY `post_id` (`post_id`), KEY `meta_key` (`meta_key`(191)) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -185,31 +185,31 @@ CREATE TABLE `wp_posts` ( `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 COLLATE utf8mb4_unicode_520_ci NOT NULL, - `post_title` text COLLATE utf8mb4_unicode_520_ci NOT NULL, - `post_excerpt` text COLLATE utf8mb4_unicode_520_ci NOT NULL, - `post_status` varchar(20) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT 'publish', - `comment_status` varchar(20) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT 'open', - `ping_status` varchar(20) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT 'open', - `post_password` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `post_name` varchar(200) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `to_ping` text COLLATE utf8mb4_unicode_520_ci NOT NULL, - `pinged` text COLLATE utf8mb4_unicode_520_ci NOT NULL, + `post_content` longtext COLLATE utf8mb4_unicode_ci NOT NULL, + `post_title` text COLLATE utf8mb4_unicode_ci NOT NULL, + `post_excerpt` text COLLATE utf8mb4_unicode_ci NOT NULL, + `post_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'publish', + `comment_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'open', + `ping_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'open', + `post_password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `post_name` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `to_ping` text COLLATE utf8mb4_unicode_ci NOT NULL, + `pinged` text COLLATE utf8mb4_unicode_ci 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 COLLATE utf8mb4_unicode_520_ci NOT NULL, + `post_content_filtered` longtext COLLATE utf8mb4_unicode_ci NOT NULL, `post_parent` bigint(20) unsigned NOT NULL DEFAULT 0, - `guid` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', + `guid` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', `menu_order` int(11) NOT NULL DEFAULT 0, - `post_type` varchar(20) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT 'post', - `post_mime_type` varchar(100) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', + `post_type` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'post', + `post_mime_type` varchar(100) COLLATE utf8mb4_unicode_ci 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`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -235,7 +235,7 @@ CREATE TABLE `wp_term_relationships` ( `term_order` int(11) NOT NULL DEFAULT 0, PRIMARY KEY (`object_id`,`term_taxonomy_id`), KEY `term_taxonomy_id` (`term_taxonomy_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -257,14 +257,14 @@ DROP TABLE IF EXISTS `wp_term_taxonomy`; 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) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `description` longtext COLLATE utf8mb4_unicode_520_ci NOT NULL, + `taxonomy` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `description` longtext COLLATE utf8mb4_unicode_ci 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`) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -287,12 +287,12 @@ DROP TABLE IF EXISTS `wp_termmeta`; 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) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, - `meta_value` longtext COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, + `meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `meta_value` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL, PRIMARY KEY (`meta_id`), KEY `term_id` (`term_id`), KEY `meta_key` (`meta_key`(191)) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -313,13 +313,13 @@ DROP TABLE IF EXISTS `wp_terms`; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `wp_terms` ( `term_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, - `name` varchar(200) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `slug` varchar(200) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', + `name` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `slug` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', `term_group` bigint(10) NOT NULL DEFAULT 0, PRIMARY KEY (`term_id`), KEY `slug` (`slug`(191)), KEY `name` (`name`(191)) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -342,12 +342,12 @@ DROP TABLE IF EXISTS `wp_usermeta`; 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) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, - `meta_value` longtext COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, + `meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `meta_value` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL, PRIMARY KEY (`umeta_id`), KEY `user_id` (`user_id`), KEY `meta_key` (`meta_key`(191)) -) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; +) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -369,20 +369,20 @@ DROP TABLE IF EXISTS `wp_users`; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `wp_users` ( `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, - `user_login` varchar(60) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `user_pass` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `user_nicename` varchar(50) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `user_email` varchar(100) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', - `user_url` varchar(100) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', + `user_login` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `user_pass` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `user_nicename` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `user_email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `user_url` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', `user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', - `user_activation_key` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', + `user_activation_key` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', `user_status` int(11) NOT NULL DEFAULT 0, - `display_name` varchar(250) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', + `display_name` varchar(250) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', PRIMARY KEY (`ID`), KEY `user_login_key` (`user_login`), KEY `user_nicename` (`user_nicename`), KEY `user_email` (`user_email`) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- diff --git a/tests/wpunit/BuilderTest.php b/tests/wpunit/BuilderTest.php index d38ed6d..8946369 100644 --- a/tests/wpunit/BuilderTest.php +++ b/tests/wpunit/BuilderTest.php @@ -365,4 +365,76 @@ public function update() { Register::remove_table( $klutz_table ); Register::remove_table( $zorps_table ); } + + /** + * @test + */ + public function should_not_create_tables_during_switch_blog_if_blog_not_installed(): void { + // Register the table. + $table = $this->get_simple_table(); + Register::table( $table ); + $table->drop(); + // Sanity check. + $this->assertFalse( $table->exists() ); + // Set up as if switching to a blog before it's installed, during its creeation. + wp_cache_delete( 'is_blog_installed' ); + // Remove all other filters to avoid side-effects. + remove_all_filters( 'switch_blog' ); + + $builder = Schema::builder(); + + add_action( 'switch_blog', [ $builder, 'update_blog_tables' ] ); + + do_action( 'switch_blog', 66 ); + + $this->assertFalse( $table->exists() ); + } + + /** + * @test + */ + public function should_create_tables_during_switch_blog_if_blog_installed(): void { + // Register the table. + $table = $this->get_simple_table(); + Register::table( $table ); + $table->drop(); + // Sanity check. + $this->assertFalse( $table->exists() ); + // Set up as if switching to a blog after it's installed. + wp_cache_set( 'is_blog_installed', true ); + // Remove all other filters to avoid side-effects. + remove_all_filters( 'switch_blog' ); + + $builder = Schema::builder(); + + add_action( 'switch_blog', [ $builder, 'update_blog_tables' ] ); + + do_action( 'switch_blog', 66 ); + + $this->assertTrue( $table->exists() ); + } + + /** + * @test + */ + public function should_create_tables_during_activate_blog(): void { + // Register the table. + $table = $this->get_simple_table(); + Register::table( $table ); + $table->drop(); + // Sanity check. + $this->assertFalse( $table->exists() ); + // Set up as if switching to a blog after it's installed. + wp_cache_set( 'is_blog_installed', true ); + // Remove all other filters to avoid side-effects. + remove_all_filters( 'activate_blog' ); + + $builder = Schema::builder(); + + add_action( 'activate_blog', [ $builder, 'update_blog_tables' ] ); + + do_action( 'activate_blog', 66 ); + + $this->assertTrue( $table->exists() ); + } } From 16255291226588090d5aada64245ce75d7669b8a Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Fri, 18 Jul 2025 13:05:59 +0200 Subject: [PATCH 2/2] doc(src) correct --- src/Schema/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index ec26ccc..9f33691 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -274,7 +274,7 @@ public function up( $force = false ) { * fire while creating the site. * * When is the next chance to create the tables? Likely in the `activate_blog` action that will be fired - * in the same request following the site blog creation, or in the next `switch_blog` action. + * in the same request following the blog creation or in the next `switch_blog` action. */ $is_blog_installed = wp_cache_get( 'is_blog_installed' ); } else {