46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package pgstore
|
|
|
|
import (
|
|
"io/fs"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// The migration SET is checkable without a database, and that check is worth having: a file whose
|
|
// name does not parse is not "skipped", it silently never runs.
|
|
func TestMigrationSetIsWellFormed(t *testing.T) {
|
|
names, err := fs.Glob(Migrations(), "*")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(names) == 0 {
|
|
t.Fatal("no migrations embedded")
|
|
}
|
|
nameRe := regexp.MustCompile(`^(\d{5})_[a-z0-9_]+\.sql$`)
|
|
prev := 0
|
|
for _, name := range names {
|
|
m := nameRe.FindStringSubmatch(name)
|
|
if m == nil {
|
|
t.Fatalf("%s: goose expects NNNNN_name.sql", name)
|
|
}
|
|
version, _ := strconv.Atoi(m[1])
|
|
if version <= prev {
|
|
t.Fatalf("%s: versions must ascend and never repeat (previous %05d)", name, prev)
|
|
}
|
|
prev = version
|
|
|
|
body, err := fs.ReadFile(Migrations(), name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, marker := range []string{"-- +goose Up", "-- +goose Down"} {
|
|
if !strings.Contains(string(body), marker) {
|
|
// A missing Down is not cosmetic: a rollout that cannot be rolled back is a
|
|
// one-way door, and goose reports it only when someone tries to walk back.
|
|
t.Fatalf("%s: missing %q", name, marker)
|
|
}
|
|
}
|
|
}
|
|
}
|