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:
@@ -239,7 +239,7 @@ func importState(d *db.DB, path string) (int, error) {
|
||||
}
|
||||
n := 0
|
||||
for name, t := range st.LastChecked {
|
||||
if _, err := d.Exec(`INSERT OR IGNORE INTO fetch_state (key, last_checked) VALUES (?, ?)`,
|
||||
if _, err := d.Exec(`INSERT INTO fetch_state (key, last_checked) VALUES (?, ?) ON CONFLICT DO NOTHING`,
|
||||
"inventory:"+name, t.UTC().Format(time.RFC3339)); err != nil {
|
||||
return n, err
|
||||
}
|
||||
@@ -309,7 +309,7 @@ func importDismissed(d *db.DB, path string) (int, error) {
|
||||
if !dismissed {
|
||||
continue
|
||||
}
|
||||
if _, err := d.Exec(`INSERT OR IGNORE INTO dismissed (item_id) VALUES (?)`, id); err != nil {
|
||||
if _, err := d.Exec(`INSERT INTO dismissed (item_id) VALUES (?) ON CONFLICT DO NOTHING`, id); err != nil {
|
||||
return n, err
|
||||
}
|
||||
n++
|
||||
|
||||
Reference in New Issue
Block a user