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
@@ -72,6 +72,9 @@ func run(ctx context.Context, config config.Config, logger *slog.Logger) error {
|
||||
if err := config.Validate(); err != nil {
|
||||
return fmt.Errorf("bootstrap.run - validate configuration: %w", err)
|
||||
}
|
||||
if err := migrateDatabase(ctx, config, logger); err != nil {
|
||||
return fmt.Errorf("bootstrap.run - migrate database: %w", err)
|
||||
}
|
||||
application, cleanup, err := initialize(ctx, config, logger)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bootstrap.run - initialize application: %w", err)
|
||||
|
||||
@@ -3,11 +3,14 @@ package bootstrap
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"lincodex/backend/config"
|
||||
)
|
||||
|
||||
func TestServeServersGracefullyStopsOnCancellation(t *testing.T) {
|
||||
@@ -67,3 +70,35 @@ func listenForTest(t *testing.T) net.Listener {
|
||||
t.Cleanup(func() { _ = listener.Close() })
|
||||
return listener
|
||||
}
|
||||
|
||||
func TestMigrateDatabaseSkipsWithoutWork(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
settings config.Config
|
||||
}{
|
||||
{name: "disabled", settings: config.Config{DatabaseMigrateOnStart: false, DatabaseURL: "postgres://app@db/app"}},
|
||||
{name: "database not configured", settings: config.Config{DatabaseMigrateOnStart: true}},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if err := migrateDatabase(t.Context(), test.settings, slog.New(slog.DiscardHandler)); err != nil {
|
||||
t.Fatalf("migrateDatabase() error = %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateDatabaseHidesConnectionStringOnFailure(t *testing.T) {
|
||||
const password = "startup-secret-password"
|
||||
settings := config.Config{
|
||||
DatabaseMigrateOnStart: true,
|
||||
DatabaseURL: "postgres://app:" + password + "@db:not-a-port/app",
|
||||
}
|
||||
err := migrateDatabase(t.Context(), settings, slog.New(slog.DiscardHandler))
|
||||
if err == nil {
|
||||
t.Fatal("migrateDatabase() accepted an invalid DATABASE_URL")
|
||||
}
|
||||
if strings.Contains(err.Error(), password) || strings.Contains(err.Error(), "db:not-a-port") {
|
||||
t.Fatalf("migrateDatabase() error exposes the connection string: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"lincodex/backend/config"
|
||||
"lincodex/backend/db"
|
||||
"lincodex/backend/internal/adapter/secondary/postgres"
|
||||
)
|
||||
|
||||
// startupMigrationTimeout bounds lock waiting and migration work before the
|
||||
// servers start. It matches the migration lock retry window.
|
||||
const startupMigrationTimeout = 5 * time.Minute
|
||||
|
||||
// migrateDatabase applies embedded migrations before any listener opens, so the
|
||||
// service never serves requests against an older schema. Without DATABASE_URL
|
||||
// the service keeps its existing behavior: it starts and reports not ready.
|
||||
func migrateDatabase(ctx context.Context, settings config.Config, logger *slog.Logger) error {
|
||||
if !settings.DatabaseMigrateOnStart {
|
||||
logger.Info("startup database migrations are disabled")
|
||||
return nil
|
||||
}
|
||||
if settings.DatabaseURL == "" {
|
||||
logger.Warn("startup database migrations skipped because DATABASE_URL is not set")
|
||||
return nil
|
||||
}
|
||||
migrations, err := db.Migrations()
|
||||
if err != nil {
|
||||
return fmt.Errorf("bootstrap.migrateDatabase - load migrations: %w", err)
|
||||
}
|
||||
migrator, err := postgres.NewMigrator(settings.DatabaseURL, migrations)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bootstrap.migrateDatabase - create migrator: %w", err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, startupMigrationTimeout)
|
||||
defer cancel()
|
||||
applied, err := migrator.Up(ctx)
|
||||
if closeErr := migrator.Close(); closeErr != nil {
|
||||
err = errors.Join(err, fmt.Errorf("bootstrap.migrateDatabase - close migrator: %w", closeErr))
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("bootstrap.migrateDatabase - apply migrations: %w", err)
|
||||
}
|
||||
for _, migration := range applied {
|
||||
logger.Info("database migration applied",
|
||||
slog.Int64("version", migration.Version),
|
||||
slog.String("name", migration.Name),
|
||||
slog.Duration("duration", migration.Duration),
|
||||
)
|
||||
}
|
||||
logger.Info("database schema is up to date", slog.Int("applied_migrations", len(applied)))
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user