cmd/gitbayd/main_test.go
54 lines · 1392 bytes
1package main
2
3import (
4 "bytes"
5 "log/slog"
6 "strconv"
7 "strings"
8 "testing"
9)
10
11// A restart that moves the schema says so. Migrations used to run in silence,
12// which left an unexpected user_version with nothing in the journal tying it to
13// the deploy that applied it.
14func TestOpenStoreLogsSchemaMigration(t *testing.T) {
15 cfg := testConfig(t)
16
17 var buf bytes.Buffer
18 prev := slog.Default()
19 slog.SetDefault(slog.New(slog.NewTextHandler(&buf, nil)))
20 defer slog.SetDefault(prev)
21
22 // First open creates the database and migrates it from nothing.
23 s, err := openStore(cfg)
24 if err != nil {
25 t.Fatalf("openStore: %v", err)
26 }
27 version, err := s.Version()
28 if err != nil {
29 t.Fatalf("version: %v", err)
30 }
31 s.Close()
32
33 logged := buf.String()
34 if !strings.Contains(logged, "schema migrated") {
35 t.Fatalf("no migration logged:\n%s", logged)
36 }
37 for _, want := range []string{"from=0", "to=" + strconv.Itoa(version)} {
38 if !strings.Contains(logged, want) {
39 t.Errorf("expected %q in the log line:\n%s", want, logged)
40 }
41 }
42
43 // Reopening an already-current database is silent: every restart would
44 // otherwise claim a migration that did not happen.
45 buf.Reset()
46 s, err = openStore(cfg)
47 if err != nil {
48 t.Fatalf("reopen: %v", err)
49 }
50 s.Close()
51 if strings.Contains(buf.String(), "schema migrated") {
52 t.Errorf("logged a migration on an up-to-date database:\n%s", buf.String())
53 }
54}