Files
OwnCord/Server/db/lockout_queries.go
T
Claude 44323373e3 refactor(server/db): adopt sqlc as the query layer — phase 1 (D2)
Wire the sqlc-generated dbgen package into db.DB so it stops being dead
code (audit A-2026-07-05) and becomes the real, CI-verified query layer.

db.DB now holds a *dbgen.Queries (initialized in Open via dbgen.New).
Query method bodies delegate to it; sqlc owns the SQL text and parameter
binding (make sqlc-verify), while db keeps its stable public API and
domain model types so no caller in api/admin/ws/service changes. The
migration is incremental — a method either delegates to d.q.* or still
runs raw SQL — so both layers are correct during the transition.

Converted domains (now load-bearing through sqlc):
- blocks: BlockUser, UnblockUser, IsBlocked, IsEitherBlocked,
  ListBlockedUsers (added the query to blocks.sql + regenerated).
  Empty ListBlockedUsers now returns []int64{} instead of nil, matching
  the MemStore backend — a latent inconsistency fixed, not a regression.
- lockouts: UpsertLockout, LoadActiveLockouts, CleanupExpiredLockouts,
  DeleteLockout (RFC3339 time formatting/parsing kept in the wrappers).
- roles: GetRoleByID, ListRoles, GetRoleForUser via a shared roleFromGen
  mapper (int64 position/is_default -> int/bool). GetUserWithRole stays
  raw for now.

Remaining domains stay on raw SQL and are tracked in
docs/plans/sqlc-adoption.md; store/ event+plugin SQL is intentionally
excluded (that layer is removed in D3). Decisions doc + audit closure
updated (A-2026-07-05 -> in progress).

Verified: go build ./...; go test -race ./db ./service ./auth ./ws (api
green non-race, race run matches CI's -timeout 20m); make sqlc-verify and
protocol-verify pass with the regenerated output committed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UA17KPvqGBX3XbXYnMf1rA
2026-07-19 15:01:54 +00:00

44 lines
1.2 KiB
Go

package db
import (
"time"
"github.com/owncord/server/db/dbgen"
)
// UpsertLockout inserts or replaces a rate-limit lockout entry.
func (d *DB) UpsertLockout(key string, expiresAt time.Time) error {
return d.q.UpsertLockout(dbCtx(), dbgen.UpsertLockoutParams{
Key: key,
ExpiresAt: expiresAt.UTC().Format(time.RFC3339),
})
}
// LoadActiveLockouts returns all lockouts that have not yet expired as
// parallel slices of keys and expiry times.
func (d *DB) LoadActiveLockouts() (keys []string, expiresAt []time.Time, err error) {
rows, err := d.q.LoadActiveLockouts(dbCtx(), time.Now().UTC().Format(time.RFC3339))
if err != nil {
return nil, nil, err
}
for _, r := range rows {
t, parseErr := time.Parse(time.RFC3339, r.ExpiresAt)
if parseErr != nil {
continue // skip unparseable rows
}
keys = append(keys, r.Key)
expiresAt = append(expiresAt, t)
}
return keys, expiresAt, nil
}
// CleanupExpiredLockouts removes lockout rows whose expiry has passed.
func (d *DB) CleanupExpiredLockouts() error {
return d.q.CleanupExpiredLockouts(dbCtx(), time.Now().UTC().Format(time.RFC3339))
}
// DeleteLockout removes a single lockout entry.
func (d *DB) DeleteLockout(key string) error {
return d.q.DeleteLockout(dbCtx(), key)
}