Files
lincodex/backend/integration_test/candidate_card_access_migration_test.go

118 lines
4.1 KiB
Go

//go:build integration
package integration_test
import (
"context"
"testing"
"time"
"github.com/google/uuid"
postgresadapter "lincodex/backend/internal/adapter/secondary/postgres"
"lincodex/backend/internal/domain"
)
const candidateCardAccessMigrationVersion = 30
func TestCandidateCardAccessMigrationDownRetainsHistory(t *testing.T) {
env := bootstrap(t, postgresService)
ctx, cancel := context.WithTimeout(t.Context(), migrationTestTimeout)
defer cancel()
databaseURL := env.createIsolatedDatabase(ctx, t)
migrator := newMigrator(t, databaseURL)
pool := newPoolFor(ctx, t, databaseURL)
if _, err := migrator.Up(ctx); err != nil {
t.Fatalf("Up() error = %v", err)
}
// The organization is created while the latest schema is in place: its creation
// needs the holdings table, which the rollback below removes.
now := time.Now().UTC().Truncate(time.Microsecond)
identity := identityFixture{
ctx: ctx, pool: pool, store: postgresadapter.NewIdentityStore(pool), now: now,
}
candidate := identity.createUser(t, "candidate@example.com", domain.ScenarioCandidate, now.Add(-time.Hour))
organizationFixture := organizationFixture{
ctx: ctx, pool: pool, identity: identity,
transactor: postgresadapter.NewTransactor(pool), now: now,
}
owner := organizationFixture.employer(t, "owner@example.com")
organization := organizationFixture.create(t, owner, organizationFixture.completeProfile(t, "Example Company"), now)
rollBackToCandidateCardAccessVersion(ctx, t, migrator)
if _, err := pool.Exec(ctx, `INSERT INTO candidate_card_access_events (
id, candidate_id, organization_id, actor_id, kind, field_codes, occurred_at
) VALUES ($1, $2, $3, $4, 'card_viewed', ARRAY['stack']::text[], $5)`,
uuid.New(), candidate.ID, organization.ID(), owner.ID, now); err != nil {
t.Fatalf("insert retained card access event: %v", err)
}
_, err := migrator.DownOne(ctx)
assertPgError(t, "DownOne() with retained card access history", err, privacyIntegrityViolation, "candidate_card_access_events_retained")
var tableExists bool
if err := pool.QueryRow(ctx, `SELECT to_regclass('public.candidate_card_access_events') IS NOT NULL`).Scan(&tableExists); err != nil {
t.Fatalf("check access history table after refused rollback: %v", err)
}
if !tableExists {
t.Fatal("candidate_card_access_events was removed despite retained history")
}
var count int
if err := pool.QueryRow(ctx, `SELECT count(*) FROM candidate_card_access_events WHERE candidate_id = $1`, candidate.ID).Scan(&count); err != nil {
t.Fatalf("read retained access event after refused rollback: %v", err)
}
if count != 1 {
t.Fatalf("retained access event count = %d, want 1", count)
}
statuses, err := migrator.Status(ctx)
if err != nil {
t.Fatalf("Status() after refused rollback: %v", err)
}
version30Applied := false
for _, status := range statuses {
if status.Version == candidateCardAccessMigrationVersion {
version30Applied = status.Applied
break
}
}
if !version30Applied {
t.Fatal("migration 30 is not applied after its guarded Down() failed")
}
}
func rollBackToCandidateCardAccessVersion(ctx context.Context, t *testing.T, migrator *postgresadapter.Migrator) {
t.Helper()
for {
statuses, err := migrator.Status(ctx)
if err != nil {
t.Fatalf("Status() while preparing migration %d: %v", candidateCardAccessMigrationVersion, err)
}
version30Applied := false
laterMigrationApplied := false
for _, status := range statuses {
if !status.Applied {
continue
}
if status.Version == candidateCardAccessMigrationVersion {
version30Applied = true
}
if status.Version > candidateCardAccessMigrationVersion {
laterMigrationApplied = true
}
}
if !version30Applied {
t.Fatalf("migration %d is not applied", candidateCardAccessMigrationVersion)
}
if !laterMigrationApplied {
return
}
result, err := migrator.DownOne(ctx)
if err != nil {
t.Fatalf("DownOne() while preparing migration %d: %v", candidateCardAccessMigrationVersion, err)
}
if result.Version <= candidateCardAccessMigrationVersion {
t.Fatalf("DownOne() rolled back migration %d while preparing migration %d", result.Version, candidateCardAccessMigrationVersion)
}
}
}