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:
@@ -742,11 +742,7 @@ func siteChecks(a *Audit, rob *robots, llms *fetched, mapErr error, mapped int)
|
||||
|
||||
// StartAudit records that an audit began.
|
||||
func StartAudit(d *db.DB, site string, now time.Time) (int64, error) {
|
||||
res, err := d.Exec(`INSERT INTO seo_audits (site, status, started_at) VALUES (?, 'running', ?)`, site, now.UTC().Format(time.RFC3339))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.LastInsertId()
|
||||
return d.Insert(`INSERT INTO seo_audits (site, status, started_at) VALUES (?, 'running', ?)`, site, now.UTC().Format(time.RFC3339))
|
||||
}
|
||||
|
||||
// FinishAudit stores an audit's result, or its error.
|
||||
|
||||
@@ -299,13 +299,13 @@ Answer with JSON only, in this shape:
|
||||
if page == "" {
|
||||
b.Path = "/" + slugify(b.Keyword) + "/"
|
||||
}
|
||||
r, err := d.Exec(`INSERT INTO seo_briefs (site, property, keyword, page, status, shown_for, top, plan, answers, path, searches, created_at)
|
||||
newID, err := d.Insert(`INSERT INTO seo_briefs (site, property, keyword, page, status, shown_for, top, plan, answers, path, searches, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, b.Site, b.Property, b.Keyword, b.Page, b.Status, js(b.ShownFor), js(b.Top),
|
||||
js(b.Plan), js(b.Answers), b.Path, b.Searches, now.UTC().Format(time.RFC3339))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b.ID, _ = r.LastInsertId()
|
||||
b.ID = newID
|
||||
return b, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -150,13 +150,13 @@ func ChangesIn(d *db.DB, statuses ...string) ([]Change, error) {
|
||||
}
|
||||
|
||||
func insertChange(d *db.DB, c *Change) error {
|
||||
res, err := d.Exec(`INSERT INTO seo_changes (site, property, page, cur_title, new_title, cur_description, new_description, why,
|
||||
newID, err := d.Insert(`INSERT INTO seo_changes (site, property, page, cur_title, new_title, cur_description, new_description, why,
|
||||
status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, c.Site, c.Property, c.Page, c.CurTitle, c.NewTitle,
|
||||
c.CurDescription, c.NewDescription, c.Why, ChangeProposed, c.CreatedAt.UTC().Format(time.RFC3339))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.ID, err = res.LastInsertId()
|
||||
c.ID = newID
|
||||
c.Status = ChangeProposed
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ func FindKeywords(ctx context.Context, d *db.DB, site, property string, p *profi
|
||||
rows, err := d.Query(`SELECT query, sum(impressions), sum(pos_sum),
|
||||
(SELECT page FROM gsc_query_days q2 WHERE q2.site = q.site AND q2.query = q.query AND q2.date BETWEEN ? AND ?
|
||||
GROUP BY page ORDER BY sum(impressions) DESC LIMIT 1)
|
||||
FROM gsc_query_days q WHERE `+cond+` AND date BETWEEN ? AND ? GROUP BY query
|
||||
FROM gsc_query_days q WHERE `+cond+` AND date BETWEEN ? AND ? GROUP BY site, query
|
||||
HAVING sum(impressions) >= 20 AND sum(pos_sum) / sum(impressions) > 20
|
||||
ORDER BY sum(impressions) DESC LIMIT 200`, append(args, from, to)...)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user