fix(2meet-data-optimizer): skip zone deletes on tables that were never created

- TMDO_Zone_Router::delete_post() deleted from wp_wpdo_hot_/cold_{post_type}
  whenever fields were registered for that zone, so every post delete on a
  HivePress site (cold fields registered, module idle, table never created)
  logged a "table doesn't exist" error. Hot and cold deletes now also need
  the table to exist; module state is not checked, so a table left by a
  rolled-back module still loses the rows of deleted posts.
- Live suite zero_side_effect: saving / updating / deleting an hp_listing
  issues no query on missing zone tables (covers PR-054 and this PR).
- Verify: ZoneRouterTest 22 OK; live zero_side_effect + shadow_write +
  serialized 9/9 PASS (old code: zero_side_effect FAIL); unit 687 OK,
  integration 484 OK, phpcs (phpcs.xml) 0, stan OK
- PR: PR-054b
This commit is contained in:
2026-09-26 19:02:10 +08:00
parent b84252d787
commit 781f171770
4 changed files with 114 additions and 16 deletions
+61 -13
View File
@@ -21,9 +21,69 @@ class ZoneRouterTest extends TestCase {
self::$last_query = '';
self::$last_delete = [];
self::$get_var_return = null;
WPDO_Schema_Manager::flush_table_exists_cache();
$this->setup_wpdb_mock();
}
/**
* $wpdb mock recording delete() tables; SHOW TABLES reports $existing.
*
* @param string[] $existing Tables that exist.
*/
private function delete_logger( array &$captured, array $existing ): void {
global $wpdb;
$wpdb = new class( $captured, $existing ) {
public string $prefix = 'wp_';
public array $log;
public function __construct( array &$ref, private array $existing ) { $this->log = &$ref; }
public function prepare( string $s, mixed ...$a ): string { return str_replace( '%s', (string) ( $a[0] ?? '' ), $s ); }
public function query( string $s ): int { return 1; }
public function get_var( string $s ): mixed {
foreach ( $this->existing as $t ) {
if ( "SHOW TABLES LIKE {$t}" === $s ) {
return $t;
}
}
return null;
}
public function insert( string $t, array $d, mixed $f = null ): int { return 1; }
public function delete( string $table, array $where, array $formats ): void {
$this->log[] = $table;
}
};
}
private function register_hot_and_cold(): void {
$sr = new ReflectionClass( WPDO_Schema_Registry::class );
$sr->getProperty( 'instance' )->setValue( null, null );
WPDO_Schema_Registry::instance()->register( 'test', array( 'post_type' => 'hp_listing', 'meta_key' => 'hp_price', 'zone' => 'hot', 'column' => 'hp_price', 'type' => 'decimal' ) );
WPDO_Schema_Registry::instance()->register( 'test', array( 'post_type' => 'hp_listing', 'meta_key' => 'hp_images', 'zone' => 'cold' ) );
}
/** Registered fields do not mean the zone tables exist (zero side effect). */
public function test_delete_post_skips_hot_and_cold_tables_that_do_not_exist(): void {
$this->register_hot_and_cold();
$captured = array();
$this->delete_logger( $captured, array() );
WPDO_Zone_Router::delete_post( 3, 'hp_listing' );
$this->assertNotContains( 'wp_wpdo_hot_hp_listing', $captured );
$this->assertNotContains( 'wp_wpdo_cold_hp_listing', $captured );
$this->assertContains( 'wp_wpdo_warm', $captured );
}
public function test_delete_post_deletes_cold_rows_when_the_table_exists(): void {
$this->register_hot_and_cold();
$captured = array();
$this->delete_logger( $captured, array( 'wp_wpdo_cold_hp_listing' ) );
WPDO_Zone_Router::delete_post( 4, 'hp_listing' );
$this->assertContains( 'wp_wpdo_cold_hp_listing', $captured );
$this->assertNotContains( 'wp_wpdo_hot_hp_listing', $captured );
}
private function setup_wpdb_mock(): void {
global $wpdb;
$wpdb = new class {
@@ -204,19 +264,7 @@ class ZoneRouterTest extends TestCase {
) );
$captured = array();
global $wpdb;
$wpdb = new class( $captured ) {
public string $prefix = 'wp_';
public array $log;
public function __construct( array &$ref ) { $this->log = &$ref; }
public function prepare( string $s, mixed ...$a ): string { return $s; }
public function query( string $s ): int { return 1; }
public function get_var( string $s ): mixed { return null; }
public function insert( string $t, array $d, mixed $f = null ): int { return 1; }
public function delete( string $table, array $where, array $formats ): void {
$this->log[] = $table;
}
};
$this->delete_logger( $captured, array( 'wp_wpdo_hot_hp_listing' ) );
WPDO_Zone_Router::delete_post( 10, 'hp_listing' );