fix(zone-cold): JSON_MERGE_PATCH 原子寫入 + JSON_REMOVE 原子刪除(B2)

set()/set_many() 原本 get_blob_raw()→array_merge→save_blob() 三步,並發
寫入會互相覆蓋整個 blob;remove() 同樣是 read-modify-write。改為:
- 新增 private save_patch():INSERT ... ON DUPLICATE KEY UPDATE
  data = JSON_MERGE_PATCH(COALESCE(data,'{}'), patch)
- remove():UPDATE ... SET data = JSON_REMOVE(COALESCE(data,'{}'), '$.key')
- save_blob() 改用 TMDO_DB::upsert()
- SQLite 保留 read-modify-write fallback
- ZoneColdTest stub 補 JSON_REMOVE / JSON_MERGE_PATCH / upsert 三種 SQL 模擬

對應 A v3.3.3 P1-21。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:
2026-07-31 05:34:27 +08:00
parent 151a14a7e8
commit 59f3b19ce6
2 changed files with 121 additions and 36 deletions
+39
View File
@@ -89,6 +89,45 @@ class ZoneColdTest extends TestCase {
}
public function query( string $sql ): int|bool {
$flat = preg_replace( '/\s+/', ' ', $sql );
// JSON_REMOVE UPDATE — from TMDO_Zone_Cold::remove (MySQL path).
// SQL: UPDATE `{table}` SET data = JSON_REMOVE(..., '$.key'), ... WHERE post_id = N
if ( stripos( $flat, 'JSON_REMOVE' ) !== false ) {
if ( preg_match( "/'\\\$\\.([^']+)'/", $flat, $km )
&& preg_match( '/WHERE post_id = (\d+)/i', $flat, $wm ) ) {
$post_id = (int) $wm[1];
$key = $km[1];
$data = json_decode( ZoneColdTest::$db_store[ $post_id ] ?? '{}', true );
$data = is_array( $data ) ? $data : array();
unset( $data[ $key ] );
ZoneColdTest::$db_store[ $post_id ] = (string) wp_json_encode( $data );
}
return 1;
}
// INSERT...ON DUPLICATE KEY with JSON_MERGE_PATCH — from save_patch (MySQL path).
// Merges the patch into the existing blob, preserving untouched keys.
if ( stripos( $flat, 'ON DUPLICATE KEY' ) !== false && stripos( $flat, 'JSON_MERGE_PATCH' ) !== false ) {
if ( preg_match( "/VALUES \((\d+), '((?:[^'\\\\]|\\\\.)*)'/i", $flat, $m ) ) {
$post_id = (int) $m[1];
$patch = json_decode( stripslashes( $m[2] ), true );
$patch = is_array( $patch ) ? $patch : array();
$existing = json_decode( ZoneColdTest::$db_store[ $post_id ] ?? '{}', true );
$existing = is_array( $existing ) ? $existing : array();
ZoneColdTest::$db_store[ $post_id ] = (string) wp_json_encode( array_merge( $existing, $patch ) );
}
return 1;
}
// INSERT...ON DUPLICATE KEY UPDATE (from TMDO_DB::upsert via save_blob).
// Pattern: VALUES (post_id, 'json_data', 'datetime') ON DUPLICATE KEY UPDATE col = VALUES(col)
if ( stripos( $flat, 'ON DUPLICATE KEY' ) !== false ) {
if ( preg_match( "/VALUES \((\d+), '((?:[^'\\\\]|\\\\.)*)'/i", $flat, $m ) ) {
ZoneColdTest::$db_store[ (int) $m[1] ] = stripslashes( $m[2] );
}
}
return 1;
}
};