Route every query through a dialect, before there is a second one

Groundwork for PostgreSQL, with no PostgreSQL in it yet: the store still opens
SQLite and behaves exactly as it did.

One set of queries rather than one per database. The SQL here is almost
entirely portable — ON CONFLICT upserts work in both, and the only construct
that genuinely differs is the placeholder — so two copies of every query would
buy nothing and drift, one query at a time, discovered in production.

The rewrite is a wrapper around the handle rather than a change at each call
site. There are sixty of those and sixty chances to forget one; this way the
only route to the driver goes through the rewrite. Making it a wrapper found
six places that reached past it, including all three manual transactions and
two prepared statements — every one of which would have worked on SQLite and
failed on PostgreSQL, on a path nobody exercises before deploying.

The rewriter skips quoted sections. No query here contains a question mark in
a literal today; this is so that adding one is a test failure rather than a
statement that silently binds the wrong argument.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5 committed 2026-09-25 16:10:35 +02:00
1 parent 539bdac152
commit ccc9aa8b76
8 files changed
+194 -27

No files matched your search

+3 -3
View File
@@ -92,7 +92,7 @@ func (s *SQLite) ConsumeClaim(ctx context.Context, token string) (string, error)
hash := HashKey(token)
now := time.Now()
tx, err := s.db.BeginTx(ctx, nil)
q, tx, err := s.begin(ctx)
if err != nil {
return "", err
}
@@ -100,7 +100,7 @@ func (s *SQLite) ConsumeClaim(ctx context.Context, token string) (string, error)
var sealed []byte
var expires int64
err = tx.QueryRowContext(ctx,
err = q.QueryRowContext(ctx,
`SELECT sealed, expires_at FROM key_claims WHERE token_hash = ? AND claimed_at IS NULL`,
hash).Scan(&sealed, &expires)
if errors.Is(err, sql.ErrNoRows) {
@@ -120,7 +120,7 @@ func (s *SQLite) ConsumeClaim(ctx context.Context, token string) (string, error)
// Blank the sealed value as well as marking it claimed, so a used link
// leaves nothing behind even before the row is purged.
res, err := tx.ExecContext(ctx,
res, err := q.ExecContext(ctx,
`UPDATE key_claims SET claimed_at = ?, sealed = x'' WHERE token_hash = ? AND claimed_at IS NULL`,
millis(now), hash)
if err != nil {
+4 -4
View File
@@ -67,7 +67,7 @@ func TestTheDatabaseNeverHoldsAUsableKey(t *testing.T) {
s, token, secret := claimFixture(t)
var sealed []byte
if err := s.db.QueryRow(`SELECT sealed FROM key_claims`).Scan(&sealed); err != nil {
if err := s.db.QueryRowContext(context.Background(), `SELECT sealed FROM key_claims`).Scan(&sealed); err != nil {
t.Fatal(err)
}
if len(sealed) == 0 {
@@ -79,7 +79,7 @@ func TestTheDatabaseNeverHoldsAUsableKey(t *testing.T) {
// And the token itself is not there either, so nothing in the database can
// derive the key that opens it.
var count int
if err := s.db.QueryRow(`SELECT COUNT(*) FROM key_claims WHERE token_hash = ?`, []byte(token)).
if err := s.db.QueryRowContext(context.Background(), `SELECT COUNT(*) FROM key_claims WHERE token_hash = ?`, []byte(token)).
Scan(&count); err != nil {
t.Fatal(err)
}
@@ -98,7 +98,7 @@ func TestAClaimedLinkLeavesNothingBehind(t *testing.T) {
t.Fatal(err)
}
var sealed []byte
if err := s.db.QueryRow(`SELECT sealed FROM key_claims`).Scan(&sealed); err != nil {
if err := s.db.QueryRowContext(context.Background(), `SELECT sealed FROM key_claims`).Scan(&sealed); err != nil {
t.Fatal(err)
}
if len(sealed) != 0 {
@@ -149,7 +149,7 @@ func TestAnExpiredLinkIsRefused(t *testing.T) {
// Age it directly. CreateClaim treats a non-positive TTL as "use the
// default", which is the right thing for a caller and no way to build the
// row this test needs.
if _, err := s.db.Exec(`UPDATE key_claims SET expires_at = ?`,
if _, err := s.db.ExecContext(context.Background(), `UPDATE key_claims SET expires_at = ?`,
time.Now().Add(-time.Hour).UnixMilli()); err != nil {
t.Fatal(err)
}
+103
View File
@@ -0,0 +1,103 @@
package store
import (
"context"
"database/sql"
"strconv"
"strings"
)
// Dialect is which SQL database is underneath.
//
// There is one set of queries, not one per database, because the SQL this
// store writes is almost entirely portable — the only construct that genuinely
// differs is the placeholder, and `ON CONFLICT` upserts work in both. Two
// copies of every query would drift, and the drift would be discovered one
// query at a time in production.
type Dialect string
// The supported databases.
const (
SQLiteDialect Dialect = "sqlite"
PostgresDialect Dialect = "postgres"
)
// rebind rewrites `?` placeholders for the dialect in use.
//
// SQLite takes `?`, PostgreSQL takes `$1`, `$2` and so on. Queries are written
// with `?` because that is what the majority of them were written with
// already, and rewriting is cheaper and safer than maintaining two spellings
// of each.
//
// A `?` inside a string literal would be rewritten too, which would be a bug —
// so this skips over quoted sections rather than scanning blindly. No query
// here contains one today; this exists so that adding one is not a silent
// corruption.
func (d Dialect) rebind(query string) string {
if d != PostgresDialect || !strings.Contains(query, "?") {
return query
}
var b strings.Builder
b.Grow(len(query) + 8)
n := 0
inSingle, inDouble := false, false
for i := 0; i < len(query); i++ {
c := query[i]
switch {
case c == '\'' && !inDouble:
inSingle = !inSingle
case c == '"' && !inSingle:
inDouble = !inDouble
case c == '?' && !inSingle && !inDouble:
n++
b.WriteByte('$')
b.WriteString(strconv.Itoa(n))
continue
}
b.WriteByte(c)
}
return b.String()
}
// conn is the part of *sql.DB and *sql.Tx this store uses.
//
// Named so the same query helpers work inside and outside a transaction, and
// so every path goes through the rebinding wrapper rather than some of them
// reaching the driver directly with the wrong placeholders.
type conn interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
// PrepareContext is here because a prepared statement carries the query
// with it: rebinding at Exec time would be too late, the placeholders are
// fixed when it is prepared.
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}
// bound is a conn that rewrites placeholders on the way through.
//
// A wrapper rather than a change at each call site: there are sixty of those,
// and sixty chances to forget one. Here the only way to reach the driver is
// through the rewrite.
type bound struct {
c Dialect
// to is the underlying *sql.DB or *sql.Tx.
to conn
}
func (b bound) ExecContext(ctx context.Context, q string, args ...any) (sql.Result, error) {
return b.to.ExecContext(ctx, b.c.rebind(q), args...)
}
func (b bound) QueryContext(ctx context.Context, q string, args ...any) (*sql.Rows, error) {
return b.to.QueryContext(ctx, b.c.rebind(q), args...)
}
func (b bound) QueryRowContext(ctx context.Context, q string, args ...any) *sql.Row {
return b.to.QueryRowContext(ctx, b.c.rebind(q), args...)
}
func (b bound) PrepareContext(ctx context.Context, q string) (*sql.Stmt, error) {
return b.to.PrepareContext(ctx, b.c.rebind(q))
}
+44
View File
@@ -0,0 +1,44 @@
package store
import "testing"
func TestSQLiteKeepsItsPlaceholders(t *testing.T) {
q := "SELECT a FROM t WHERE b = ? AND c = ?"
if got := SQLiteDialect.rebind(q); got != q {
t.Errorf("sqlite query was rewritten: %s", got)
}
}
func TestPostgresGetsNumberedPlaceholders(t *testing.T) {
got := PostgresDialect.rebind("SELECT a FROM t WHERE b = ? AND c = ?")
want := "SELECT a FROM t WHERE b = $1 AND c = $2"
if got != want {
t.Errorf("rebind = %q, want %q", got, want)
}
}
func TestAQuestionMarkInsideALiteralIsLeftAlone(t *testing.T) {
// No query here contains one today. This exists so that adding one is a
// test failure rather than a query that silently reads the wrong argument
// — or, worse, one that still parses and returns the wrong rows.
got := PostgresDialect.rebind(`SELECT a FROM t WHERE name = 'why?' AND b = ?`)
want := `SELECT a FROM t WHERE name = 'why?' AND b = $1`
if got != want {
t.Errorf("rebind = %q, want %q", got, want)
}
}
func TestDoubleQuotedIdentifiersAreAlsoSkipped(t *testing.T) {
got := PostgresDialect.rebind(`SELECT "odd?name" FROM t WHERE b = ?`)
want := `SELECT "odd?name" FROM t WHERE b = $1`
if got != want {
t.Errorf("rebind = %q, want %q", got, want)
}
}
func TestAQueryWithNoPlaceholdersIsUntouched(t *testing.T) {
q := "DELETE FROM t"
if got := PostgresDialect.rebind(q); got != q {
t.Errorf("rebind = %q", got)
}
}
+4 -4
View File
@@ -138,14 +138,14 @@ func (s *SQLite) PutNote(ctx context.Context, projectID, agentID string, n Note)
return ErrMemoryNotEnabled
}
tx, err := s.db.BeginTx(ctx, nil)
q, tx, err := s.begin(ctx)
if err != nil {
return err
}
defer func() { _ = tx.Rollback() }()
now := time.Now().UnixMilli()
res, err := tx.ExecContext(ctx,
res, err := q.ExecContext(ctx,
`UPDATE memory_notes SET text = ?, tags = ?, updated_at = ?
WHERE project_id = ? AND agent_id = ? AND key = ?`,
n.Text, tags, now, projectID, agentID, n.Key)
@@ -154,7 +154,7 @@ func (s *SQLite) PutNote(ctx context.Context, projectID, agentID string, n Note)
}
if updated, _ := res.RowsAffected(); updated == 0 {
var count int
if err := tx.QueryRowContext(ctx,
if err := q.QueryRowContext(ctx,
`SELECT COUNT(*) FROM memory_notes WHERE project_id = ? AND agent_id = ?`,
projectID, agentID).Scan(&count); err != nil {
return err
@@ -162,7 +162,7 @@ func (s *SQLite) PutNote(ctx context.Context, projectID, agentID string, n Note)
if count >= MaxNotesPerAgent {
return ErrMemoryFull
}
if _, err := tx.ExecContext(ctx,
if _, err := q.ExecContext(ctx,
`INSERT INTO memory_notes (project_id, agent_id, key, text, tags, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)`,
projectID, agentID, n.Key, n.Text, tags, now, now); err != nil {
+29 -9
View File
@@ -17,7 +17,14 @@ import (
// SQLite is the single-instance store.
type SQLite struct {
db *sql.DB
// raw is for the things that are not queries: transactions, pings, close.
raw *sql.DB
dialect Dialect
// db is the same handle with placeholders rewritten for the dialect. Every
// query goes through it, which is the point — a call site that reached the
// driver directly would work on SQLite and fail on PostgreSQL, and only on
// the path nobody exercised before deploying.
db bound
// mu guards the default limits, which are set once at wiring but read on
// every authentication.
@@ -63,11 +70,11 @@ func OpenSQLite(ctx context.Context, path string) (*SQLite, error) {
return nil, fmt.Errorf("securing %s: %w", f, err)
}
}
return &SQLite{db: db}, nil
return &SQLite{raw: db, dialect: SQLiteDialect, db: bound{c: SQLiteDialect, to: db}}, nil
}
// Close releases the database.
func (s *SQLite) Close() error { return s.db.Close() }
func (s *SQLite) Close() error { return s.raw.Close() }
// --- tenancy ---
@@ -193,7 +200,7 @@ func (s *SQLite) TouchKeys(ctx context.Context, seen map[string]time.Time) error
if len(seen) == 0 {
return nil
}
return s.inTx(ctx, func(tx *sql.Tx) error {
return s.inTx(ctx, func(tx conn) error {
stmt, err := tx.PrepareContext(ctx,
`UPDATE api_keys SET last_used_at = ? WHERE id = ? AND (last_used_at IS NULL OR last_used_at < ?)`)
if err != nil {
@@ -378,7 +385,7 @@ func (s *SQLite) WriteUsage(ctx context.Context, records []Usage) error {
if len(records) == 0 {
return nil
}
return s.inTx(ctx, func(tx *sql.Tx) error {
return s.inTx(ctx, func(tx conn) error {
stmt, err := tx.PrepareContext(ctx,
`INSERT INTO usage (ts, key_id, project_id, model, served_model,
prompt_tokens, completion_tokens, cost_micros, status, latency_ms)
@@ -421,13 +428,26 @@ func (s *SQLite) SpendSince(ctx context.Context, since time.Time) (map[string]in
// --- helpers ---
func (s *SQLite) inTx(ctx context.Context, fn func(*sql.Tx) error) error {
tx, err := s.db.BeginTx(ctx, nil)
// 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) {
tx, err := s.raw.BeginTx(ctx, nil)
if err != nil {
return nil, nil, err
}
return bound{c: s.dialect, to: tx}, tx, nil
}
// 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 {
tx, err := s.raw.BeginTx(ctx, nil)
if err != nil {
return err
}
defer func() { _ = tx.Rollback() }()
if err := fn(tx); err != nil {
if err := fn(bound{c: s.dialect, to: tx}); err != nil {
return err
}
return tx.Commit()
@@ -453,7 +473,7 @@ func scanKey(sc scanner) (Key, error) {
return k, nil
}
func queryAll[T any](ctx context.Context, db *sql.DB, query string, args []any,
func queryAll[T any](ctx context.Context, db conn, query string, args []any,
scan func(*sql.Rows) (T, error),
) ([]T, error) {
rows, err := db.QueryContext(ctx, query, args...)
+1 -1
View File
@@ -85,7 +85,7 @@ func TestTheKeyItselfIsNeverStored(t *testing.T) {
project := tree(t, s)
secret, _ := mustKey(t, s, "key1", project)
rows, err := s.db.Query(`SELECT id, key_hash, key_prefix, name FROM api_keys`)
rows, err := s.db.QueryContext(context.Background(), `SELECT id, key_hash, key_prefix, name FROM api_keys`)
if err != nil {
t.Fatal(err)
}
+6 -6
View File
@@ -137,7 +137,7 @@ func (s *SQLite) AppendThread(ctx context.Context, projectID, threadID string, m
return ErrThreadsNotEnabled
}
tx, err := s.db.BeginTx(ctx, nil)
q, tx, err := s.begin(ctx)
if err != nil {
return err
}
@@ -145,10 +145,10 @@ func (s *SQLite) AppendThread(ctx context.Context, projectID, threadID string, m
now := time.Now().UnixMilli()
var owner string
err = tx.QueryRowContext(ctx, `SELECT project_id FROM threads WHERE id = ?`, threadID).Scan(&owner)
err = q.QueryRowContext(ctx, `SELECT project_id FROM threads WHERE id = ?`, threadID).Scan(&owner)
switch {
case errors.Is(err, sql.ErrNoRows):
if _, err := tx.ExecContext(ctx,
if _, err := q.ExecContext(ctx,
`INSERT INTO threads (id, project_id, created_at, updated_at) VALUES (?, ?, ?, ?)`,
threadID, projectID, now, now); err != nil {
return err
@@ -163,7 +163,7 @@ func (s *SQLite) AppendThread(ctx context.Context, projectID, threadID string, m
}
var next int64
if err := tx.QueryRowContext(ctx,
if err := q.QueryRowContext(ctx,
`SELECT COALESCE(MAX(seq), -1) + 1 FROM thread_messages WHERE thread_id = ?`,
threadID).Scan(&next); err != nil {
return err
@@ -173,14 +173,14 @@ func (s *SQLite) AppendThread(ctx context.Context, projectID, threadID string, m
if m.Model != "" {
model = m.Model
}
if _, err := tx.ExecContext(ctx,
if _, err := q.ExecContext(ctx,
`INSERT INTO thread_messages (thread_id, seq, role, content, model) VALUES (?, ?, ?, ?, ?)`,
threadID, next+int64(i), m.Role, m.Content, model); err != nil {
return err
}
}
// Touched on every append, so retention measures idleness rather than age.
if _, err := tx.ExecContext(ctx,
if _, err := q.ExecContext(ctx,
`UPDATE threads SET updated_at = ? WHERE id = ?`, now, threadID); err != nil {
return err
}