Files
uptimesebi/internal/store/org_purges.sql.go
T
Sebastian UnterschützandClaude Opus 5.5 8ff4a86371
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m28s
ci / test (pull_request) Successful in 12m39s
ci / build (pull_request) Successful in 16m10s
ci / image (pull_request) Successful in 5m20s
fix(orgs): delete organizations at once and purge their check data later
Deleting an organization removed its check results and aggregates in the
same transaction as the request. With gigabytes of results that took
longer than the request may last, it was cut off and rolled back, so a
large organization could not be deleted at all.

The organization and everything with a foreign key on it now go in the
request; its check data is queued in org_purges. Every API instance
purges the queue a chunk at a time, each chunk in a transaction of its
own, right after a deletion and every minute. Raw results go first, so
the aggregates' refresh does not rebuild what was purged. A lease lets
another instance take over a purge whose instance stopped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-28 14:50:18 +01:00

110 lines
2.7 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: org_purges.sql
package store
import (
"context"
"github.com/google/uuid"
)
const claimOrgPurge = `-- name: ClaimOrgPurge :one
UPDATE org_purges SET leased_until = now() + interval '15 minutes'
WHERE org_id = (
SELECT org_id FROM org_purges WHERE leased_until < now()
ORDER BY requested_at LIMIT 1
FOR UPDATE SKIP LOCKED
)
RETURNING org_id
`
// System scope: leases the longest waiting purge no other instance holds.
func (q *Queries) ClaimOrgPurge(ctx context.Context) (uuid.UUID, error) {
row := q.db.QueryRow(ctx, claimOrgPurge)
var org_id uuid.UUID
err := row.Scan(&org_id)
return org_id, err
}
const countOrgPurges = `-- name: CountOrgPurges :one
SELECT count(*) FROM org_purges
`
func (q *Queries) CountOrgPurges(ctx context.Context) (int64, error) {
row := q.db.QueryRow(ctx, countOrgPurges)
var count int64
err := row.Scan(&count)
return count, err
}
const extendOrgPurge = `-- name: ExtendOrgPurge :exec
UPDATE org_purges SET leased_until = now() + interval '15 minutes' WHERE org_id = $1
`
func (q *Queries) ExtendOrgPurge(ctx context.Context, orgID uuid.UUID) error {
_, err := q.db.Exec(ctx, extendOrgPurge, orgID)
return err
}
const finishOrgPurge = `-- name: FinishOrgPurge :exec
DELETE FROM org_purges WHERE org_id = $1
`
func (q *Queries) FinishOrgPurge(ctx context.Context, orgID uuid.UUID) error {
_, err := q.db.Exec(ctx, finishOrgPurge, orgID)
return err
}
const orgCheckDataChunks = `-- name: OrgCheckDataChunks :many
SELECT chunk::text FROM org_check_data_chunks() AS chunk
`
func (q *Queries) OrgCheckDataChunks(ctx context.Context) ([]string, error) {
rows, err := q.db.Query(ctx, orgCheckDataChunks)
if err != nil {
return nil, err
}
defer rows.Close()
items := []string{}
for rows.Next() {
var chunk string
if err := rows.Scan(&chunk); err != nil {
return nil, err
}
items = append(items, chunk)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const purgeOrgCheckChunk = `-- name: PurgeOrgCheckChunk :one
SELECT purge_org_check_chunk($1::uuid, $2::text)::bigint AS rows
`
type PurgeOrgCheckChunkParams struct {
OrgID uuid.UUID
Chunk string
}
func (q *Queries) PurgeOrgCheckChunk(ctx context.Context, arg PurgeOrgCheckChunkParams) (int64, error) {
row := q.db.QueryRow(ctx, purgeOrgCheckChunk, arg.OrgID, arg.Chunk)
var rows int64
err := row.Scan(&rows)
return rows, err
}
const queueOrgPurge = `-- name: QueueOrgPurge :exec
INSERT INTO org_purges (org_id) VALUES ($1) ON CONFLICT DO NOTHING
`
// System scope: has the check data of a deleted organization purged.
func (q *Queries) QueueOrgPurge(ctx context.Context, orgID uuid.UUID) error {
_, err := q.db.Exec(ctx, queueOrgPurge, orgID)
return err
}