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>
This commit is contained in:
2026-11-25 08:09:14 -07:00
co-authored by Claude Opus 5.5
parent c6c4c40600
commit 5f1b8d59ce
14 changed files with 1098 additions and 8 deletions
+10
View File
@@ -245,6 +245,14 @@ File storage (`internal/storage`) is tested on a folder, and on an
S3-compatible bucket when `SUPERMARKET_TEST_S3` names one, such as a Garage in
Docker: `SUPERMARKET_TEST_S3='http://ACCESS_KEY:SECRET_KEY@127.0.0.1:3900/bucket'`.
To check every page renders the same from real data on both databases:
`supermarket export`, import the file into a new Postgres database and a new
SQLite one (`supermarket import`, with each edition's settings), then
`SUPERMARKET_TEST_PAGES_DB=postgres://… SUPERMARKET_TEST_PAGES_SQLITE=…/app.db
go test -run TestPagesOnRealData ./internal/web`. Queries whose rows are
summed or shown in order need an `ORDER BY` that settles every tie: the two
databases break ties differently.
**Changing the schema.** Add a `db.Migration` to `both` in
`internal/db/migrations.go`, written in SQLite's SQL. Postgres gets the same
statements translated (`INTEGER` → `BIGINT`, `INTEGER PRIMARY KEY` → an identity
@@ -262,6 +270,8 @@ together), and each is recorded in `schema_migrations`.
| `cmd/supermarket` | The server |
| `internal/config` | The edition (local, server, multi-tenant) and its settings |
| `internal/db` | The database: SQLite or Postgres, the schema and its migrations |
| `internal/transfer` | Export and import: a whole Supermarket as a `.tar.gz`, between SQLite and Postgres |
| `internal/jobs` | Locks shared by copies of the app, so only one does each job |
| `internal/storage` | Files that must last (exports, uploads): a folder, or an S3 bucket (Garage) |
| `internal/web` | Pages (Go templates + [htmx](https://htmx.org), no JavaScript build) |
| `internal/profile`, `internal/inventory` | The company profile and the inventory |