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>
69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// A database from Phase 0 (schema 1) keeps its queued posts as drafts for
|
|
// the platforms they hadn't reached.
|
|
func TestMigratePostsToDrafts(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "app.db")
|
|
old, err := sql.Open("sqlite", "file:"+path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, q := range []string{
|
|
migrations[0],
|
|
`PRAGMA user_version = 1`,
|
|
`INSERT INTO inventory (name, relationship, data) VALUES ('Jitsi', 'competitor', '{}')`,
|
|
`INSERT INTO posts (id, inventory_name, content, source_url, generated_at, posted_to) VALUES
|
|
('a', 'Jitsi', 'Posted to X only', 'https://x.com/1', '2026-11-01T10:00:00Z', '["x"]'),
|
|
('b', 'Nextcloud', 'Not posted', '', '2026-11-02T10:00:00Z', '[]'),
|
|
('c', 'Nextcloud', 'Posted everywhere', '', '2026-11-03T10:00:00Z', '["x","linkedin"]')`,
|
|
} {
|
|
if _, err := old.Exec(q); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
old.Close()
|
|
|
|
d, err := OpenSQLite(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer d.Close()
|
|
|
|
rows, err := d.Query(`SELECT batch, inventory_name, kind, channel, content, status FROM drafts ORDER BY batch, channel`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer rows.Close()
|
|
var got []string
|
|
for rows.Next() {
|
|
var batch, name, kind, channel, content, status string
|
|
if err := rows.Scan(&batch, &name, &kind, &channel, &content, &status); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got = append(got, batch+" "+name+" "+kind+" "+channel+" "+status)
|
|
}
|
|
want := []string{
|
|
"a Jitsi competitor linkedin draft",
|
|
"b Nextcloud partner linkedin draft",
|
|
"b Nextcloud partner x draft",
|
|
}
|
|
if len(got) != len(want) {
|
|
t.Fatalf("drafts = %q, want %q", got, want)
|
|
}
|
|
for i := range want {
|
|
if got[i] != want[i] {
|
|
t.Errorf("draft %d = %q, want %q", i, got[i], want[i])
|
|
}
|
|
}
|
|
var n int
|
|
if err := d.QueryRow(`SELECT count(*) FROM sqlite_master WHERE name = 'posts'`).Scan(&n); err != nil || n != 0 {
|
|
t.Errorf("posts table still there (%d, %v)", n, err)
|
|
}
|
|
}
|