M1/#291: env-gated migration test against real postgres
This commit is contained in:
+50
-5
@@ -1,13 +1,58 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"context"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMigrateStubLoadsEmbeddedFiles(t *testing.T) {
|
// TestMigrateAgainstPostgres runs the embedded migrations against a real
|
||||||
err := Migrate()
|
// Postgres indicated by MINSTREL_TEST_DATABASE_URL. Intended flow for local
|
||||||
if !errors.Is(err, ErrNotConfigured) {
|
// development: `docker compose up -d postgres` then
|
||||||
t.Errorf("Migrate stub err = %v, want ErrNotConfigured", err)
|
// `MINSTREL_TEST_DATABASE_URL=postgres://minstrel:minstrel@localhost:5432/minstrel?sslmode=disable go test ./...`.
|
||||||
|
// Skipped in `-short` mode (which is what CI runs) and when the env var is unset.
|
||||||
|
func TestMigrateAgainstPostgres(t *testing.T) {
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip("skipping migration test in -short mode")
|
||||||
|
}
|
||||||
|
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
||||||
|
if dsn == "" {
|
||||||
|
t.Skip("MINSTREL_TEST_DATABASE_URL not set; skipping")
|
||||||
|
}
|
||||||
|
|
||||||
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
|
|
||||||
|
if err := Migrate(dsn, logger); err != nil {
|
||||||
|
t.Fatalf("first Migrate: %v", err)
|
||||||
|
}
|
||||||
|
if err := Migrate(dsn, logger); err != nil {
|
||||||
|
t.Fatalf("second Migrate (idempotency): %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pool, err := Open(context.Background(), dsn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Open: %v", err)
|
||||||
|
}
|
||||||
|
defer pool.Close()
|
||||||
|
|
||||||
|
var count int
|
||||||
|
if err := pool.QueryRow(context.Background(),
|
||||||
|
`SELECT count(*) FROM schema_migrations`).Scan(&count); err != nil {
|
||||||
|
t.Fatalf("query schema_migrations: %v", err)
|
||||||
|
}
|
||||||
|
if count == 0 {
|
||||||
|
t.Fatal("schema_migrations empty after Migrate")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMigrationsFSListsFiles(t *testing.T) {
|
||||||
|
entries, err := migrationsFS.ReadDir("migrations")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("read embedded dir: %v", err)
|
||||||
|
}
|
||||||
|
if len(entries) == 0 {
|
||||||
|
t.Fatal("no migration files embedded")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user