|
| 1 | +//go:build vfs |
| 2 | +// +build vfs |
| 3 | + |
| 4 | +package main_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "database/sql" |
| 9 | + "fmt" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + _ "github.com/mattn/go-sqlite3" |
| 15 | + "github.com/psanford/sqlite3vfs" |
| 16 | + |
| 17 | + "github.com/benbjohnson/litestream" |
| 18 | + "github.com/benbjohnson/litestream/file" |
| 19 | + "github.com/benbjohnson/litestream/internal/testingutil" |
| 20 | +) |
| 21 | + |
| 22 | +func TestVFS_TimeTravelFunctions(t *testing.T) { |
| 23 | + ctx := context.Background() |
| 24 | + client := file.NewReplicaClient(t.TempDir()) |
| 25 | + vfs := newVFS(t, client) |
| 26 | + vfs.PollInterval = 50 * time.Millisecond |
| 27 | + if err := sqlite3vfs.RegisterVFS("litestream-time", vfs); err != nil { |
| 28 | + t.Fatalf("failed to register litestream vfs: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "db")) |
| 32 | + db.MonitorInterval = 50 * time.Millisecond |
| 33 | + db.Replica = litestream.NewReplica(db) |
| 34 | + db.Replica.Client = client |
| 35 | + db.Replica.SyncInterval = 50 * time.Millisecond |
| 36 | + if err := db.Open(); err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + defer func() { _ = db.Close(ctx) }() |
| 40 | + |
| 41 | + sqldb0 := testingutil.MustOpenSQLDB(t, db.Path()) |
| 42 | + defer testingutil.MustCloseSQLDB(t, sqldb0) |
| 43 | + |
| 44 | + if _, err := sqldb0.Exec("CREATE TABLE t (x INTEGER)"); err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + if _, err := sqldb0.Exec("INSERT INTO t (x) VALUES (100)"); err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + time.Sleep(6 * db.MonitorInterval) |
| 51 | + |
| 52 | + firstCreatedAt := fetchLTXCreatedAt(t, ctx, client) |
| 53 | + |
| 54 | + time.Sleep(20 * time.Millisecond) // Ensure a different timestamp for the next file. |
| 55 | + if _, err := sqldb0.Exec("UPDATE t SET x = 200"); err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + time.Sleep(4 * db.MonitorInterval) |
| 59 | + |
| 60 | + sqldb1, err := sql.Open("sqlite3", "file:/tmp/time-travel.db?vfs=litestream-time") |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("failed to open database: %v", err) |
| 63 | + } |
| 64 | + defer sqldb1.Close() |
| 65 | + sqldb1.SetMaxOpenConns(1) |
| 66 | + time.Sleep(2 * vfs.PollInterval) |
| 67 | + |
| 68 | + var value int |
| 69 | + if err := sqldb1.QueryRow("SELECT x FROM t").Scan(&value); err != nil { |
| 70 | + t.Fatalf("query latest value: %v", err) |
| 71 | + } else if got, want := value, 200; got != want { |
| 72 | + t.Fatalf("latest value: got %d, want %d", got, want) |
| 73 | + } |
| 74 | + |
| 75 | + target := firstCreatedAt.Add(1 * time.Millisecond).UTC().Format(time.RFC3339Nano) |
| 76 | + if _, err := sqldb1.Exec(fmt.Sprintf("PRAGMA LITESTREAM_TIME = '%s'", target)); err != nil { |
| 77 | + t.Fatalf("set target time: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + if err := sqldb1.QueryRow("SELECT x FROM t").Scan(&value); err != nil { |
| 81 | + t.Fatalf("query historical value: %v", err) |
| 82 | + } else if got, want := value, 100; got != want { |
| 83 | + t.Fatalf("historical value: got %d, want %d", got, want) |
| 84 | + } |
| 85 | + |
| 86 | + var currentTime string |
| 87 | + if err := sqldb1.QueryRow("PRAGMA litestream_time").Scan(¤tTime); err != nil { |
| 88 | + t.Fatalf("current time: %v", err) |
| 89 | + } else if currentTime != target { |
| 90 | + t.Fatalf("current time mismatch: got %s, want %s", currentTime, target) |
| 91 | + } |
| 92 | + |
| 93 | + if _, err := sqldb1.Exec("PRAGMA LITESTREAM_TIME = LATEST"); err != nil { |
| 94 | + t.Fatalf("reset time: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + if err := sqldb1.QueryRow("SELECT x FROM t").Scan(&value); err != nil { |
| 98 | + t.Fatalf("query reset value: %v", err) |
| 99 | + } else if got, want := value, 200; got != want { |
| 100 | + t.Fatalf("reset value: got %d, want %d", got, want) |
| 101 | + } |
| 102 | + |
| 103 | + if err := sqldb1.QueryRow("PRAGMA litestream_time").Scan(¤tTime); err != nil { |
| 104 | + t.Fatalf("current time after reset: %v", err) |
| 105 | + } else if currentTime != "latest" { |
| 106 | + t.Fatalf("current time after reset mismatch: got %s, want latest", currentTime) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func fetchLTXCreatedAt(tb testing.TB, ctx context.Context, client litestream.ReplicaClient) time.Time { |
| 111 | + tb.Helper() |
| 112 | + |
| 113 | + itr, err := client.LTXFiles(ctx, 0, 0, true) |
| 114 | + if err != nil { |
| 115 | + tb.Fatalf("ltx files: %v", err) |
| 116 | + } |
| 117 | + defer itr.Close() |
| 118 | + |
| 119 | + var ts time.Time |
| 120 | + for itr.Next() { |
| 121 | + ts = itr.Item().CreatedAt |
| 122 | + } |
| 123 | + if err := itr.Close(); err != nil { |
| 124 | + tb.Fatalf("close iterator: %v", err) |
| 125 | + } |
| 126 | + if ts.IsZero() { |
| 127 | + tb.Fatalf("no ltx files found") |
| 128 | + } |
| 129 | + return ts.UTC() |
| 130 | +} |
0 commit comments