package dbdoortest import ( "context" "database/sql" "path/filepath" "testing" "github.com/benbjohnson/litestream" lsfile "github.com/benbjohnson/litestream/file" ) // The helper is only worth having if litestream itself can restore what it // wrote, with every batch present, from more than one L0 file. func TestReplicateWritesARestorableMultiFileReplica(t *testing.T) { dir := t.TempDir() Replicate(t, dir, 3) c := lsfile.NewReplicaClient(dir) itr, err := c.LTXFiles(context.Background(), 0, 0, false) if err != nil { t.Fatal(err) } n := 0 for itr.Next() { n++ } itr.Close() if n < 2 { t.Fatalf("replica holds %d L0 files, want several", n) } out := filepath.Join(t.TempDir(), "out.db") opt := litestream.NewRestoreOptions() opt.OutputPath = out if err := litestream.NewReplicaWithClient(nil, c).Restore(context.Background(), opt); err != nil { t.Fatal(err) } db, err := sql.Open("sqlite", "file:"+out+"?mode=ro") if err != nil { t.Fatal(err) } defer db.Close() var rows int if err := db.QueryRow(`SELECT count(*) FROM t`).Scan(&rows); err != nil { t.Fatal(err) } if rows != 3*Rows { t.Fatalf("restored %d rows, want %d", rows, 3*Rows) } }