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
+3 -3
View File
@@ -94,7 +94,7 @@ func cashFlows(d *db.DB, currency string) ([]revenueEvent, error) {
var out []revenueEvent
rows, err := d.Query(`SELECT customer, coalesce(CAST(json_extract(data, '$.status_transitions.paid_at') AS BIGINT), created),
coalesce(CAST(json_extract(data, '$.amount_paid') AS BIGINT), 0) FROM stripe_objects
WHERE kind = 'invoice' AND status = 'paid' AND currency = ? AND customer != ''`, currency)
WHERE kind = 'invoice' AND status = 'paid' AND currency = ? AND customer != '' ORDER BY id`, currency)
if err != nil {
return nil, err
}
@@ -110,7 +110,7 @@ func cashFlows(d *db.DB, currency string) ([]revenueEvent, error) {
}
rows.Close()
rows, err = d.Query(`SELECT customer, created, amount FROM stripe_objects
WHERE kind = 'refund' AND status = 'succeeded' AND currency = ? AND customer != ''`, currency)
WHERE kind = 'refund' AND status = 'succeeded' AND currency = ? AND customer != '' ORDER BY id`, currency)
if err != nil {
return nil, err
}
@@ -125,7 +125,7 @@ func cashFlows(d *db.DB, currency string) ([]revenueEvent, error) {
}
rows.Close()
rows, err = d.Query(`SELECT customer, created, data FROM stripe_objects
WHERE kind = 'credit_note' AND status = 'issued' AND currency = ? AND customer != ''`, currency)
WHERE kind = 'credit_note' AND status = 'issued' AND currency = ? AND customer != '' ORDER BY id`, currency)
if err != nil {
return nil, err
}
+4 -1
View File
@@ -266,7 +266,10 @@ func Matches(d *db.DB, model *Model) ([]Match, error) {
case a.MRR < b.MRR:
return 1
}
return strings.Compare(strings.ToLower(a.Customer.Name+a.Customer.Email), strings.ToLower(b.Customer.Name+b.Customer.Email))
if c := strings.Compare(strings.ToLower(a.Customer.Name+a.Customer.Email), strings.ToLower(b.Customer.Name+b.Customer.Email)); c != 0 {
return c
}
return strings.Compare(a.Customer.ID, b.Customer.ID) // the same name and email twice: a stable order
})
return out, nil
}
+1 -1
View File
@@ -51,7 +51,7 @@ func (l line) monthly() float64 {
func loadLines(d *db.DB, currency string) ([]line, error) {
rows, err := d.Query(`SELECT customer, amount, period_start, period_end, interval, interval_count, proration, product
FROM stripe_invoice_lines WHERE recurring = 1 AND status IN ('paid', 'open') AND currency = ? AND customer != ''
AND period_end > period_start`, currency)
AND period_end > period_start ORDER BY invoice, id`, currency) // a fixed order: float sums come out the same everywhere
if err != nil {
return nil, err
}