fix(zone-warm): warm 表 UNIQUE KEY + set() 改 upsert + 原子 increment(B1/B3)

- CREATE TABLE wpdo_warm 的 meta_key varchar(255)→varchar(191),
  KEY idx_post_meta → UNIQUE KEY ui_post_meta:沒有這個唯一鍵,
  ON DUPLICATE KEY UPDATE 不會觸發,會不斷 INSERT 重複列。
  (既有站台的 ALTER 遷移原本就在 installer:1329,只有 CREATE 落後)
- Zone_Warm::set() 由 SELECT→update/insert 改為 TMDO_DB::upsert()
- 新增 Zone_Warm::increment():CAST(COALESCE(meta_value,0) AS SIGNED)+N
  原子計數,取代 get()+set() 的 read-modify-write race(A v3.3.3)
- ZoneWarmTest stub 與 ZoneWarmIntegrationTest 建表同步適配

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:31:34 +08:00
parent 68f7f6871c
commit 151a14a7e8
4 changed files with 81 additions and 35 deletions
@@ -28,7 +28,7 @@ class ZoneWarmIntegrationTest extends TestCase {
`expires_at` datetime DEFAULT NULL,
`created_at` datetime NOT NULL DEFAULT \'0000-00-00 00:00:00\',
PRIMARY KEY (`id`),
KEY `post_id` (`post_id`),
UNIQUE KEY `ui_post_meta` (`post_id`, `meta_key`),
KEY `expires_at` (`expires_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4'
);
+34
View File
@@ -102,6 +102,40 @@ class ZoneWarmTest extends TestCase {
}
public function query( string $sql ): int|bool {
$store = &ZoneWarmTest::$store;
$flat = preg_replace( '/\s+/', ' ', $sql );
// INSERT ... ON DUPLICATE KEY UPDATE (from set() via TMDO_DB::upsert())
// Pattern: VALUES (post_id, 'meta_key', 'value', expires_or_NULL, 'now') ON DUPLICATE KEY
if ( stripos( $flat, 'ON DUPLICATE KEY' ) !== false && stripos( $flat, 'COALESCE' ) === false ) {
if ( preg_match( "/VALUES \((\d+), '([^']+)', '([^']*)', (NULL|'[^']+'), '[^']+'\)/i", $flat, $m ) ) {
$post_id = (int) $m[1];
$meta_key = $m[2];
$meta_value = $m[3];
$expires_at = 'NULL' === $m[4] ? null : strtotime( trim( $m[4], "'" ) );
$store[ $post_id ][ $meta_key ] = array(
'value' => $meta_value,
'expires_at' => $expires_at,
);
return 1;
}
}
// INSERT ... ON DUPLICATE KEY UPDATE meta_value = CAST(COALESCE...) + N (from increment())
if ( stripos( $flat, 'COALESCE' ) !== false ) {
if ( preg_match( "/VALUES \((\d+), '([^']+)', (\d+),/i", $flat, $m ) ) {
$post_id = (int) $m[1];
$meta_key = $m[2];
$by = (int) $m[3];
$current = (int) ( $store[ $post_id ][ $meta_key ]['value'] ?? 0 );
$store[ $post_id ][ $meta_key ] = array(
'value' => (string) ( $current + $by ),
'expires_at' => null,
);
return 1;
}
}
return 0;
}
};