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:
@@ -223,9 +223,29 @@ go test ./...
|
||||
SUPERMARKET_DATA=/tmp/cm SUPERMARKET_ADDR=127.0.0.1:8470 go run ./cmd/supermarket
|
||||
```
|
||||
|
||||
The tests also run on Postgres (the server and multi-tenant editions'
|
||||
database), each test in a new schema that's dropped afterwards. Postgres 18
|
||||
or later is needed:
|
||||
|
||||
```
|
||||
docker run -d --name sm-pgtest --rm -e POSTGRES_PASSWORD=test -e POSTGRES_DB=sm \
|
||||
-p 127.0.0.1:55432:5432 --tmpfs /var/lib/postgresql postgres:18-alpine
|
||||
SUPERMARKET_TEST_POSTGRES='postgres://postgres:test@127.0.0.1:55432/sm?sslmode=disable' go test ./...
|
||||
```
|
||||
|
||||
The SQL is written for SQLite, with `?` parameters. On Postgres, `internal/db`
|
||||
numbers the parameters and makes `LIKE` ignore case as SQLite's does, and
|
||||
the Postgres schema (`internal/db/postgres.sql`) adds SQLite's `instr`,
|
||||
`json_extract` and two-argument `max`/`min`, plus a case-insensitive `nocase`
|
||||
collation. New SQL should run on both: use `db.Insert` (not `LastInsertId`),
|
||||
`ON CONFLICT … DO NOTHING` (not `INSERT OR IGNORE`), table-qualified columns
|
||||
in an upsert's `DO UPDATE`, and every selected column in `GROUP BY`.
|
||||
|
||||
| Path | What |
|
||||
|------|------|
|
||||
| `cmd/supermarket` | The server |
|
||||
| `internal/config` | The edition (local, server, multi-tenant) and its settings |
|
||||
| `internal/db` | The database: SQLite or Postgres, the schema and its migrations |
|
||||
| `internal/web` | Pages (Go templates + [htmx](https://htmx.org), no JavaScript build) |
|
||||
| `internal/profile`, `internal/inventory` | The company profile and the inventory |
|
||||
| `internal/ai` | AI providers: Anthropic (official Go SDK) and OpenAI-compatible servers |
|
||||
|
||||
Reference in New Issue
Block a user