Phase 9, part 2: one database layer for SQLite and Postgres

db.DB wraps database/sql for either database. The SQL stays SQLite's: on
Postgres each statement's ? become $n and LIKE ignores case as SQLite's
does, and the Postgres schema (postgres.sql, schema 29 as a whole) adds
SQLite's instr, json_extract and two-argument max/min, and a nocase ICU
collation. Needs Postgres 18.

LastInsertId became db.Insert (RETURNING id), INSERT OR IGNORE became ON
CONFLICT DO NOTHING, upserts qualify their columns, and three queries
group by every column they use. SUPERMARKET_TEST_POSTGRES runs the test
suite on Postgres, a new schema per test; all tests pass on both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 07:10:44 -07:00
co-authored by Claude Opus 5.5
parent 7a857c7346
commit 1b2947de7c
32 changed files with 1188 additions and 69 deletions
+2 -2
View File
@@ -114,13 +114,13 @@ type Run struct {
const timeFormat = time.RFC3339
func record(d *db.DB, r *Run) error {
res, err := d.Exec(`INSERT INTO enrich_runs (kind, subject, at, found, added, credits, result, error)
newID, err := d.Insert(`INSERT INTO enrich_runs (kind, subject, at, found, added, credits, result, error)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, r.Kind, r.Subject, r.At.UTC().Format(timeFormat),
r.Found, r.Added, r.Credits, r.Result, r.Error)
if err != nil {
return err
}
r.ID, err = res.LastInsertId()
r.ID = newID
return err
}