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:
@@ -1878,6 +1878,37 @@ them work are done. S3 storage takes `SUPERMARKET_S3_ENDPOINT`,
|
||||
mail takes `SUPERMARKET_MAIL_URL` (`smtp://` with STARTTLS, or
|
||||
`smtps://`) and `SUPERMARKET_MAIL_FROM`.
|
||||
|
||||
**Part 2 built 2026-11-25** on `phase9`. It took less change to the SQL
|
||||
than planned: rather than helpers in every query, the SQL stays as it is
|
||||
(SQLite's, with `?`), and Postgres is made to understand it.
|
||||
- `db.DB` wraps `database/sql` for either database. On Postgres, each
|
||||
statement's `?`s become `$1, $2…` and `LIKE` ignores case as SQLite's
|
||||
does (`x COLLATE nocase LIKE y`), skipping strings and comments. Go
|
||||
booleans go in as 0 and 1, and times as SQLite's driver writes them.
|
||||
- The Postgres schema (`internal/db/postgres.sql`, schema 29 as a whole,
|
||||
so part of part 3 came early, to have something to test on) adds:
|
||||
- SQLite's `instr`, `json_extract` and two-argument `max`/`min`, as SQL
|
||||
functions
|
||||
- a case-insensitive, ICU-based `nocase` collation, so `COLLATE NOCASE`
|
||||
means the same in both, in columns and in queries
|
||||
- 64-bit integers, identity columns for `INTEGER PRIMARY KEY`, and the
|
||||
tables in an order Postgres accepts
|
||||
|
||||
It needs Postgres 18 or later, for `LIKE` with that collation.
|
||||
- What changed in the packages:
|
||||
- 11 `LastInsertId`s became `db.Insert` (`RETURNING id`)
|
||||
- 4 `INSERT OR IGNORE`s became `ON CONFLICT DO NOTHING`
|
||||
- 4 upserts got table-qualified columns
|
||||
- 3 queries were grouped by every column they use
|
||||
- 2 JSON values are cast to numbers
|
||||
- 1 `strftime` moved to Go
|
||||
- `SUPERMARKET_TEST_POSTGRES` runs the whole test suite on Postgres, a new
|
||||
schema per test. Every test passes on both databases. The tests that
|
||||
test SQLite itself (its migrations) open SQLite with `db.OpenSQLite`;
|
||||
the backup page's tests skip on Postgres.
|
||||
- Left for part 3: migrations after schema 29 on both databases, and what
|
||||
replaces SQLite's backup before a migration.
|
||||
|
||||
Nothing changes for the local user; this makes the rest possible. A
|
||||
commit after each part; the whole test suite runs against both SQLite and
|
||||
Postgres from part 2 on.
|
||||
|
||||
Reference in New Issue
Block a user