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:
@@ -21,7 +21,7 @@ func AddedToList(d *db.DB, contactID, listID int64, list string, confirmed bool,
|
||||
c = 1
|
||||
}
|
||||
_, err := d.Exec(`INSERT INTO list_members (contact_id, list_id, list_name, confirmed, at) VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT (contact_id, list_id) DO UPDATE SET list_name = excluded.list_name, confirmed = max(confirmed, excluded.confirmed)`,
|
||||
ON CONFLICT (contact_id, list_id) DO UPDATE SET list_name = excluded.list_name, confirmed = max(list_members.confirmed, excluded.confirmed)`,
|
||||
contactID, listID, list, c, now.UTC().Format(time.RFC3339))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -167,16 +167,14 @@ func AddCompany(d *db.DB, c Company, now time.Time) (id int64, added bool, err e
|
||||
WHERE id = ?`, c.Domain, c.Website, c.LinkedIn, c.Industry, c.Size, c.Location, c.Why, id)
|
||||
return id, false, err
|
||||
}
|
||||
res, err := d.Exec(`INSERT INTO prospect_companies (name, domain, website, linkedin, industry, size, location, why,
|
||||
newID, err := d.Insert(`INSERT INTO prospect_companies (name, domain, website, linkedin, industry, size, location, why,
|
||||
source, source_detail, found_at, signal_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
c.Name, c.Domain, c.Website, c.LinkedIn, c.Industry, c.Size, c.Location, c.Why, c.Source, c.SourceDetail, formatTime(now),
|
||||
nullID(c.SignalID))
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
if id, err = res.LastInsertId(); err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
id = newID
|
||||
return id, true, found(d, "company", id, c.SignalID, c.Source, "", now)
|
||||
}
|
||||
|
||||
@@ -274,16 +272,14 @@ func AddContact(d *db.DB, c Contact, now time.Time) (id int64, added bool, err e
|
||||
WHERE id = ?`, company, c.Email, c.FirstName, c.LastName, c.Title, c.EmailStatus, c.EmailStatus, c.LinkedIn, c.Phone, id)
|
||||
return id, false, err
|
||||
}
|
||||
res, err := d.Exec(`INSERT INTO prospect_contacts (company_id, first_name, last_name, title, email, email_status,
|
||||
newID, err := d.Insert(`INSERT INTO prospect_contacts (company_id, first_name, last_name, title, email, email_status,
|
||||
linkedin, phone, source, source_detail, found_at, signal_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
company, c.FirstName, c.LastName, c.Title, c.Email, c.EmailStatus, c.LinkedIn, c.Phone, c.Source, c.SourceDetail, formatTime(now),
|
||||
nullID(c.SignalID))
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
if id, err = res.LastInsertId(); err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
id = newID
|
||||
if c.CompanyID == 0 {
|
||||
err = found(d, "contact", id, c.SignalID, c.Source, c.Email, now)
|
||||
}
|
||||
@@ -900,7 +896,7 @@ func Suppress(d *db.DB, kind, value, reason string, now time.Time) error {
|
||||
return err
|
||||
}
|
||||
if _, err := d.Exec(`INSERT INTO suppressions (kind, value, reason) VALUES (?, ?, ?)
|
||||
ON CONFLICT (kind, value) DO UPDATE SET reason = CASE WHEN excluded.reason != '' THEN excluded.reason ELSE reason END`,
|
||||
ON CONFLICT (kind, value) DO UPDATE SET reason = CASE WHEN excluded.reason != '' THEN excluded.reason ELSE suppressions.reason END`,
|
||||
kind, value, strings.TrimSpace(reason)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user