forked from mtusi2026/mtusi2026lab5
chore(migrate) moved codebase from lab4
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
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 = 3
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user