Files
davidandClaude Opus 5.5 5f1b8d59ce Phase 9, part 7: export and import, between SQLite and Postgres
supermarket export [--with-secrets] writes a whole Supermarket as a
.tar.gz (a manifest, each table as JSON lines, secrets if asked for) to
the file storage. supermarket import loads one into an empty database at
the same schema, SQLite or Postgres, in one transaction, parents first,
checking row counts and moving Postgres' identity counters past the
imported ids.

Checked on Federated's 112,543 rows: imported into Postgres and SQLite,
every table and total matches, and TestPagesOnRealData renders all 57
pages the same from both. That found unsettled ordering (case-alike
campaigns, customers with the same name, metrics' float sums), now fixed
with ORDER BY and tie-breakers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 08:09:14 -07:00

110 lines
4.0 KiB
Go

package web
import (
"io"
"log/slog"
"net/http"
"os"
"regexp"
"strings"
"testing"
"supermarket/internal/db"
"supermarket/internal/secrets"
)
// TestPagesOnRealData opens every page on a database with real data in
// it, such as a local install imported into Postgres (supermarket
// import), and checks each renders. Only run when asked:
//
// SUPERMARKET_TEST_PAGES_DB=postgres://…/federated go test -run TestPagesOnRealData ./internal/web
func TestPagesOnRealData(t *testing.T) {
url := os.Getenv("SUPERMARKET_TEST_PAGES_DB")
if url == "" {
t.Skip("set SUPERMARKET_TEST_PAGES_DB to a database with data")
}
d, err := db.OpenPostgres(url)
if err != nil {
t.Fatal(err)
}
defer d.Close()
sec, err := secrets.OpenDB(d, []byte("0123456789abcdef0123456789abcdef"), nil)
if err != nil {
t.Fatal(err)
}
s := &Server{DB: d, Secrets: sec, DataDir: t.TempDir(), PublicURL: "http://localhost:8470", Version: "test",
Log: slog.New(slog.NewTextHandler(io.Discard, nil))}
h, err := s.Handler()
if err != nil {
t.Fatal(err)
}
pages := []string{"/", "/market", "/market/new", "/posts", "/content", "/content/scheduled", "/content/published",
"/signals", "/signals/new", "/prospects", "/prospects/new", "/outreach", "/prospects/suppressed", "/crm", "/crm/reach",
"/seo/check", "/seo/report", "/seo", "/seo/changes", "/seo/keywords", "/seo/briefs", "/seo/audit", "/seo/engines", "/seo/next", "/seo/links",
"/metrics", "/metrics/spend", "/metrics/clv", "/metrics/customers", "/metrics/publish",
"/sites", "/sites/pages", "/sites/previews", "/sites/publishing", "/sites/forms", "/sites/new",
"/settings/profile", "/settings/accounts", "/settings/core", "/settings/sources", "/settings/email", "/settings/ai",
"/settings/backups", "/settings/search", "/settings/tracking", "/settings/about", "/search?q=federated"}
// Each signal's page too.
rows, err := d.Query(`SELECT id FROM signals`)
if err != nil {
t.Fatal(err)
}
for rows.Next() {
var id string
rows.Scan(&id)
pages = append(pages, "/signals/"+id)
}
rows.Close()
// The same export imported into SQLite: every page should be the same.
var lite http.Handler
if path := os.Getenv("SUPERMARKET_TEST_PAGES_SQLITE"); path != "" {
ld, err := db.OpenSQLite(path)
if err != nil {
t.Fatal(err)
}
defer ld.Close()
lsec, _ := secrets.Open(t.TempDir() + "/secrets.json")
ls := &Server{DB: ld, Secrets: lsec, DataDir: s.DataDir, PublicURL: s.PublicURL, Version: "test", Log: s.Log}
if lite, err = ls.Handler(); err != nil {
t.Fatal(err)
}
}
volatile := regexp.MustCompile(`<code>[0-9a-f]{12}</code>|\b\d+ (seconds?|minutes?) ago\b|just now|nonce="[^"]*"|value="[0-9a-f]{32,}"`)
// Chart coordinates: the same sums, added in another order, differ in
// the 14th decimal place.
long := regexp.MustCompile(`(\d+\.\d{6})\d+`)
for _, p := range pages {
rec := do(h, http.MethodGet, p, nil, nil)
if lite != nil && rec.Code == http.StatusOK && p != "/settings/about" { // About lists the copies running
a := long.ReplaceAllString(volatile.ReplaceAllString(rec.Body.String(), ""), "$1")
b := long.ReplaceAllString(volatile.ReplaceAllString(do(lite, http.MethodGet, p, nil, nil).Body.String(), ""), "$1")
if a != b {
al, bl := strings.Split(a, "\n"), strings.Split(b, "\n")
for i := 0; i < min(len(al), len(bl)); i++ {
if al[i] != bl[i] {
t.Errorf("%s differs at line %d:\n Postgres %s\n SQLite %s", p, i+1, strings.TrimSpace(al[i]), strings.TrimSpace(bl[i]))
break
}
}
if len(al) != len(bl) {
t.Errorf("%s: %d lines on Postgres, %d on SQLite", p, len(al), len(bl))
}
}
}
body := rec.Body.String()
if rec.Code != http.StatusOK || strings.Contains(body, "Something went wrong") || strings.Contains(body, "SQLSTATE") {
i := strings.Index(body, "Something went wrong")
if i < 0 {
i = strings.Index(body, "SQLSTATE")
}
snippet := ""
if i >= 0 {
snippet = body[max(0, i-100):min(len(body), i+300)]
}
t.Errorf("%s: %d %s", p, rec.Code, snippet)
}
}
t.Logf("%d pages", len(pages))
}