Files
Agent-System/internal/db/migrate_test.go
T
weichenandCursor e7b46e92d2 feat(db): allow migration_repair snapshot and event kinds
00028 check constraints would reject a real PG apply. Add goose 00032
with Up/Down so repair snapshots can persist.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-14 02:21:44 +00:00

81 lines
2.1 KiB
Go

package db
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
func TestMigrationGate_ExpectedFilesMatchAppliedVersion(t *testing.T) {
migDir := ResolveMigrationsDir()
expected, err := ExpectedMigrationCount(migDir)
if err != nil {
t.Fatalf("count migrations: %v", err)
}
if expected < 1 {
t.Fatal("expected at least one migration file")
}
dsn := os.Getenv("TEST_DATABASE_URL")
if dsn == "" {
t.Skip("TEST_DATABASE_URL not set; migration gate file count only")
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Skipf("postgres unavailable: %v", err)
}
defer pool.Close()
if err := pool.Ping(ctx); err != nil {
t.Skipf("postgres ping failed: %v", err)
}
if err := RunMigrations(ctx, pool, migDir); err != nil {
t.Fatalf("RunMigrations: %v", err)
}
version, err := CurrentVersion(ctx, pool)
if err != nil {
t.Fatalf("CurrentVersion: %v", err)
}
if int(version) < expected {
t.Fatalf("applied schema version %d behind migration files %d", version, expected)
}
}
func TestMigration032_AllowsMigrationRepairKinds(t *testing.T) {
files, err := ListMigrationFiles(ResolveMigrationsDir())
if err != nil {
t.Fatal(err)
}
var found string
for _, name := range files {
if strings.HasPrefix(name, "00032_") && strings.HasSuffix(name, ".sql") {
found = name
break
}
}
if found == "" {
t.Fatal("expected goose 00032 for migration_repair check constraints")
}
raw, err := os.ReadFile(filepath.Join(ResolveMigrationsDir(), found))
if err != nil {
t.Fatal(err)
}
text := string(raw)
if !strings.Contains(text, "-- +goose Up") || !strings.Contains(text, "-- +goose Down") {
t.Fatal("00032 must have Up and Down")
}
if !strings.Contains(text, "chk_snapshot_source_kind") || !strings.Contains(text, "chk_change_kind") {
t.Fatal("00032 must replace source/change kind checks")
}
if strings.Count(text, "'migration_repair'") < 2 {
t.Fatalf("00032 must allow migration_repair on both constraints, got %s", found)
}
}