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