From 535656461fc211024cb41caa946f20c6250dedd8 Mon Sep 17 00:00:00 2001 From: Rebecca Hum Date: Thu, 12 Nov 2026 13:47:29 -0600 Subject: [PATCH 1/3] fix(events-store): scope table install check to the current database is_installed() counted information_schema rows for the table name across every schema the DB user can see, and required exactly one. A same-named table in another database on the same server made the count 2, so the plugin never considered itself installed and re-ran the install on every request instead of handling cron. Co-Authored-By: Claude Opus 5.5 --- __tests__/unit-tests/test-events-store.php | 28 ++++++++++++++++++++++ includes/class-events-store.php | 19 +++++++++++---- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/__tests__/unit-tests/test-events-store.php b/__tests__/unit-tests/test-events-store.php index 49f3fec2..8ea7ee43 100644 --- a/__tests__/unit-tests/test-events-store.php +++ b/__tests__/unit-tests/test-events-store.php @@ -25,6 +25,34 @@ public function test_table_exists() { $this->assertTrue( Events_Store::is_installed() ); } + public function test_is_installed_ignores_same_named_table_in_other_database() { + global $wpdb; + + // Use real tables, since temporary tables don't show up in information_schema. + remove_filter( 'query', array( $this, '_create_temporary_tables' ) ); + remove_filter( 'query', array( $this, '_drop_temporary_tables' ) ); + + $other_db = 'cron_control_tests_other'; + $table_name = Utils::get_table_name(); + + $suppress = $wpdb->suppress_errors(); + $created = $wpdb->query( $wpdb->prepare( 'CREATE DATABASE IF NOT EXISTS %i', $other_db ) ); + $wpdb->suppress_errors( $suppress ); + if ( false === $created ) { + $this->markTestSkipped( 'The test DB user cannot create databases.' ); + } + + $wpdb->query( $wpdb->prepare( 'CREATE TABLE %i.%i (`ID` bigint(20) unsigned NOT NULL)', $other_db, $table_name ) ); + + wp_cache_delete( 'is_installed', 'cron-control' ); + $is_installed = Events_Store::is_installed(); + + $wpdb->query( $wpdb->prepare( 'DROP DATABASE %i', $other_db ) ); + wp_cache_delete( 'is_installed', 'cron-control' ); + + $this->assertTrue( $is_installed ); + } + public function test_event_creation() { $store = Events_Store::instance(); diff --git a/includes/class-events-store.php b/includes/class-events-store.php index 90a9695c..4437598b 100644 --- a/includes/class-events-store.php +++ b/includes/class-events-store.php @@ -52,8 +52,7 @@ public static function is_installed(): bool { return $is_installed; } - $table_name = $wpdb->prefix . self::TABLE_SUFFIX; - $is_installed = 1 === count( $wpdb->get_col( $wpdb->prepare( 'SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME = %s', $table_name ) ) ); + $is_installed = self::table_exists( $wpdb->prefix . self::TABLE_SUFFIX ); // Cache the results, will be overridden by _prepare_table() during installation. wp_cache_add( 'is_installed', $is_installed, 'cron-control' ); @@ -61,6 +60,18 @@ public static function is_installed(): bool { return $is_installed; } + /** + * Check whether the table exists in the current database. + * + * Scoped to DATABASE() because the DB user can usually see every schema on the server, + * and a same-named table in another schema must not count. + */ + private static function table_exists( string $table_name ): bool { + global $wpdb; + + return null !== $wpdb->get_var( $wpdb->prepare( 'SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = %s', $table_name ) ); + } + /** * Build appropriate table name for this site. */ @@ -113,8 +124,6 @@ public function maybe_install_during_shutdown() { * Create the plugin's DB table when necessary */ protected function _prepare_table() { - global $wpdb; - // Use Core's method of creating/updating tables. if ( ! function_exists( 'dbDelta' ) ) { require_once ABSPATH . '/wp-admin/includes/upgrade.php'; @@ -147,7 +156,7 @@ protected function _prepare_table() { dbDelta( $schema, true ); // Confirm that the table was created, and set the option to prevent further updates. - $is_installed = 1 === count( $wpdb->get_col( $wpdb->prepare( 'SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME = %s', $table_name ) ) ); + $is_installed = self::table_exists( $table_name ); wp_cache_set( 'is_installed', $is_installed, 'cron-control' ); if ( $is_installed ) { From 971bfe897e49cc52466e8dc7087850da47bffc51 Mon Sep 17 00:00:00 2001 From: Rebecca Hum Date: Thu, 12 Nov 2026 13:57:24 -0600 Subject: [PATCH 2/3] test(events-store): use a unique database and always clean it up Create the second database with a per-run name and plain CREATE DATABASE, and drop it in finally, so the test only removes a database it created and parallel runs don't collide. Co-Authored-By: Claude Opus 5.5 --- __tests__/unit-tests/test-events-store.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/__tests__/unit-tests/test-events-store.php b/__tests__/unit-tests/test-events-store.php index 8ea7ee43..389cc0d8 100644 --- a/__tests__/unit-tests/test-events-store.php +++ b/__tests__/unit-tests/test-events-store.php @@ -32,23 +32,26 @@ public function test_is_installed_ignores_same_named_table_in_other_database() { remove_filter( 'query', array( $this, '_create_temporary_tables' ) ); remove_filter( 'query', array( $this, '_drop_temporary_tables' ) ); - $other_db = 'cron_control_tests_other'; + // Unique name, created without IF NOT EXISTS, so cleanup only drops a database this test made. + $other_db = 'cron_control_tests_' . strtolower( wp_generate_password( 12, false ) ); $table_name = Utils::get_table_name(); $suppress = $wpdb->suppress_errors(); - $created = $wpdb->query( $wpdb->prepare( 'CREATE DATABASE IF NOT EXISTS %i', $other_db ) ); + $created = $wpdb->query( $wpdb->prepare( 'CREATE DATABASE %i', $other_db ) ); $wpdb->suppress_errors( $suppress ); if ( false === $created ) { $this->markTestSkipped( 'The test DB user cannot create databases.' ); } - $wpdb->query( $wpdb->prepare( 'CREATE TABLE %i.%i (`ID` bigint(20) unsigned NOT NULL)', $other_db, $table_name ) ); + try { + $wpdb->query( $wpdb->prepare( 'CREATE TABLE %i.%i (`ID` bigint(20) unsigned NOT NULL)', $other_db, $table_name ) ); - wp_cache_delete( 'is_installed', 'cron-control' ); - $is_installed = Events_Store::is_installed(); - - $wpdb->query( $wpdb->prepare( 'DROP DATABASE %i', $other_db ) ); - wp_cache_delete( 'is_installed', 'cron-control' ); + wp_cache_delete( 'is_installed', 'cron-control' ); + $is_installed = Events_Store::is_installed(); + } finally { + $wpdb->query( $wpdb->prepare( 'DROP DATABASE %i', $other_db ) ); + wp_cache_delete( 'is_installed', 'cron-control' ); + } $this->assertTrue( $is_installed ); } From f9d3c8c0156830296b887dcb2e24382790cd1f3a Mon Sep 17 00:00:00 2001 From: Rebecca Hum Date: Thu, 12 Nov 2026 14:53:04 -0600 Subject: [PATCH 3/3] test(events-store): keep fixture DDL off the test connection CREATE DATABASE, CREATE TABLE and DROP DATABASE implicitly commit, which ended the test's transaction and made setUp() writes survive the rollback. Run them on a separate connection instead, and assert the same-named table was created so the test can't pass without the case it covers. Co-Authored-By: Claude Opus 5.5 --- __tests__/unit-tests/test-events-store.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/__tests__/unit-tests/test-events-store.php b/__tests__/unit-tests/test-events-store.php index 389cc0d8..76eeb310 100644 --- a/__tests__/unit-tests/test-events-store.php +++ b/__tests__/unit-tests/test-events-store.php @@ -26,34 +26,34 @@ public function test_table_exists() { } public function test_is_installed_ignores_same_named_table_in_other_database() { - global $wpdb; - // Use real tables, since temporary tables don't show up in information_schema. remove_filter( 'query', array( $this, '_create_temporary_tables' ) ); remove_filter( 'query', array( $this, '_drop_temporary_tables' ) ); + // Run the fixture DDL on its own connection: DDL implicitly commits, which would end the test's transaction. + $ddl = new \wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST ); + $ddl->suppress_errors(); + // Unique name, created without IF NOT EXISTS, so cleanup only drops a database this test made. $other_db = 'cron_control_tests_' . strtolower( wp_generate_password( 12, false ) ); $table_name = Utils::get_table_name(); - $suppress = $wpdb->suppress_errors(); - $created = $wpdb->query( $wpdb->prepare( 'CREATE DATABASE %i', $other_db ) ); - $wpdb->suppress_errors( $suppress ); - if ( false === $created ) { + if ( false === $ddl->query( $ddl->prepare( 'CREATE DATABASE %i', $other_db ) ) ) { + $ddl->close(); $this->markTestSkipped( 'The test DB user cannot create databases.' ); } try { - $wpdb->query( $wpdb->prepare( 'CREATE TABLE %i.%i (`ID` bigint(20) unsigned NOT NULL)', $other_db, $table_name ) ); + $created_table = $ddl->query( $ddl->prepare( 'CREATE TABLE %i.%i (`ID` bigint(20) unsigned NOT NULL)', $other_db, $table_name ) ); + $this->assertNotFalse( $created_table, 'The same-named table could not be created in the other database.' ); wp_cache_delete( 'is_installed', 'cron-control' ); - $is_installed = Events_Store::is_installed(); + $this->assertTrue( Events_Store::is_installed() ); } finally { - $wpdb->query( $wpdb->prepare( 'DROP DATABASE %i', $other_db ) ); + $ddl->query( $ddl->prepare( 'DROP DATABASE %i', $other_db ) ); + $ddl->close(); wp_cache_delete( 'is_installed', 'cron-control' ); } - - $this->assertTrue( $is_installed ); } public function test_event_creation() {