fix(hook-bus): write_to_flat 改用 TMDO_DB::upsert(B4)
原本 SELECT id → UPDATE/INSERT 三步,並發寫入同一 entity 會產生 duplicate row 或互相覆蓋。改為單一 upsert(A v3.1.7 同款)。 unit 379 / integration 398 GREEN Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
This commit is contained in:
@@ -321,35 +321,18 @@ final class TMDO_Hook_Bus {
|
|||||||
self::$internal_ops[ $lock_key ] = true;
|
self::$internal_ops[ $lock_key ] = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 檢查列是否存在
|
// 單一 upsert,避免 SELECT→UPDATE/INSERT 的 read-modify-write race。
|
||||||
$exists = $wpdb->get_var(
|
$result = TMDO_DB::upsert(
|
||||||
$wpdb->prepare(
|
$table,
|
||||||
"SELECT id FROM `{$table}` WHERE `{$id_col}` = %d",
|
array(
|
||||||
$entity_id
|
$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 );
|
TMDO_Cache_Orchestrator::invalidate( $type, $entity_id, $group );
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user