258 lines
7.8 KiB
Go
258 lines
7.8 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: operations.sql
|
|
|
|
package dbgen
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
const claimOutboundOperation = `-- 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 id, account_id, swt_sid, event_id, operation, payload, occurred_at, delivery_status, claimed_at, attempts, next_attempt_at, last_error, created_at, updated_at
|
|
`
|
|
|
|
func (q *Queries) ClaimOutboundOperation(ctx context.Context) (*OutboundOperation, error) {
|
|
row := q.db.QueryRowContext(ctx, claimOutboundOperation)
|
|
var i OutboundOperation
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.AccountID,
|
|
&i.SwtSid,
|
|
&i.EventID,
|
|
&i.Operation,
|
|
&i.Payload,
|
|
&i.OccurredAt,
|
|
&i.DeliveryStatus,
|
|
&i.ClaimedAt,
|
|
&i.Attempts,
|
|
&i.NextAttemptAt,
|
|
&i.LastError,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const completeOutboundOperation = `-- 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'
|
|
`
|
|
|
|
func (q *Queries) CompleteOutboundOperation(ctx context.Context, id int64) error {
|
|
_, err := q.db.ExecContext(ctx, completeOutboundOperation, id)
|
|
return err
|
|
}
|
|
|
|
const failExpiredUncertainOperations = `-- 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(?)
|
|
`
|
|
|
|
func (q *Queries) FailExpiredUncertainOperations(ctx context.Context, julianday interface{}) (int64, error) {
|
|
result, err := q.db.ExecContext(ctx, failExpiredUncertainOperations, julianday)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected()
|
|
}
|
|
|
|
const failOutboundOperation = `-- 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')
|
|
`
|
|
|
|
type FailOutboundOperationParams struct {
|
|
LastError *string `json:"last_error"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) FailOutboundOperation(ctx context.Context, arg FailOutboundOperationParams) error {
|
|
_, err := q.db.ExecContext(ctx, failOutboundOperation, arg.LastError, arg.ID)
|
|
return err
|
|
}
|
|
|
|
const getOutboundOperationByEventID = `-- name: GetOutboundOperationByEventID :one
|
|
SELECT id, account_id, swt_sid, event_id, operation, payload, occurred_at, delivery_status, claimed_at, attempts, next_attempt_at, last_error, created_at, updated_at FROM outbound_operations
|
|
WHERE event_id = ?
|
|
LIMIT 1
|
|
`
|
|
|
|
func (q *Queries) GetOutboundOperationByEventID(ctx context.Context, eventID string) (*OutboundOperation, error) {
|
|
row := q.db.QueryRowContext(ctx, getOutboundOperationByEventID, eventID)
|
|
var i OutboundOperation
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.AccountID,
|
|
&i.SwtSid,
|
|
&i.EventID,
|
|
&i.Operation,
|
|
&i.Payload,
|
|
&i.OccurredAt,
|
|
&i.DeliveryStatus,
|
|
&i.ClaimedAt,
|
|
&i.Attempts,
|
|
&i.NextAttemptAt,
|
|
&i.LastError,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const insertOutboundOperation = `-- 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 id, account_id, swt_sid, event_id, operation, payload, occurred_at, delivery_status, claimed_at, attempts, next_attempt_at, last_error, created_at, updated_at
|
|
`
|
|
|
|
type InsertOutboundOperationParams struct {
|
|
AccountID int64 `json:"account_id"`
|
|
SwtSid string `json:"swt_sid"`
|
|
EventID string `json:"event_id"`
|
|
Operation string `json:"operation"`
|
|
Payload string `json:"payload"`
|
|
OccurredAt time.Time `json:"occurred_at"`
|
|
}
|
|
|
|
func (q *Queries) InsertOutboundOperation(ctx context.Context, arg InsertOutboundOperationParams) (*OutboundOperation, error) {
|
|
row := q.db.QueryRowContext(ctx, insertOutboundOperation,
|
|
arg.AccountID,
|
|
arg.SwtSid,
|
|
arg.EventID,
|
|
arg.Operation,
|
|
arg.Payload,
|
|
arg.OccurredAt,
|
|
)
|
|
var i OutboundOperation
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.AccountID,
|
|
&i.SwtSid,
|
|
&i.EventID,
|
|
&i.Operation,
|
|
&i.Payload,
|
|
&i.OccurredAt,
|
|
&i.DeliveryStatus,
|
|
&i.ClaimedAt,
|
|
&i.Attempts,
|
|
&i.NextAttemptAt,
|
|
&i.LastError,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const markOutboundOperationUncertain = `-- 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'
|
|
`
|
|
|
|
type MarkOutboundOperationUncertainParams struct {
|
|
LastError *string `json:"last_error"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) MarkOutboundOperationUncertain(ctx context.Context, arg MarkOutboundOperationUncertainParams) error {
|
|
_, err := q.db.ExecContext(ctx, markOutboundOperationUncertain, arg.LastError, arg.ID)
|
|
return err
|
|
}
|
|
|
|
const recoverOutboundOperationsAsUncertain = `-- 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'
|
|
`
|
|
|
|
func (q *Queries) RecoverOutboundOperationsAsUncertain(ctx context.Context) (int64, error) {
|
|
result, err := q.db.ExecContext(ctx, recoverOutboundOperationsAsUncertain)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected()
|
|
}
|
|
|
|
const retryOutboundOperation = `-- 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'
|
|
`
|
|
|
|
type RetryOutboundOperationParams struct {
|
|
NextAttemptAt *time.Time `json:"next_attempt_at"`
|
|
LastError *string `json:"last_error"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) RetryOutboundOperation(ctx context.Context, arg RetryOutboundOperationParams) error {
|
|
_, err := q.db.ExecContext(ctx, retryOutboundOperation, arg.NextAttemptAt, arg.LastError, arg.ID)
|
|
return err
|
|
}
|