feat(backend): add transactional outbox and embedded goose migrations
This commit is contained in:
1 parent
653f3775f8
commit
15e2e50561
40 files changed
+2471
-67
No files matched your search
@@ -0,0 +1,141 @@
|
||||
//go:build integration
|
||||
|
||||
package integration_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
postgresadapter "lincodex/backend/internal/adapter/secondary/postgres"
|
||||
)
|
||||
|
||||
const (
|
||||
migrationTestTimeout = 3 * time.Minute
|
||||
concurrentMigrators = 4
|
||||
)
|
||||
|
||||
func TestPostgresMigrations(t *testing.T) {
|
||||
env := bootstrap(t, postgresService)
|
||||
ctx, cancel := context.WithTimeout(t.Context(), migrationTestTimeout)
|
||||
defer cancel()
|
||||
total := embeddedMigrationCount(t)
|
||||
|
||||
t.Run("bootstrap database has every migration applied", func(t *testing.T) {
|
||||
assertAllApplied(ctx, t, newMigrator(t, env.Config.DatabaseURL), total)
|
||||
})
|
||||
|
||||
t.Run("up, down to zero, and up again on an isolated database", func(t *testing.T) {
|
||||
databaseURL := env.createIsolatedDatabase(ctx, t)
|
||||
migrator := newMigrator(t, databaseURL)
|
||||
pool := newPoolFor(ctx, t, databaseURL)
|
||||
|
||||
applied, err := migrator.Up(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("first Up() error = %v", err)
|
||||
}
|
||||
if len(applied) != total {
|
||||
t.Fatalf("first Up() applied %d migrations, want %d", len(applied), total)
|
||||
}
|
||||
assertOutboxTableExists(ctx, t, pool, true)
|
||||
|
||||
for rolledBack := 0; ; rolledBack++ {
|
||||
_, err := migrator.DownOne(ctx)
|
||||
if errors.Is(err, postgresadapter.ErrNoMigrationToRollBack) {
|
||||
if rolledBack != total {
|
||||
t.Fatalf("rolled back %d migrations, want %d", rolledBack, total)
|
||||
}
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("DownOne() after %d rollbacks error = %v", rolledBack, err)
|
||||
}
|
||||
}
|
||||
assertOutboxTableExists(ctx, t, pool, false)
|
||||
var leftovers int
|
||||
err = pool.QueryRow(ctx, `
|
||||
SELECT count(*) FROM pg_catalog.pg_class AS c
|
||||
JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
|
||||
WHERE n.nspname = 'public' AND c.relname NOT LIKE 'goose_db_version%'`).Scan(&leftovers)
|
||||
if err != nil {
|
||||
t.Fatalf("count leftover relations: %v", err)
|
||||
}
|
||||
if leftovers != 0 {
|
||||
t.Fatalf("%d relations remain after rolling back every migration", leftovers)
|
||||
}
|
||||
|
||||
applied, err = migrator.Up(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("second Up() error = %v", err)
|
||||
}
|
||||
if len(applied) != total {
|
||||
t.Fatalf("second Up() applied %d migrations, want %d", len(applied), total)
|
||||
}
|
||||
assertOutboxTableExists(ctx, t, pool, true)
|
||||
assertAllApplied(ctx, t, migrator, total)
|
||||
})
|
||||
|
||||
t.Run("concurrent runners apply each migration once", func(t *testing.T) {
|
||||
databaseURL := env.createIsolatedDatabase(ctx, t)
|
||||
migrators := make([]*postgresadapter.Migrator, concurrentMigrators)
|
||||
for index := range migrators {
|
||||
migrators[index] = newMigrator(t, databaseURL)
|
||||
}
|
||||
applied := make([]int, len(migrators))
|
||||
errs := make([]error, len(migrators))
|
||||
start := make(chan struct{})
|
||||
var group sync.WaitGroup
|
||||
for index, migrator := range migrators {
|
||||
group.Go(func() {
|
||||
<-start
|
||||
results, err := migrator.Up(ctx)
|
||||
applied[index], errs[index] = len(results), err
|
||||
})
|
||||
}
|
||||
close(start)
|
||||
group.Wait()
|
||||
|
||||
sum := 0
|
||||
for index, err := range errs {
|
||||
if err != nil {
|
||||
t.Fatalf("runner %d Up() error = %v", index, err)
|
||||
}
|
||||
sum += applied[index]
|
||||
}
|
||||
if sum != total {
|
||||
t.Fatalf("runners applied %d migrations in total, want %d", sum, total)
|
||||
}
|
||||
assertAllApplied(ctx, t, migrators[0], total)
|
||||
})
|
||||
}
|
||||
|
||||
func assertAllApplied(ctx context.Context, t *testing.T, migrator *postgresadapter.Migrator, total int) {
|
||||
t.Helper()
|
||||
statuses, err := migrator.Status(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("Status() error = %v", err)
|
||||
}
|
||||
if len(statuses) != total {
|
||||
t.Fatalf("Status() lists %d migrations, want %d", len(statuses), total)
|
||||
}
|
||||
for _, status := range statuses {
|
||||
if !status.Applied {
|
||||
t.Fatalf("migration %s is pending", status.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func assertOutboxTableExists(ctx context.Context, t *testing.T, pool *pgxpool.Pool, want bool) {
|
||||
t.Helper()
|
||||
var exists bool
|
||||
if err := pool.QueryRow(ctx, `SELECT to_regclass('public.outbox_events') IS NOT NULL`).Scan(&exists); err != nil {
|
||||
t.Fatalf("check outbox table: %v", err)
|
||||
}
|
||||
if exists != want {
|
||||
t.Fatalf("outbox_events exists = %t, want %t", exists, want)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user