feat(attachments): add pipeline and outbox schema and queries
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: outbox.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createOutboxEntry = `-- name: CreateOutboxEntry :one
|
||||
INSERT INTO outbox (
|
||||
job_id,
|
||||
attachment_id,
|
||||
pipeline_version,
|
||||
attempt,
|
||||
available_at
|
||||
)
|
||||
SELECT
|
||||
job_id,
|
||||
id,
|
||||
pipeline_version,
|
||||
$1::int,
|
||||
$2::timestamptz
|
||||
FROM attachments
|
||||
WHERE id = $3::uuid
|
||||
AND job_id IS NOT NULL
|
||||
RETURNING id, job_id, attachment_id, pipeline_version, attempt, created_at, available_at, published_at
|
||||
`
|
||||
|
||||
type CreateOutboxEntryParams struct {
|
||||
Attempt int
|
||||
AvailableAt time.Time
|
||||
AttachmentID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) CreateOutboxEntry(ctx context.Context, arg CreateOutboxEntryParams) (Outbox, error) {
|
||||
row := q.db.QueryRow(ctx, createOutboxEntry, arg.Attempt, arg.AvailableAt, arg.AttachmentID)
|
||||
var i Outbox
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.JobID,
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempt,
|
||||
&i.CreatedAt,
|
||||
&i.AvailableAt,
|
||||
&i.PublishedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const lockPendingOutboxEntries = `-- name: LockPendingOutboxEntries :many
|
||||
SELECT id, job_id, attachment_id, pipeline_version, attempt, created_at, available_at, published_at FROM outbox
|
||||
WHERE published_at IS NULL
|
||||
AND available_at <= now()
|
||||
ORDER BY available_at, id
|
||||
LIMIT $1::int
|
||||
FOR UPDATE SKIP LOCKED
|
||||
`
|
||||
|
||||
func (q *Queries) LockPendingOutboxEntries(ctx context.Context, batchSize int) ([]Outbox, error) {
|
||||
rows, err := q.db.Query(ctx, lockPendingOutboxEntries, batchSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []Outbox{}
|
||||
for rows.Next() {
|
||||
var i Outbox
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.JobID,
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempt,
|
||||
&i.CreatedAt,
|
||||
&i.AvailableAt,
|
||||
&i.PublishedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const markOutboxPublished = `-- name: MarkOutboxPublished :execrows
|
||||
UPDATE outbox
|
||||
SET published_at = now()
|
||||
WHERE id = $1::bigint
|
||||
AND published_at IS NULL
|
||||
`
|
||||
|
||||
func (q *Queries) MarkOutboxPublished(ctx context.Context, id int64) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, markOutboxPublished, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user