feat(2meet-data-optimizer): repair drifted flat rows from native with drift-check --fix

- TMDO_Drift_Check::fix() deletes flat rows of deleted entities and, for post groups, of posts outside the group's post types; rebuilds every other drifted entity's row from native through the new TMDO_Entity_Migration_Engine::migrate_entity() (keys without native rows become NULL, PR-066); keeps a row whose entity has no native value for the group but clears its columns (no NULL-only inserts, no deleted rows the router may join on); invalidates the row cache of each touched entity; then checks again. Native meta is never written.
- `wp tmdo drift-check <entity> --fix` (not combinable with --sample).
- Verify: DriftCheckTest fix cases (RED: method missing); dev36 --fix: post 42 rows rebuilt, 1 stray row (post 312, hp_booking in hp_draft_flags) deleted, all four entities 0 drift after, checker miss/extra 0.
- PR: PR-071
This commit is contained in:
2026-09-27 00:07:33 +08:00
parent 02045c6f07
commit 3a347269c8
4 changed files with 222 additions and 29 deletions
+21 -3
View File
@@ -187,11 +187,11 @@ class TMDO_CLI_V2 {
}
/**
* Compare an entity's flat tables with native meta (read-only).
* Compare an entity's flat tables with native meta, optionally repairing them.
*
* Reports per key how many values are missing from flat (miss), differ
* (diff) or exist only in flat (extra), and records the outcome per group
* in the wpdo_drift_status option.
* in the wpdo_drift_status option. Read-only unless --fix is given.
*
* ## OPTIONS
*
@@ -204,6 +204,11 @@ class TMDO_CLI_V2 {
* [--sample=<n>]
* : Check n random entities per group instead of all of them.
*
* [--fix]
* : Rebuild drifted flat rows from native meta (native is never written),
* delete rows of deleted entities / other post types, then check again.
* Not combinable with --sample.
*
* [--format=<format>]
* : table or json. Default: table.
*
@@ -212,16 +217,29 @@ class TMDO_CLI_V2 {
* wp tmdo drift-check post
* wp tmdo drift-check user --group=activity
* wp tmdo drift-check post --sample=500
* wp tmdo drift-check post --fix
*
* @param array<int, string> $args Entity type.
* @param array<string, string> $assoc_args Options.
*/
public function drift_check( $args, $assoc_args ): void {
$entity = (string) ( $args[0] ?? '' );
$group = (string) ( $assoc_args['group'] ?? '' );
$sample = max( 0, (int) ( $assoc_args['sample'] ?? 0 ) );
$fix = isset( $assoc_args['fix'] );
if ( $fix && $sample ) {
WP_CLI::error( '--fix repairs every entity; it cannot be combined with --sample.' );
return;
}
try {
$report = TMDO_Drift_Check::run( $entity, (string) ( $assoc_args['group'] ?? '' ), $sample );
if ( $fix ) {
$result = TMDO_Drift_Check::fix( $entity, $group );
$report = $result['after'];
WP_CLI::log( sprintf( 'Fixed: %d row(s) rebuilt from native, %d cleared, %d stray row(s) deleted.', $result['fixed']['rebuilt'], $result['fixed']['cleared'], $result['fixed']['deleted_rows'] ) );
} else {
$report = TMDO_Drift_Check::run( $entity, $group, $sample );
}
} catch ( InvalidArgumentException $e ) {
WP_CLI::error( $e->getMessage() );
return;