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:
@@ -0,0 +1,34 @@
|
||||
package transfer
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// rewrite replaces one file in an export.
|
||||
func rewrite(t *testing.T, export []byte, name, body string) []byte {
|
||||
t.Helper()
|
||||
var out bytes.Buffer
|
||||
gz := gzip.NewWriter(&out)
|
||||
tw := tar.NewWriter(gz)
|
||||
err := walk(bytes.NewReader(export), func(n string, r io.Reader) error {
|
||||
raw, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == name {
|
||||
raw = []byte(body)
|
||||
}
|
||||
return addFile(tw, n, raw, time.Now())
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tw.Close()
|
||||
gz.Close()
|
||||
return out.Bytes()
|
||||
}
|
||||
Reference in New Issue
Block a user