fix(2meet-data-optimizer): drop cached routed counts when the migration engine rebuilds post rows

- migrate_entity (drift-check --fix), migrate_group and migrate_group_batch
  write flat rows outside the Hook Bus: bump the routed count version
  (once per call / batch; not on dry runs; post rows only)
- dev36: a vendor search kept counting 21515 of 21522 matches after a fix
- Verify: composer test (704), test:integration (598), phpcs, stan
- PR: PR-129
This commit is contained in:
2026-09-27 23:25:03 +08:00
parent 8ee3dce0fe
commit 692b23aed9
2 changed files with 60 additions and 1 deletions
@@ -176,6 +176,9 @@ final class TMDO_Entity_Migration_Engine {
// 標記完成
if ( ! $options['dry_run'] ) {
self::mark_migration_completed( $entity_type, $group_name );
if ( $stats['migrated'] > 0 ) {
self::counts_changed( $entity_type );
}
}
$stats['elapsed_sec'] = round( microtime( true ) - $start_time, 2 );
@@ -183,6 +186,19 @@ final class TMDO_Entity_Migration_Engine {
return $stats;
}
/**
* Rows rebuilt outside the Hook Bus (backfill, drift-check --fix): routed
* post counts cached for the old rows are stale (2.4 PR-129).
*
* @param string $entity_type Entity type.
* @return void
*/
private static function counts_changed( string $entity_type ): void {
if ( 'post' === $entity_type && class_exists( 'TMDO_Post_Query_Router' ) ) {
TMDO_Post_Query_Router::bump_count_version();
}
}
/**
* Rebuild one entity's row of a group from native meta.
*
@@ -194,7 +210,7 @@ final class TMDO_Entity_Migration_Engine {
if ( ! $adapter || empty( $group_fields ) ) {
return false;
}
return self::migrate_single_entity(
$migrated = self::migrate_single_entity(
$entity_type,
$group_name,
$entity_id,
@@ -203,6 +219,10 @@ final class TMDO_Entity_Migration_Engine {
$adapter,
false
);
if ( $migrated ) {
self::counts_changed( $entity_type );
}
return $migrated;
}
/**
@@ -409,6 +429,9 @@ final class TMDO_Entity_Migration_Engine {
$total = $prev_total + $batch_migrated;
$done = count( $entity_ids ) < $batch_size;
if ( $batch_migrated > 0 ) {
self::counts_changed( $entity_type );
}
if ( $done ) {
self::mark_migration_completed( $entity_type, $group_name );