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:
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"supermarket/internal/db"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -16,6 +17,9 @@ import (
|
||||
)
|
||||
|
||||
func TestBackupsPage(t *testing.T) {
|
||||
if db.OnPostgres() {
|
||||
t.Skip("SQLite's own backups")
|
||||
}
|
||||
s, h := newTestServer(t, nil)
|
||||
if body := do(h, http.MethodGet, "/settings/backups", nil, nil).Body.String(); !strings.Contains(body, "No backups yet") {
|
||||
t.Errorf("empty: %s", body)
|
||||
@@ -58,9 +62,12 @@ func TestBackupsPage(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRestoreFromThePage(t *testing.T) {
|
||||
if db.OnPostgres() {
|
||||
t.Skip("SQLite's own backups")
|
||||
}
|
||||
s, h := newTestServer(t, nil)
|
||||
s.DB.PutJSON("test:value", "before")
|
||||
b, err := backup.Make(s.DB.DB, s.backupDir(), backup.Manual, "good", time.Now().Add(-time.Hour))
|
||||
b, err := backup.Make(s.DB.SQL, s.backupDir(), backup.Manual, "good", time.Now().Add(-time.Hour))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -89,8 +96,11 @@ func TestRestoreFromThePage(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBackupUpload(t *testing.T) {
|
||||
if db.OnPostgres() {
|
||||
t.Skip("SQLite's own backups")
|
||||
}
|
||||
s, h := newTestServer(t, nil)
|
||||
b, _ := backup.Make(s.DB.DB, t.TempDir(), backup.Manual, "elsewhere", time.Now())
|
||||
b, _ := backup.Make(s.DB.SQL, t.TempDir(), backup.Manual, "elsewhere", time.Now())
|
||||
raw, _ := os.ReadFile(b.Path)
|
||||
upload := func(name string, content []byte) string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
Reference in New Issue
Block a user