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
@@ -92,8 +92,8 @@ type revenueEvent struct {
// Pre-payment credit notes already lowered the amount paid.
func cashFlows(d *db.DB, currency string) ([]revenueEvent, error) {
var out []revenueEvent
rows, err := d.Query(`SELECT customer, coalesce(json_extract(data, '$.status_transitions.paid_at'), created),
coalesce(json_extract(data, '$.amount_paid'), 0) FROM stripe_objects
rows, err := d.Query(`SELECT customer, coalesce(CAST(json_extract(data, '$.status_transitions.paid_at') AS BIGINT), created),
coalesce(CAST(json_extract(data, '$.amount_paid') AS BIGINT), 0) FROM stripe_objects
WHERE kind = 'invoice' AND status = 'paid' AND currency = ? AND customer != ''`, currency)
if err != nil {
return nil, err
+1 -1
View File
@@ -37,7 +37,7 @@ func addLines(t *testing.T, d *db.DB, ls ...ln) {
t.Fatal(err)
}
}
d.Exec(`INSERT OR IGNORE INTO stripe_objects (kind, id, currency, data) VALUES ('invoice', 'in_x', 'usd', '{}')`)
d.Exec(`INSERT INTO stripe_objects (kind, id, currency, data) VALUES ('invoice', 'in_x', 'usd', '{}') ON CONFLICT DO NOTHING`)
}
// monthly bills a customer $amount a month on the 10th, from one month to