Files
Mikhail Verkovykh 9e43faa536 feat(lab5): durable async attachment pipeline through grade 4
- RabbitMQ quorum queue, persistent messages and publisher confirms
- S3 staging plus PostgreSQL attachments/jobs outbox transaction
- worker variants (thumbnail, crop, optimized WebP), DB lease and lease sweeper
- jittered retry, permanent failures, explicit replay and admission cap
- deterministic crash hooks, resilience script and evidence for grade 4
- async-aware custom tests, oversized pixel case, docs and changelog
2026-11-15 00:30:36 +03:00

89 lines
2.4 KiB
Go

package database
import (
"errors"
"io/fs"
"regexp"
"strconv"
"testing"
"github.com/golang-migrate/migrate/v4/source/iofs"
)
// latestMigrationVersion is the only value to bump when a migration is added.
// The rest of the test derives filenames and ordering from the directory.
const latestMigrationVersion = 5
var migrationFilePattern = regexp.MustCompile(`^(\d{4})_[a-z0-9_]+\.(up|down)\.sql$`)
func TestEmbeddedMigrationsAreCompleteAndContiguous(t *testing.T) {
entries, err := fs.ReadDir(migrationFiles, "migrations")
if err != nil {
t.Fatalf("read embedded migrations: %v", err)
}
up := make(map[int]string, latestMigrationVersion)
down := make(map[int]string, latestMigrationVersion)
for _, entry := range entries {
if entry.IsDir() {
continue
}
name := entry.Name()
match := migrationFilePattern.FindStringSubmatch(name)
if match == nil {
t.Fatalf("migration %q does not match NNNN_name.{up,down}.sql", name)
}
version, parseErr := strconv.Atoi(match[1])
if parseErr != nil || version < 1 || version > latestMigrationVersion {
t.Fatalf("migration %q has version outside 1..%d", name, latestMigrationVersion)
}
files := up
if match[2] == "down" {
files = down
}
if previous, ok := files[version]; ok {
t.Fatalf("version %d has two %s files: %q and %q", version, match[2], previous, name)
}
files[version] = name
}
for version := 1; version <= latestMigrationVersion; version++ {
if up[version] == "" {
t.Fatalf("version %d is missing an up migration", version)
}
if down[version] == "" {
t.Fatalf("version %d is missing a down migration", version)
}
}
source, err := iofs.New(migrationFiles, "migrations")
if err != nil {
t.Fatalf("parse migrations with golang-migrate: %v", err)
}
defer func() { _ = source.Close() }()
version, err := source.First()
if err != nil {
t.Fatalf("read first migration: %v", err)
}
for expected := 1; expected <= latestMigrationVersion; expected++ {
if version != uint(expected) {
t.Fatalf("migration order = %d, want %d", version, expected)
}
next, nextErr := source.Next(version)
if expected == latestMigrationVersion {
if !errors.Is(nextErr, fs.ErrNotExist) {
t.Fatalf("expected %d to be the last migration, got %v", version, nextErr)
}
break
}
if nextErr != nil {
t.Fatalf("read migration after %d: %v", version, nextErr)
}
version = next
}
}