Skip to content

Commit bf88b38

Browse files
authored
Merge pull request #40 from stellarwp/fix/multisite-table-up
Fix - multsite table up
2 parents 62e0150 + 84b4df9 commit bf88b38

2 files changed

Lines changed: 89 additions & 5 deletions

File tree

src/Schema/Builder.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,23 @@ public function register_custom_tables_names() {
263263
* @return array<mixed> A list of each creation or update result.
264264
*/
265265
public function up( $force = false ) {
266-
/*
267-
* The value will be `false` if the value has not been set yet or the blog is not installed.
268-
* In either case we should not try to create the tables.
269-
*/
270-
$is_blog_installed = wp_cache_get( 'is_blog_installed' );
266+
if ( doing_action( 'switch_blog' ) ) {
267+
/*
268+
* The `switch_blog` action can be called by the `wp_initialize_site` function, before the blog exists.
269+
* Running `is_blog_installed()` in this case will kill the site with a dead db message since the
270+
* `users` table will be found (it's common for all blogs), but the `options` table will not be found for
271+
* the blog.
272+
* If the blog does not exist, the value will not be cached yet and the value will be `false`.
273+
* Else we can just use the value; this will be the case for a normal `switch_blog` action that does not
274+
* fire while creating the site.
275+
*
276+
* When is the next chance to create the tables? Likely in the `activate_blog` action that will be fired
277+
* in the same request following the blog creation or in the next `switch_blog` action.
278+
*/
279+
$is_blog_installed = wp_cache_get( 'is_blog_installed' );
280+
} else {
281+
$is_blog_installed = is_blog_installed();
282+
}
271283

272284
if ( ! $is_blog_installed || wp_installing() ) {
273285
return [];

tests/wpunit/BuilderTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,76 @@ public function update() {
365365
Register::remove_table( $klutz_table );
366366
Register::remove_table( $zorps_table );
367367
}
368+
369+
/**
370+
* @test
371+
*/
372+
public function should_not_create_tables_during_switch_blog_if_blog_not_installed(): void {
373+
// Register the table.
374+
$table = $this->get_simple_table();
375+
Register::table( $table );
376+
$table->drop();
377+
// Sanity check.
378+
$this->assertFalse( $table->exists() );
379+
// Set up as if switching to a blog before it's installed, during its creeation.
380+
wp_cache_delete( 'is_blog_installed' );
381+
// Remove all other filters to avoid side-effects.
382+
remove_all_filters( 'switch_blog' );
383+
384+
$builder = Schema::builder();
385+
386+
add_action( 'switch_blog', [ $builder, 'update_blog_tables' ] );
387+
388+
do_action( 'switch_blog', 66 );
389+
390+
$this->assertFalse( $table->exists() );
391+
}
392+
393+
/**
394+
* @test
395+
*/
396+
public function should_create_tables_during_switch_blog_if_blog_installed(): void {
397+
// Register the table.
398+
$table = $this->get_simple_table();
399+
Register::table( $table );
400+
$table->drop();
401+
// Sanity check.
402+
$this->assertFalse( $table->exists() );
403+
// Set up as if switching to a blog after it's installed.
404+
wp_cache_set( 'is_blog_installed', true );
405+
// Remove all other filters to avoid side-effects.
406+
remove_all_filters( 'switch_blog' );
407+
408+
$builder = Schema::builder();
409+
410+
add_action( 'switch_blog', [ $builder, 'update_blog_tables' ] );
411+
412+
do_action( 'switch_blog', 66 );
413+
414+
$this->assertTrue( $table->exists() );
415+
}
416+
417+
/**
418+
* @test
419+
*/
420+
public function should_create_tables_during_activate_blog(): void {
421+
// Register the table.
422+
$table = $this->get_simple_table();
423+
Register::table( $table );
424+
$table->drop();
425+
// Sanity check.
426+
$this->assertFalse( $table->exists() );
427+
// Set up as if switching to a blog after it's installed.
428+
wp_cache_set( 'is_blog_installed', true );
429+
// Remove all other filters to avoid side-effects.
430+
remove_all_filters( 'activate_blog' );
431+
432+
$builder = Schema::builder();
433+
434+
add_action( 'activate_blog', [ $builder, 'update_blog_tables' ] );
435+
436+
do_action( 'activate_blog', 66 );
437+
438+
$this->assertTrue( $table->exists() );
439+
}
368440
}

0 commit comments

Comments
 (0)