Files
gochat/channels/shangwutong/db/queries/operations.sql
T

103 lines
3.4 KiB
SQL

-- name: InsertOutboundOperation :one
INSERT INTO outbound_operations (
account_id, swt_sid, event_id, operation, payload, occurred_at
) VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(event_id) DO NOTHING
RETURNING *;
-- name: GetOutboundOperationByEventID :one
SELECT * FROM outbound_operations
WHERE event_id = ?
LIMIT 1;
-- name: ClaimOutboundOperation :one
UPDATE outbound_operations SET
delivery_status = 'delivering',
claimed_at = CURRENT_TIMESTAMP,
attempts = attempts + 1,
updated_at = CURRENT_TIMESTAMP
WHERE id = (
SELECT candidate.id
FROM outbound_operations AS candidate
JOIN accounts AS account ON account.id = candidate.account_id
WHERE candidate.delivery_status = 'pending'
AND account.enabled = 1
AND account.desired_presence <> 'offline'
AND account.deleted_at IS NULL
AND (candidate.next_attempt_at IS NULL OR julianday(candidate.next_attempt_at) IS NULL OR julianday(candidate.next_attempt_at) <= julianday('now'))
AND NOT EXISTS (
SELECT 1 FROM outbound_operations AS earlier
WHERE earlier.account_id = candidate.account_id
AND earlier.id < candidate.id
AND earlier.delivery_status IN ('pending', 'delivering', 'uncertain')
)
AND NOT EXISTS (
SELECT 1 FROM outbound_messages AS active_message
WHERE active_message.account_id = candidate.account_id
AND active_message.delivery_status IN ('delivering', 'uncertain')
)
AND NOT EXISTS (
SELECT 1 FROM outbound_messages AS earlier_message
WHERE earlier_message.account_id = candidate.account_id
AND earlier_message.delivery_status = 'pending'
AND (
julianday(earlier_message.occurred_at) < julianday(candidate.occurred_at) OR
(julianday(earlier_message.occurred_at) = julianday(candidate.occurred_at) AND earlier_message.event_id < candidate.event_id)
)
)
ORDER BY candidate.id
LIMIT 1
)
AND delivery_status = 'pending'
RETURNING *;
-- name: CompleteOutboundOperation :exec
UPDATE outbound_operations SET
delivery_status = 'delivered',
claimed_at = NULL,
last_error = NULL,
updated_at = CURRENT_TIMESTAMP
WHERE id = ? AND delivery_status = 'delivering';
-- name: RetryOutboundOperation :exec
UPDATE outbound_operations SET
delivery_status = 'pending',
claimed_at = NULL,
next_attempt_at = ?,
last_error = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ? AND delivery_status = 'delivering';
-- name: MarkOutboundOperationUncertain :exec
UPDATE outbound_operations SET
delivery_status = 'uncertain',
claimed_at = NULL,
last_error = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ? AND delivery_status = 'delivering';
-- name: FailOutboundOperation :exec
UPDATE outbound_operations SET
delivery_status = 'failed',
claimed_at = NULL,
last_error = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ? AND delivery_status IN ('delivering', 'uncertain');
-- name: RecoverOutboundOperationsAsUncertain :execrows
UPDATE outbound_operations SET
delivery_status = 'uncertain',
claimed_at = NULL,
last_error = 'connector restarted while operation was delivering',
updated_at = CURRENT_TIMESTAMP
WHERE delivery_status = 'delivering';
-- name: FailExpiredUncertainOperations :execrows
UPDATE outbound_operations SET
delivery_status = 'failed',
claimed_at = NULL,
last_error = 'operation result remained uncertain beyond the observation window',
updated_at = CURRENT_TIMESTAMP
WHERE delivery_status = 'uncertain'
AND julianday(updated_at) <= julianday(?);