feat(2meet-data-optimizer): store post_status and post_date copies in post entity tables
- TMDO_Post_Status_Sync: post group tables get post_status / post_date
columns (posts' collation) and an (post_status, post_date) index.
Brand-new empty tables get them on creation; existing tables only via
`wp tmdo post-status-sync [--dry-run] [--batch]`, never as DDL on a
populated table in a web request. Per-group state in
tmdo_post_status_columns ('columns' | 'ready').
- transition_post_status refreshes the copies; Hook Bus upserts that
create a post row fill them in (existing rows keep theirs).
- Bench (1M rows): add columns 0.05 s, index 5.3 s, batched backfill 32 s,
0 mismatches afterwards.
- Verify: core unit 659 / integration 466 green, phpcs clean
- PR: PR-028
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* post_status / post_date copies on post entity tables (O-1 schema).
|
||||
*
|
||||
* Listing 1 (publish) has rows in both groups, draft 2 only in ps_a.
|
||||
*/
|
||||
class PostStatusSyncTest extends TestCase {
|
||||
|
||||
private const A = 'wp_itest_wpdo_post_ps_a';
|
||||
private const B = 'wp_itest_wpdo_post_ps_b';
|
||||
|
||||
public static function setUpBeforeClass(): void {
|
||||
$base = WPDO_PLUGIN_DIR;
|
||||
foreach ( array(
|
||||
'includes/adapters/interface-entity-adapter.php',
|
||||
'includes/engine/class-tmdo-type-caster.php',
|
||||
'includes/engine/class-tmdo-schema-manager.php',
|
||||
'includes/engine/class-tmdo-entity-registry.php',
|
||||
'includes/engine/class-tmdo-mode-manager.php',
|
||||
'includes/engine/class-tmdo-cache-orchestrator.php',
|
||||
'includes/adapters/class-tmdo-adapter-post.php',
|
||||
'includes/engine/class-tmdo-hook-bus.php',
|
||||
'includes/engine/class-tmdo-post-status-sync.php',
|
||||
) as $file ) {
|
||||
require_once $base . $file;
|
||||
}
|
||||
}
|
||||
|
||||
protected function setUp(): void {
|
||||
global $wpdb;
|
||||
self::drop();
|
||||
$wpdb->query( "CREATE TABLE `{$wpdb->posts}` ( ID BIGINT UNSIGNED NOT NULL, post_type VARCHAR(20) NOT NULL DEFAULT 'post', post_status VARCHAR(20) NOT NULL DEFAULT 'publish', post_date DATETIME NOT NULL DEFAULT '2000-01-01 00:00:00', PRIMARY KEY (ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci" );
|
||||
foreach ( array( self::A, self::B ) as $table ) {
|
||||
$wpdb->query( "CREATE TABLE `{$table}` ( id BIGINT NOT NULL AUTO_INCREMENT, post_id BIGINT NOT NULL, k VARCHAR(255) DEFAULT NULL, PRIMARY KEY (id), UNIQUE KEY uk_entity (post_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" );
|
||||
}
|
||||
$wpdb->query( "INSERT INTO `{$wpdb->posts}` VALUES (1, 'hp_listing', 'publish', '2026-01-01 10:00:00'), (2, 'hp_listing', 'draft', '2026-02-01 10:00:00'), (3, 'hp_listing', 'pending', '2026-03-01 10:00:00')" );
|
||||
$wpdb->query( 'INSERT INTO `' . self::A . "` (post_id, k) VALUES (1, 'a1'), (2, 'a2')" );
|
||||
$wpdb->query( 'INSERT INTO `' . self::B . "` (post_id, k) VALUES (1, 'b1')" );
|
||||
|
||||
$GLOBALS['_wp_options'] = array();
|
||||
$GLOBALS['_wp_cache'] = array();
|
||||
$GLOBALS['_wp_posts'] = array();
|
||||
$GLOBALS['_wp_current_filter'] = '';
|
||||
|
||||
$ref = new ReflectionProperty( WPDO_Schema_Manager::class, 'table_exists_cache' );
|
||||
$ref->setAccessible( true );
|
||||
$ref->setValue( null, array() );
|
||||
|
||||
WPDO_Entity_Registry::init();
|
||||
WPDO_Entity_Registry::register_adapter( 'post', new WPDO_Adapter_Post() );
|
||||
WPDO_Entity_Registry::register_group( 'post', 'ps_a', array( array( 'key' => 'k', 'type' => 'text' ) ) );
|
||||
WPDO_Entity_Registry::register_group( 'post', 'ps_b', array( array( 'key' => 'k2', 'type' => 'text' ) ) );
|
||||
}
|
||||
|
||||
protected function tearDown(): void {
|
||||
self::drop();
|
||||
WPDO_Entity_Registry::init();
|
||||
$ref = new ReflectionProperty( WPDO_Mode_Manager::class, 'cache' );
|
||||
$ref->setAccessible( true );
|
||||
$ref->setValue( null, null );
|
||||
}
|
||||
|
||||
private static function drop(): void {
|
||||
global $wpdb;
|
||||
foreach ( array( $wpdb->posts, self::A, self::B ) as $table ) {
|
||||
$wpdb->query( "DROP TABLE IF EXISTS `{$table}`" );
|
||||
}
|
||||
}
|
||||
|
||||
/** @return array<int, array{0:?string, 1:?string}> post_id → [status, date] */
|
||||
private function copies( string $table ): array {
|
||||
global $wpdb;
|
||||
$out = array();
|
||||
foreach ( (array) $wpdb->get_results( "SELECT post_id, post_status, post_date FROM `{$table}` ORDER BY post_id", ARRAY_A ) as $row ) {
|
||||
$out[ (int) $row['post_id'] ] = array( $row['post_status'], $row['post_date'] );
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
private function has_index( string $table ): bool {
|
||||
global $wpdb;
|
||||
return in_array( 'idx_post_status_date', array_column( (array) $wpdb->get_results( "SHOW INDEX FROM `{$table}`", ARRAY_A ), 'Key_name' ), true );
|
||||
}
|
||||
|
||||
public function test_run_adds_the_columns_copies_every_row_and_marks_groups_ready(): void {
|
||||
$report = TMDO_Post_Status_Sync::run( false, 1 );
|
||||
|
||||
$this->assertSame(
|
||||
array(
|
||||
1 => array( 'publish', '2026-01-01 10:00:00' ),
|
||||
2 => array( 'draft', '2026-02-01 10:00:00' ),
|
||||
),
|
||||
$this->copies( self::A )
|
||||
);
|
||||
$this->assertSame( array( 1 => array( 'publish', '2026-01-01 10:00:00' ) ), $this->copies( self::B ) );
|
||||
$this->assertTrue( $this->has_index( self::A ) );
|
||||
$this->assertSame( 'ready', TMDO_Post_Status_Sync::state( 'ps_a' ) );
|
||||
$this->assertSame( 'ready', TMDO_Post_Status_Sync::state( 'ps_b' ) );
|
||||
$this->assertSame( 0, TMDO_Post_Status_Sync::mismatches( 'ps_a' ) );
|
||||
$this->assertSame( 2, $report['ps_a']['mismatched'] );
|
||||
}
|
||||
|
||||
public function test_copies_use_the_posts_collation_so_they_compare_with_posts(): void {
|
||||
global $wpdb;
|
||||
TMDO_Post_Status_Sync::run();
|
||||
|
||||
$column = $wpdb->get_row( 'SHOW FULL COLUMNS FROM `' . self::A . "` LIKE 'post_status'", ARRAY_A );
|
||||
$this->assertSame( 'utf8mb4_unicode_520_ci', $column['Collation'] );
|
||||
$wpdb->query( 'UPDATE `' . self::A . "` SET post_status = 'stale' WHERE post_id = 2" );
|
||||
$this->assertSame( 1, TMDO_Post_Status_Sync::mismatches( 'ps_a' ) );
|
||||
}
|
||||
|
||||
public function test_dry_run_only_counts(): void {
|
||||
global $wpdb;
|
||||
$report = TMDO_Post_Status_Sync::run( true );
|
||||
|
||||
$this->assertSame( 2, $report['ps_a']['mismatched'] );
|
||||
$this->assertSame( 1, $report['ps_b']['mismatched'] );
|
||||
$this->assertNotContains( 'post_status', $wpdb->get_col( 'SHOW COLUMNS FROM `' . self::A . '`' ) );
|
||||
$this->assertSame( '', TMDO_Post_Status_Sync::state( 'ps_a' ) );
|
||||
}
|
||||
|
||||
public function test_status_transition_updates_the_post_in_every_group_table(): void {
|
||||
TMDO_Post_Status_Sync::run();
|
||||
|
||||
TMDO_Post_Status_Sync::on_transition( 'trash', 'publish', (object) array( 'ID' => 1, 'post_status' => 'trash', 'post_date' => '2026-01-05 00:00:00' ) );
|
||||
|
||||
$this->assertSame( array( 'trash', '2026-01-05 00:00:00' ), $this->copies( self::A )[1] );
|
||||
$this->assertSame( array( 'trash', '2026-01-05 00:00:00' ), $this->copies( self::B )[1] );
|
||||
$this->assertSame( array( 'draft', '2026-02-01 10:00:00' ), $this->copies( self::A )[2], 'other posts untouched' );
|
||||
}
|
||||
|
||||
public function test_transition_leaves_tables_without_the_columns_alone(): void {
|
||||
global $wpdb;
|
||||
$wpdb->last_error = '';
|
||||
|
||||
TMDO_Post_Status_Sync::on_transition( 'trash', 'publish', (object) array( 'ID' => 1, 'post_status' => 'trash', 'post_date' => '2026-01-05 00:00:00' ) );
|
||||
|
||||
$this->assertSame( '', (string) $wpdb->last_error );
|
||||
}
|
||||
|
||||
public function test_new_empty_table_gets_the_columns_at_once_but_a_populated_one_waits_for_the_cli(): void {
|
||||
global $wpdb;
|
||||
$wpdb->query( 'TRUNCATE TABLE `' . self::B . '`' );
|
||||
|
||||
TMDO_Post_Status_Sync::on_schema_updated( 'post', 'ps_b', self::B );
|
||||
TMDO_Post_Status_Sync::on_schema_updated( 'post', 'ps_a', self::A );
|
||||
|
||||
$this->assertSame( 'ready', TMDO_Post_Status_Sync::state( 'ps_b' ) );
|
||||
$this->assertTrue( $this->has_index( self::B ) );
|
||||
$this->assertSame( '', TMDO_Post_Status_Sync::state( 'ps_a' ) );
|
||||
$this->assertNotContains( 'post_status', $wpdb->get_col( 'SHOW COLUMNS FROM `' . self::A . '`' ) );
|
||||
}
|
||||
|
||||
public function test_flat_write_that_creates_a_row_fills_the_copies(): void {
|
||||
TMDO_Post_Status_Sync::run();
|
||||
$mode = new ReflectionProperty( WPDO_Mode_Manager::class, 'cache' );
|
||||
$mode->setAccessible( true );
|
||||
$mode->setValue( null, array( 'post' => 'dual_write', 'user' => 'disabled', 'term' => 'disabled', 'comment' => 'disabled' ) );
|
||||
$GLOBALS['_wp_posts'][3] = (object) array( 'ID' => 3, 'post_type' => 'hp_listing', 'post_status' => 'pending', 'post_date' => '2026-03-01 10:00:00' );
|
||||
$GLOBALS['_wp_current_filter'] = 'update_post_metadata';
|
||||
|
||||
WPDO_Hook_Bus::intercept_update( null, 3, 'k', 'a3', '' );
|
||||
WPDO_Hook_Bus::intercept_update( null, 1, 'k', 'a1b', '' );
|
||||
|
||||
$this->assertSame( array( 'pending', '2026-03-01 10:00:00' ), $this->copies( self::A )[3] );
|
||||
$this->assertSame( array( 'publish', '2026-01-01 10:00:00' ), $this->copies( self::A )[1], 'existing row keeps its synced copies' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user