From caf0351294b676bf6bd9cff9eb6ad97b87f22e27 Mon Sep 17 00:00:00 2001 From: wpdev Date: Fri, 31 Jul 2026 05:35:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(hook-bus):=20write=5Fto=5Fflat=20=E6=94=B9?= =?UTF-8?q?=E7=94=A8=20TMDO=5FDB::upsert=EF=BC=88B4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原本 SELECT id → UPDATE/INSERT 三步,並發寫入同一 entity 會產生 duplicate row 或互相覆蓋。改為單一 upsert(A v3.1.7 同款)。 unit 379 / integration 398 GREEN Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY --- includes/engine/class-tmdo-hook-bus.php | 37 +++++++------------------ 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/includes/engine/class-tmdo-hook-bus.php b/includes/engine/class-tmdo-hook-bus.php index 7d20802..3f6ba30 100644 --- a/includes/engine/class-tmdo-hook-bus.php +++ b/includes/engine/class-tmdo-hook-bus.php @@ -321,35 +321,18 @@ final class TMDO_Hook_Bus { self::$internal_ops[ $lock_key ] = true; try { - // 檢查列是否存在 - $exists = $wpdb->get_var( - $wpdb->prepare( - "SELECT id FROM `{$table}` WHERE `{$id_col}` = %d", - $entity_id - ) + // 單一 upsert,避免 SELECT→UPDATE/INSERT 的 read-modify-write race。 + $result = TMDO_DB::upsert( + $table, + array( + $id_col => $entity_id, + $col => $db_value, + ), + array( $col ), + $id_col, + array( '%d', $format ) ); - if ( $exists ) { - // UPDATE - $result = $wpdb->update( - $table, - array( $col => $db_value ), - array( $id_col => $entity_id ), - array( $format ), - array( '%d' ) - ); - } else { - // INSERT - $result = $wpdb->insert( - $table, - array( - $id_col => $entity_id, - $col => $db_value, - ), - array( '%d', $format ) - ); - } - // 清除快取 TMDO_Cache_Orchestrator::invalidate( $type, $entity_id, $group );