Durable staging handoff, transactional outbox, at-least-once worker with lease fencing, idempotent versioned pipeline, three variants, operator tooling, contract tests and evidence. Includes the review fixes: reprocess source selection and failure settlement, replay guards, stale-job retirement, PNG structural/raster validation, migration rollback safety and audit events.
51 lines
1.7 KiB
SQL
51 lines
1.7 KiB
SQL
-- Claims the key for this actor/chat and returns whether this request owns it.
|
|
-- A live record wins: the statement returns nothing and the caller replays the
|
|
-- stored message. An expired record is replaced in place.
|
|
-- name: ClaimMessageIdempotency :one
|
|
INSERT INTO message_idempotency (actor_id, chat_id, key, request_hash, expires_at)
|
|
VALUES (
|
|
sqlc.arg(actor_id),
|
|
sqlc.arg(chat_id),
|
|
sqlc.arg(key),
|
|
sqlc.arg(request_hash),
|
|
sqlc.arg(expires_at)
|
|
)
|
|
ON CONFLICT (actor_id, chat_id, key) DO UPDATE
|
|
SET request_hash = EXCLUDED.request_hash,
|
|
expires_at = EXCLUDED.expires_at,
|
|
message_id = NULL
|
|
WHERE message_idempotency.expires_at <= now()
|
|
RETURNING key;
|
|
|
|
-- Concurrent duplicates block here until the owner commits, then read the
|
|
-- result it stored in the same transaction.
|
|
-- name: GetMessageIdempotency :one
|
|
SELECT message_id, request_hash
|
|
FROM message_idempotency
|
|
WHERE actor_id = sqlc.arg(actor_id)
|
|
AND chat_id = sqlc.arg(chat_id)
|
|
AND key = sqlc.arg(key)
|
|
FOR UPDATE;
|
|
|
|
-- name: CompleteMessageIdempotency :exec
|
|
UPDATE message_idempotency
|
|
SET message_id = sqlc.arg(message_id)
|
|
WHERE actor_id = sqlc.arg(actor_id)
|
|
AND chat_id = sqlc.arg(chat_id)
|
|
AND key = sqlc.arg(key);
|
|
|
|
-- name: GetMessageByID :one
|
|
SELECT id, chat_id, sender_id, text, created_at
|
|
FROM messages
|
|
WHERE id = sqlc.arg(id);
|
|
|
|
-- Read-only replay lookup used before attachment-state validation. A live key
|
|
-- returns the stored message; an expired key is ignored and may be claimed.
|
|
-- name: LookupMessageIdempotency :one
|
|
SELECT message_id, request_hash
|
|
FROM message_idempotency
|
|
WHERE actor_id = sqlc.arg(actor_id)
|
|
AND chat_id = sqlc.arg(chat_id)
|
|
AND key = sqlc.arg(key)
|
|
AND expires_at > now();
|