Stop calling the PostgreSQL store SQLite, and stop guessing where it is

Two things that were only ever confusing, and one of them stopped a command
working on the cluster.

The store type was named SQLite and kept the name when PostgreSQL was added, so
OpenPostgres returned a *SQLite and all seventy-odd methods on the production
store were spelled after the database it was not running on. Nothing broke;
people did. Earlier today I added three methods to *SQLite for a table that on
this cluster only ever lives in PostgreSQL, and the name did not once look
wrong. It is a field, which is all it ever was.

`llmgw users` and `llmgw keys` defaulted --store to a SQLite path. On a
deployment whose store is PostgreSQL every one of them failed with "unable to
open database file" while the right answer sat in auth.store in the config file
beside them; the conclusion an operator draws is that the tool is broken, and
they are not far off. They now ask the server's own configuration before
falling back, and the error names which of the three sources chose the address
— the failure was never the path, it was which one picked it.

The single-machine default still applies last, so a deployment with no config
file keeps working with no flags.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5 committed 2026-09-27 12:20:40 +02:00
1 parent 3febb6ef63
commit ebce41e22d
32 files changed
+320 -140

No files matched your search

+54 -34
View File
@@ -15,8 +15,17 @@ import (
_ "modernc.org/sqlite" // the cgo-free driver, so the binary stays static
)
// SQLite is the single-instance store.
type SQLite struct {
// DB is the store, on either dialect.
//
// It was called SQLite, and kept the name after PostgreSQL was added, so
// OpenPostgres returned a *SQLite and every one of the seventy-odd methods on
// the production store was spelled after the database it was not running on.
// Nothing broke; people did. A method written for a table that only ever lives
// in PostgreSQL still read as SQLite code, and the deployment that matters most
// looked like the one nobody uses.
//
// The dialect is a field, which is all it ever was.
type DB struct {
// raw is for the things that are not queries: transactions, pings, close.
raw *sql.DB
dialect Dialect
@@ -41,7 +50,7 @@ type SQLite struct {
// in SQLite, which would silently let the tenancy tree grow orphans. busy_timeout
// turns "database is locked" from an error into a wait, which is what a caller
// wants when the batch writer holds the write lock for a few milliseconds.
func OpenSQLite(ctx context.Context, path string) (*SQLite, error) {
func OpenSQLite(ctx context.Context, path string) (*DB, error) {
dsn := path + "?_pragma=journal_mode(WAL)&_pragma=foreign_keys(1)&_pragma=busy_timeout(5000)&_pragma=synchronous(NORMAL)"
db, err := sql.Open("sqlite", dsn)
if err != nil {
@@ -70,16 +79,16 @@ func OpenSQLite(ctx context.Context, path string) (*SQLite, error) {
return nil, fmt.Errorf("securing %s: %w", f, err)
}
}
return &SQLite{raw: db, dialect: SQLiteDialect, db: bound{c: SQLiteDialect, to: db}}, nil
return &DB{raw: db, dialect: SQLiteDialect, db: bound{c: SQLiteDialect, to: db}}, nil
}
// Close releases the database.
func (s *SQLite) Close() error { return s.raw.Close() }
func (s *DB) Close() error { return s.raw.Close() }
// --- tenancy ---
// PutOrg upserts an organization.
func (s *SQLite) PutOrg(ctx context.Context, o Org) error {
func (s *DB) PutOrg(ctx context.Context, o Org) error {
_, err := s.db.ExecContext(ctx,
`INSERT INTO organizations (id, name) VALUES (?, ?)
ON CONFLICT (id) DO UPDATE SET name = excluded.name`, o.ID, o.Name)
@@ -87,7 +96,7 @@ func (s *SQLite) PutOrg(ctx context.Context, o Org) error {
}
// PutTeam upserts a team.
func (s *SQLite) PutTeam(ctx context.Context, t Team) error {
func (s *DB) PutTeam(ctx context.Context, t Team) error {
_, err := s.db.ExecContext(ctx,
`INSERT INTO teams (id, org_id, name) VALUES (?, ?, ?)
ON CONFLICT (id) DO UPDATE SET org_id = excluded.org_id, name = excluded.name`,
@@ -96,7 +105,7 @@ func (s *SQLite) PutTeam(ctx context.Context, t Team) error {
}
// PutProject upserts a project.
func (s *SQLite) PutProject(ctx context.Context, p Project) error {
func (s *DB) PutProject(ctx context.Context, p Project) error {
_, err := s.db.ExecContext(ctx,
`INSERT INTO projects (id, team_id, name) VALUES (?, ?, ?)
ON CONFLICT (id) DO UPDATE SET team_id = excluded.team_id, name = excluded.name`,
@@ -105,7 +114,7 @@ func (s *SQLite) PutProject(ctx context.Context, p Project) error {
}
// Orgs lists every organization.
func (s *SQLite) Orgs(ctx context.Context) ([]Org, error) {
func (s *DB) Orgs(ctx context.Context) ([]Org, error) {
return queryAll(ctx, s.db, `SELECT id, name FROM organizations ORDER BY name`,
nil, func(rows *sql.Rows) (Org, error) {
var o Org
@@ -114,7 +123,7 @@ func (s *SQLite) Orgs(ctx context.Context) ([]Org, error) {
}
// Teams lists an organization's teams.
func (s *SQLite) Teams(ctx context.Context, orgID string) ([]Team, error) {
func (s *DB) Teams(ctx context.Context, orgID string) ([]Team, error) {
return queryAll(ctx, s.db, `SELECT id, org_id, name FROM teams WHERE org_id = ? ORDER BY name`,
[]any{orgID}, func(rows *sql.Rows) (Team, error) {
var t Team
@@ -123,7 +132,7 @@ func (s *SQLite) Teams(ctx context.Context, orgID string) ([]Team, error) {
}
// Projects lists a team's projects.
func (s *SQLite) Projects(ctx context.Context, teamID string) ([]Project, error) {
func (s *DB) Projects(ctx context.Context, teamID string) ([]Project, error) {
return queryAll(ctx, s.db, `SELECT id, team_id, name FROM projects WHERE team_id = ? ORDER BY name`,
[]any{teamID}, func(rows *sql.Rows) (Project, error) {
var p Project
@@ -138,7 +147,7 @@ const keyColumns = `id, key_hash, key_prefix, name, project_id, band, routing_pr
// CreateKey stores a key. Only its hash is written; the key itself is the
// caller's to show once and then forget.
func (s *SQLite) CreateKey(ctx context.Context, k Key) error {
func (s *DB) CreateKey(ctx context.Context, k Key) error {
if len(k.Hash) == 0 {
return errors.New("store: a key needs a hash")
}
@@ -151,7 +160,7 @@ func (s *SQLite) CreateKey(ctx context.Context, k Key) error {
}
// KeyByHash finds a key by the hash of the presented secret.
func (s *SQLite) KeyByHash(ctx context.Context, hash []byte) (Key, error) {
func (s *DB) KeyByHash(ctx context.Context, hash []byte) (Key, error) {
row := s.db.QueryRowContext(ctx,
`SELECT `+keyColumns+` FROM api_keys WHERE key_hash = ?`, hash)
k, err := scanKey(row)
@@ -162,14 +171,14 @@ func (s *SQLite) KeyByHash(ctx context.Context, hash []byte) (Key, error) {
}
// Keys lists a project's keys, revoked ones included.
func (s *SQLite) Keys(ctx context.Context, projectID string) ([]Key, error) {
func (s *DB) Keys(ctx context.Context, projectID string) ([]Key, error) {
return queryAll(ctx, s.db,
`SELECT `+keyColumns+` FROM api_keys WHERE project_id = ? ORDER BY created_at`,
[]any{projectID}, func(rows *sql.Rows) (Key, error) { return scanKey(rows) })
}
// RevokeKey marks a key unusable without deleting it.
func (s *SQLite) RevokeKey(ctx context.Context, id string, at time.Time) error {
func (s *DB) RevokeKey(ctx context.Context, id string, at time.Time) error {
res, err := s.db.ExecContext(ctx,
`UPDATE api_keys SET revoked_at = ? WHERE id = ? AND revoked_at IS NULL`,
millis(at), id)
@@ -196,7 +205,7 @@ func (s *SQLite) RevokeKey(ctx context.Context, id string, at time.Time) error {
// Batched, and at most once a minute per key, because nothing reads this field
// in real time and writing it per request would put a database write back on
// the request path.
func (s *SQLite) TouchKeys(ctx context.Context, seen map[string]time.Time) error {
func (s *DB) TouchKeys(ctx context.Context, seen map[string]time.Time) error {
if len(seen) == 0 {
return nil
}
@@ -219,7 +228,7 @@ func (s *SQLite) TouchKeys(ctx context.Context, seen map[string]time.Time) error
// --- limits ---
// PutLimits upserts the limits at one scope.
func (s *SQLite) PutLimits(ctx context.Context, l Limits) error {
func (s *DB) PutLimits(ctx context.Context, l Limits) error {
models, err := encodeModels(l.AllowedModels)
if err != nil {
return err
@@ -237,7 +246,7 @@ func (s *SQLite) PutLimits(ctx context.Context, l Limits) error {
}
// LimitsFor reads the limits set at one scope, without inheritance.
func (s *SQLite) LimitsFor(ctx context.Context, scope Scope, id string) (Limits, error) {
func (s *DB) LimitsFor(ctx context.Context, scope Scope, id string) (Limits, error) {
row := s.db.QueryRowContext(ctx,
`SELECT rpm, tpm, budget_micros, budget_period, allowed_models
FROM limits WHERE scope_type = ? AND scope_id = ?`, string(scope), id)
@@ -276,7 +285,7 @@ func (s *SQLite) LimitsFor(ctx context.Context, scope Scope, id string) (Limits,
//
// One query rather than four lookups: the chain is a fixed three-level join, and
// this is called on a cache miss during authentication.
func (s *SQLite) Resolve(ctx context.Context, keyID string) (Effective, error) {
func (s *DB) Resolve(ctx context.Context, keyID string) (Effective, error) {
var e Effective
row := s.db.QueryRowContext(ctx,
`SELECT k.id, k.band, k.routing_profile, p.id, t.id, o.id
@@ -330,7 +339,7 @@ func (s *SQLite) Resolve(ctx context.Context, keyID string) (Effective, error) {
// limits — which is the easy path, and what the "default" project on this
// cluster still is — metered nothing at all, and the first sign would have
// been the bill or the outage.
func (s *SQLite) SetDefaultLimits(rpm, tpm *int) {
func (s *DB) SetDefaultLimits(rpm, tpm *int) {
s.mu.Lock()
defer s.mu.Unlock()
s.defaultRPM, s.defaultTPM = rpm, tpm
@@ -342,7 +351,7 @@ func (s *SQLite) SetDefaultLimits(rpm, tpm *int) {
// each caller that was given no limit of its own — read as a shared pool it
// would become one cluster-wide rate limit that every key in every
// organization competes for, which is not a default, it is an outage.
func (s *SQLite) applyDefaults(e *Effective) {
func (s *DB) applyDefaults(e *Effective) {
s.mu.RLock()
defer s.mu.RUnlock()
own := LimitOwner{Scope: ScopeKey, ID: e.KeyID}
@@ -357,7 +366,7 @@ func (s *SQLite) applyDefaults(e *Effective) {
}
// SpendSinceByScope totals cost per key, with the key's place in the tree.
func (s *SQLite) SpendSinceByScope(ctx context.Context, since time.Time) ([]ScopeSpend, error) {
func (s *DB) SpendSinceByScope(ctx context.Context, since time.Time) ([]ScopeSpend, error) {
rows, err := s.db.QueryContext(ctx,
`SELECT k.id, p.id, t.id, o.id, SUM(u.cost_micros)
FROM usage u
@@ -428,7 +437,7 @@ func (e *Effective) tighten(l Limits) {
// --- accounting ---
// WriteUsage appends a batch of records in one transaction.
func (s *SQLite) WriteUsage(ctx context.Context, records []Usage) error {
func (s *DB) WriteUsage(ctx context.Context, records []Usage) error {
if len(records) == 0 {
return nil
}
@@ -453,7 +462,7 @@ func (s *SQLite) WriteUsage(ctx context.Context, records []Usage) error {
}
// SpendSince totals cost per key since a moment.
func (s *SQLite) SpendSince(ctx context.Context, since time.Time) (map[string]int64, error) {
func (s *DB) SpendSince(ctx context.Context, since time.Time) (map[string]int64, error) {
rows, err := s.db.QueryContext(ctx,
`SELECT key_id, SUM(cost_micros) FROM usage WHERE ts >= ? GROUP BY key_id`, millis(since))
if err != nil {
@@ -478,7 +487,7 @@ func (s *SQLite) SpendSince(ctx context.Context, since time.Time) (map[string]in
// begin starts a transaction and returns it twice: bound for queries, raw for
// commit and rollback. The bound half is what callers must use, so a statement
// inside a transaction is rewritten exactly as one outside it is.
func (s *SQLite) begin(ctx context.Context) (conn, *sql.Tx, error) {
func (s *DB) begin(ctx context.Context) (conn, *sql.Tx, error) {
tx, err := s.raw.BeginTx(ctx, nil)
if err != nil {
return nil, nil, err
@@ -488,7 +497,7 @@ func (s *SQLite) begin(ctx context.Context) (conn, *sql.Tx, error) {
// inTx runs fn in a transaction, with the same placeholder rewriting the
// outside-transaction path gets.
func (s *SQLite) inTx(ctx context.Context, fn func(conn) error) error {
func (s *DB) inTx(ctx context.Context, fn func(conn) error) error {
tx, err := s.raw.BeginTx(ctx, nil)
if err != nil {
return err
@@ -620,14 +629,14 @@ func fromNullMillis(v sql.NullInt64) time.Time {
func nowMillis() int64 { return time.Now().UnixMilli() }
// SQLite satisfies Store.
var _ Store = (*SQLite)(nil)
var _ Store = (*DB)(nil)
// UsageSince summarizes activity per key.
//
// The join brings in the key's name and prefix, which is what a dashboard needs
// to show: a row identified only by an opaque id tells an operator nothing.
// Revoked keys still appear, because their usage still happened.
func (s *SQLite) UsageSince(ctx context.Context, since time.Time) ([]KeyUsage, error) {
func (s *DB) UsageSince(ctx context.Context, since time.Time) ([]KeyUsage, error) {
return queryAll(ctx, s.db, `
SELECT u.key_id,
COALESCE(k.name, ''), COALESCE(k.key_prefix, ''), u.project_id,
@@ -638,7 +647,9 @@ func (s *SQLite) UsageSince(ctx context.Context, since time.Time) ([]KeyUsage, e
FROM usage u
LEFT JOIN api_keys k ON k.id = u.key_id
WHERE u.ts >= ?
GROUP BY u.key_id, u.project_id
-- k.name and k.key_prefix are selected, so PostgreSQL wants them
-- grouped. Both are determined by the key id already in the list.
GROUP BY u.key_id, u.project_id, k.name, k.key_prefix
ORDER BY SUM(u.cost_micros) DESC, COUNT(*) DESC`,
[]any{millis(since)}, func(rows *sql.Rows) (KeyUsage, error) {
var k KeyUsage
@@ -655,7 +666,7 @@ func (s *SQLite) UsageSince(ctx context.Context, since time.Time) ([]KeyUsage, e
// Empty slices are filled in, which a GROUP BY cannot do: a chart with hours
// missing where nothing happened reads as a chart with hours missing from the
// data, and the two mean very different things.
func (s *SQLite) UsageBuckets(ctx context.Context, since time.Time, width time.Duration) ([]Bucket, error) {
func (s *DB) UsageBuckets(ctx context.Context, since time.Time, width time.Duration) ([]Bucket, error) {
if width <= 0 {
width = time.Hour
}
@@ -667,7 +678,11 @@ func (s *SQLite) UsageBuckets(ctx context.Context, since time.Time, width time.D
SUM(prompt_tokens), SUM(completion_tokens), SUM(cost_micros),
AVG(latency_ms)
FROM usage WHERE ts >= ?
GROUP BY ts / ? ORDER BY 1`, ms, ms, millis(since), ms)
-- By ordinal, because PostgreSQL matches a grouped expression to a
-- selected one syntactically: "ts / ?" and "(ts / ?) * ?" are not the
-- same text, so it saw a bare ts and refused. The ordinal is the same
-- expression by construction, and both dialects accept it.
GROUP BY 1 ORDER BY 1`, ms, ms, millis(since))
if err != nil {
return nil, err
}
@@ -728,7 +743,7 @@ func (u UserUsage) Tokens() int64 { return u.Prompt + u.Completion }
// key and a notebook key, and reading three rows to add them up is a chore that
// gets skipped. Keys with no owner — issued by an operator before accounts
// existed — are grouped under an empty user, which is honest rather than tidy.
func (s *SQLite) UsageByUser(ctx context.Context, since time.Time) ([]UserUsage, error) {
func (s *DB) UsageByUser(ctx context.Context, since time.Time) ([]UserUsage, error) {
rows, err := s.db.QueryContext(ctx, `
SELECT COALESCE(k.user_id, ''), COALESCE(us.email, ''), COALESCE(us.name, ''),
COUNT(DISTINCT u.key_id),
@@ -740,7 +755,12 @@ func (s *SQLite) UsageByUser(ctx context.Context, since time.Time) ([]UserUsage,
LEFT JOIN api_keys k ON k.id = u.key_id
LEFT JOIN users us ON us.id = k.user_id
WHERE u.ts >= ?
GROUP BY COALESCE(k.user_id, '')
-- Every selected column that is not an aggregate has to be grouped.
-- SQLite picks an arbitrary row for the rest and carries on; PostgreSQL
-- refuses, so this query worked in tests and returned nothing but an
-- error on the cluster. The email and name are functionally dependent
-- on the user id, so grouping by them changes no result.
GROUP BY COALESCE(k.user_id, ''), COALESCE(us.email, ''), COALESCE(us.name, '')
ORDER BY SUM(u.cost_micros) DESC, COUNT(*) DESC`, millis(since))
if err != nil {
return nil, err
@@ -781,7 +801,7 @@ func (m ModelUsage) Tokens() int64 { return m.Prompt + m.Completion }
// a fallback answers — and "what actually ran" is the question being asked of a
// cluster. The requested name is kept in the same table for anyone who needs to
// ask the other question.
func (s *SQLite) UsageByModel(ctx context.Context, since time.Time) ([]ModelUsage, error) {
func (s *DB) UsageByModel(ctx context.Context, since time.Time) ([]ModelUsage, error) {
rows, err := s.db.QueryContext(ctx, `
SELECT CASE WHEN served_model != '' THEN served_model ELSE model END,
COUNT(*), SUM(prompt_tokens), SUM(completion_tokens),