Download this file
| 1 | package dbdoortest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/benbjohnson/litestream" |
| 10 | lsfile "github.com/benbjohnson/litestream/file" |
| 11 | ) |
| 12 | |
| 13 | // The helper is only worth having if litestream itself can restore what it |
| 14 | // wrote, with every batch present, from more than one L0 file. |
| 15 | func TestReplicateWritesARestorableMultiFileReplica(t *testing.T) { |
| 16 | dir := t.TempDir() |
| 17 | Replicate(t, dir, 3) |
| 18 | c := lsfile.NewReplicaClient(dir) |
| 19 | itr, err := c.LTXFiles(context.Background(), 0, 0, false) |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | n := 0 |
| 24 | for itr.Next() { |
| 25 | n++ |
| 26 | } |
| 27 | itr.Close() |
| 28 | if n < 2 { |
| 29 | t.Fatalf("replica holds %d L0 files, want several", n) |
| 30 | } |
| 31 | out := filepath.Join(t.TempDir(), "out.db") |
| 32 | opt := litestream.NewRestoreOptions() |
| 33 | opt.OutputPath = out |
| 34 | if err := litestream.NewReplicaWithClient(nil, c).Restore(context.Background(), opt); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | db, err := sql.Open("sqlite", "file:"+out+"?mode=ro") |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | defer db.Close() |
| 42 | var rows int |
| 43 | if err := db.QueryRow(`SELECT count(*) FROM t`).Scan(&rows); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | if rows != 3*Rows { |
| 47 | t.Fatalf("restored %d rows, want %d", rows, 3*Rows) |
| 48 | } |
| 49 | } |
| 50 | |