package db import ( "io/fs" "regexp" "strings" "testing" ) var migrationNamePattern = regexp.MustCompile(`^[0-9]{5}_[a-z0-9_]+\.sql$`) // TestMigrationsHaveSequentialNamesAndBothDirections runs without a database so // make -C backend check catches a malformed or one-way migration. func TestMigrationsHaveSequentialNamesAndBothDirections(t *testing.T) { migrations, err := Migrations() if err != nil { t.Fatalf("Migrations() error = %v", err) } names, err := fs.Glob(migrations, "*.sql") if err != nil { t.Fatalf("list migrations: %v", err) } if len(names) == 0 { t.Fatal("no embedded migrations") } for _, name := range names { if !migrationNamePattern.MatchString(name) { t.Fatalf("migration %s does not match NNNNN_description.sql", name) } content, err := fs.ReadFile(migrations, name) if err != nil { t.Fatalf("read %s: %v", name, err) } text := string(content) up := strings.Index(text, "-- +goose Up") down := strings.Index(text, "-- +goose Down") if up < 0 || down < up { t.Fatalf("migration %s needs a -- +goose Up section followed by a -- +goose Down section", name) } if strings.TrimSpace(text[down+len("-- +goose Down"):]) == "" { t.Fatalf("migration %s has an empty Down section", name) } } }