fix(2meet-data-optimizer): restore native meta without overwriting newer values or inventing defaults

- TMDO_Native_Restorer::run() gains a policy: overwrite (flat wins; the
  default in aeav_only, where writes only reached flat and native is
  stale) or only_missing (native wins, flat only fills gaps; the default in
  every other mode, where native is the source of truth and may be newer —
  on dev36 an aeav_only round trip overwrote WooCommerce stock 42 with 0).
  CLI: --overwrite / --only-missing.
- Native values are read first and each owned key is classified as
  missing / differs / same / default_skipped; unchanged values are no
  longer rewritten. A flat value equal to its field default is not
  written for an entity without a native row (dev36: 1,368 invented
  session_count / account_flags rows). --dry-run reports the counts.
- Verify: NativeRestorer* 17 OK (5 new: policy by mode, forced
  overwrite, only-missing in aeav_only, dry-run counts, no invented
  defaults); unit 687 OK, integration 490 OK, phpcs (phpcs.xml) 0,
  stan OK; dev36 `bridge-restore-native user --dry-run`: policy
  only_missing, activity 120 defaults skipped
- PR: PR-058
This commit is contained in:
2026-09-26 19:22:34 +08:00
parent 77e338f9c3
commit 8c049f0cf2
3 changed files with 278 additions and 28 deletions
+29 -4
View File
@@ -162,7 +162,15 @@ class TMDO_CLI_V2 {
* : post | user | term | comment
*
* [--dry-run]
* : Count the flat rows per group; write nothing.
* : Count the flat rows per group and how their values compare with native
* meta (missing / differs / same / default skipped); write nothing.
*
* [--overwrite]
* : Flat values replace differing native values. Default in aeav_only.
*
* [--only-missing]
* : Only fill native rows that do not exist; never replace. Default in every
* other mode, where native meta is the source of truth.
*
* [--batch=<n>]
* : Rows per batch. Default 500.
@@ -190,17 +198,34 @@ class TMDO_CLI_V2 {
public function bridge_restore_native( $args, $assoc_args ): void {
$entity = (string) ( $args[0] ?? '' );
if ( ! in_array( $entity, TMDO_Entity_Registry::VALID_ENTITY_TYPES, true ) ) {
WP_CLI::error( 'Usage: wp tmdo bridge-restore-native <post|user|term|comment> [--dry-run] [--batch=<n>] [--demote] [--group=<group> [--retire]]' );
WP_CLI::error( 'Usage: wp tmdo bridge-restore-native <post|user|term|comment> [--dry-run] [--overwrite|--only-missing] [--batch=<n>] [--demote] [--group=<group> [--retire]]' );
}
$dry_run = isset( $assoc_args['dry-run'] );
$group = sanitize_key( (string) ( $assoc_args['group'] ?? '' ) );
if ( isset( $assoc_args['retire'] ) && '' === $group ) {
WP_CLI::error( '--retire needs --group=<group>.' );
}
$report = TMDO_Native_Restorer::run( $entity, $dry_run, (int) ( $assoc_args['batch'] ?? 500 ), isset( $assoc_args['demote'] ), $group );
if ( isset( $assoc_args['overwrite'] ) && isset( $assoc_args['only-missing'] ) ) {
WP_CLI::error( 'Use either --overwrite or --only-missing, not both.' );
}
$policy = isset( $assoc_args['overwrite'] ) ? 'overwrite' : ( isset( $assoc_args['only-missing'] ) ? 'only_missing' : '' );
$report = TMDO_Native_Restorer::run( $entity, $dry_run, (int) ( $assoc_args['batch'] ?? 500 ), isset( $assoc_args['demote'] ), $group, $policy );
WP_CLI::log( "Policy: {$report['policy']}" );
foreach ( $report['groups'] as $group => $g ) {
WP_CLI::log( sprintf( '%s/%s: %d row(s)%s', $entity, $group, $g['rows'], $dry_run ? '' : sprintf( ', %d native value(s) written', $g['written'] ) ) );
WP_CLI::log(
sprintf(
'%s/%s: %d row(s) — native missing %d, differs %d, same %d, default skipped %d%s',
$entity,
$group,
$g['rows'],
$g['missing'],
$g['differs'],
$g['same'],
$g['default_skipped'],
$dry_run ? '' : sprintf( '; %d native value(s) written', $g['written'] )
)
);
}
if ( $dry_run ) {
WP_CLI::success( '[dry-run] Nothing written.' );