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:
@@ -211,7 +211,7 @@ func Pages(d *db.DB, site string) ([]SitePage, error) {
|
||||
// SignalPages lists a buying signal's pages, on every site, oldest first
|
||||
// (so variants follow the page they're made from).
|
||||
func SignalPages(d *db.DB, signal int64) ([]SitePage, error) {
|
||||
rows, err := d.Query(`SELECT `+pageCols+` FROM site_pages WHERE json_extract(source, '$.signal') = ? ORDER BY id`, signal)
|
||||
rows, err := d.Query(`SELECT `+pageCols+` FROM site_pages WHERE CAST(json_extract(source, '$.signal') AS BIGINT) = ? ORDER BY id`, signal)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -274,7 +274,7 @@ func SavePage(d *db.DB, p *SitePage) error {
|
||||
if p.Status == "" {
|
||||
p.Status = PageDraft
|
||||
}
|
||||
res, err := d.Exec(`INSERT INTO site_pages (site, repo, kind, source, notes, path, title, description, sections, needs, warnings,
|
||||
newID, err := d.Insert(`INSERT INTO site_pages (site, repo, kind, source, notes, path, title, description, sections, needs, warnings,
|
||||
claims, status, branch, file, pr_number, pr_url, note, error, created_at, updated_at, merged_at,
|
||||
removal_branch, removal_pr_number, removal_pr_url, live_at, gone_at, checked_at, live_note)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
@@ -285,7 +285,7 @@ func SavePage(d *db.DB, p *SitePage) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.ID, err = res.LastInsertId()
|
||||
p.ID = newID
|
||||
return err
|
||||
}
|
||||
res, err := d.Exec(`UPDATE site_pages SET site = ?, repo = ?, kind = ?, source = ?, notes = ?, path = ?, title = ?, description = ?,
|
||||
|
||||
Reference in New Issue
Block a user