Files
panel/controller/internal/db/migrate_test.go
T
2026-07-14 19:19:43 -07:00

50 lines
1.2 KiB
Go

package db
import (
"context"
"os"
"sync"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
// TestMigrateConcurrent runs Migrate from two goroutines against a real
// Postgres and asserts neither errors (the advisory lock serializes them;
// the loser sees every version recorded and no-ops). Requires a live DB:
// set PANEL_TEST_DSN to run, e.g.
//
// PANEL_TEST_DSN=postgres://user:pw@localhost:5432/panel_test go test ./controller/internal/db/
//
// Skipped otherwise — no Postgres is assumed on dev machines or CI.
func TestMigrateConcurrent(t *testing.T) {
dsn := os.Getenv("PANEL_TEST_DSN")
if dsn == "" {
t.Skip("PANEL_TEST_DSN not set; migration lock test needs a live Postgres")
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatalf("connect: %v", err)
}
defer pool.Close()
var wg sync.WaitGroup
errs := make([]error, 2)
for i := 0; i < 2; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
errs[i] = Migrate(ctx, pool)
}(i)
}
wg.Wait()
for i, e := range errs {
if e != nil {
t.Errorf("concurrent Migrate %d: %v", i, e)
}
}
}