Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
3.6 KB
Raw Blame
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: comment_idempotency.sql

package db

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const claimCommentIdempotencyReceipt = `-- name: ClaimCommentIdempotencyReceipt :execrows
INSERT INTO comment_idempotency_receipt (
    workspace_id, actor_type, actor_id, idempotency_key, issue_id, comment_id, request_fingerprint
) VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (workspace_id, actor_type, actor_id, idempotency_key) DO NOTHING
`

type ClaimCommentIdempotencyReceiptParams struct {
	WorkspaceID        pgtype.UUID `json:"workspace_id"`
	ActorType          string      `json:"actor_type"`
	ActorID            pgtype.UUID `json:"actor_id"`
	IdempotencyKey     string      `json:"idempotency_key"`
	IssueID            pgtype.UUID `json:"issue_id"`
	CommentID          pgtype.UUID `json:"comment_id"`
	RequestFingerprint []byte      `json:"request_fingerprint"`
}

// Runs first in the comment's own transaction. A concurrent request with the
// same key waits on the unique index until the first one commits or rolls
// back; it then claims nothing (0 rows) and replays the first comment, or
// claims the key itself if the first request rolled back.
func (q *Queries) ClaimCommentIdempotencyReceipt(ctx context.Context, arg ClaimCommentIdempotencyReceiptParams) (int64, error) {
	result, err := q.db.Exec(ctx, claimCommentIdempotencyReceipt,
		arg.WorkspaceID,
		arg.ActorType,
		arg.ActorID,
		arg.IdempotencyKey,
		arg.IssueID,
		arg.CommentID,
		arg.RequestFingerprint,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const deleteCommentIdempotencyReceiptsBefore = `-- name: DeleteCommentIdempotencyReceiptsBefore :execrows
DELETE FROM comment_idempotency_receipt
WHERE ctid = ANY(ARRAY(
    SELECT r.ctid FROM comment_idempotency_receipt r
    WHERE r.created_at < $1::timestamptz
    ORDER BY r.created_at
    LIMIT $2::int
))
`

type DeleteCommentIdempotencyReceiptsBeforeParams struct {
	Before    pgtype.Timestamptz `json:"before"`
	BatchSize int32              `json:"batch_size"`
}

// Bounded retention sweep. Receipts only need to outlive a client's retry
// window, so old rows are deleted in batches instead of kept forever.
func (q *Queries) DeleteCommentIdempotencyReceiptsBefore(ctx context.Context, arg DeleteCommentIdempotencyReceiptsBeforeParams) (int64, error) {
	result, err := q.db.Exec(ctx, deleteCommentIdempotencyReceiptsBefore, arg.Before, arg.BatchSize)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const getCommentIdempotencyReceipt = `-- name: GetCommentIdempotencyReceipt :one
SELECT issue_id, comment_id, request_fingerprint FROM comment_idempotency_receipt
WHERE workspace_id = $1 AND actor_type = $2 AND actor_id = $3 AND idempotency_key = $4
`

type GetCommentIdempotencyReceiptParams struct {
	WorkspaceID    pgtype.UUID `json:"workspace_id"`
	ActorType      string      `json:"actor_type"`
	ActorID        pgtype.UUID `json:"actor_id"`
	IdempotencyKey string      `json:"idempotency_key"`
}

type GetCommentIdempotencyReceiptRow struct {
	IssueID            pgtype.UUID `json:"issue_id"`
	CommentID          pgtype.UUID `json:"comment_id"`
	RequestFingerprint []byte      `json:"request_fingerprint"`
}

func (q *Queries) GetCommentIdempotencyReceipt(ctx context.Context, arg GetCommentIdempotencyReceiptParams) (GetCommentIdempotencyReceiptRow, error) {
	row := q.db.QueryRow(ctx, getCommentIdempotencyReceipt,
		arg.WorkspaceID,
		arg.ActorType,
		arg.ActorID,
		arg.IdempotencyKey,
	)
	var i GetCommentIdempotencyReceiptRow
	err := row.Scan(&i.IssueID, &i.CommentID, &i.RequestFingerprint)
	return i, err
}