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,94 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
||||
"lincodex/backend/db"
|
||||
)
|
||||
|
||||
// unreachableDatabaseURL points at a closed local port. The tests below never
|
||||
// need a server: constructors do not connect, and the connect test expects a
|
||||
// refusal.
|
||||
const unreachableDatabaseURL = "postgres://migrator:migration-secret@127.0.0.1:1/app?sslmode=disable&connect_timeout=2"
|
||||
|
||||
func TestNewMigratorLoadsEmbeddedMigrationsWithoutConnecting(t *testing.T) {
|
||||
migrations, err := db.Migrations()
|
||||
if err != nil {
|
||||
t.Fatalf("db.Migrations() error = %v", err)
|
||||
}
|
||||
migrator, err := NewMigrator(unreachableDatabaseURL, migrations)
|
||||
if err != nil {
|
||||
t.Fatalf("NewMigrator() rejected the embedded migrations: %v", err)
|
||||
}
|
||||
if err := migrator.Close(); err != nil {
|
||||
t.Fatalf("Close() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewMigratorRejectsDuplicateVersions(t *testing.T) {
|
||||
migrations := fstest.MapFS{
|
||||
"00001_first.sql": {Data: []byte("-- +goose Up\nSELECT 1;\n-- +goose Down\nSELECT 1;\n")},
|
||||
"00001_second.sql": {Data: []byte("-- +goose Up\nSELECT 1;\n-- +goose Down\nSELECT 1;\n")},
|
||||
}
|
||||
if migrator, err := NewMigrator(unreachableDatabaseURL, migrations); err == nil {
|
||||
_ = migrator.Close()
|
||||
t.Fatal("NewMigrator() accepted two migrations with the same version")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrationConnConfigSetsSessionTimeouts(t *testing.T) {
|
||||
connConfig, err := migrationConnConfig(unreachableDatabaseURL)
|
||||
if err != nil {
|
||||
t.Fatalf("migrationConnConfig() error = %v", err)
|
||||
}
|
||||
for name, want := range map[string]time.Duration{
|
||||
"lock_timeout": migrationLockTimeout,
|
||||
"statement_timeout": migrationStatementTimeout,
|
||||
} {
|
||||
got, err := time.ParseDuration(connConfig.RuntimeParams[name] + "ms")
|
||||
if err != nil || got != want {
|
||||
t.Fatalf("%s = %q, want %s", name, connConfig.RuntimeParams[name], want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrationConnConfigHidesInvalidURL(t *testing.T) {
|
||||
const password = "parse-secret"
|
||||
_, err := migrationConnConfig("postgres://app:" + password + "@db:not-a-port/app")
|
||||
if err == nil {
|
||||
t.Fatal("migrationConnConfig() accepted an invalid URL")
|
||||
}
|
||||
if strings.Contains(err.Error(), password) {
|
||||
t.Fatalf("migrationConnConfig() error exposes the password: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeConnectErrorDropsConnectionIdentity(t *testing.T) {
|
||||
_, connectErr := pgconn.Connect(t.Context(), unreachableDatabaseURL)
|
||||
var original *pgconn.ConnectError
|
||||
if !errors.As(connectErr, &original) {
|
||||
t.Fatalf("pgconn.Connect() error = %v, want *pgconn.ConnectError", connectErr)
|
||||
}
|
||||
err := sanitizeConnectError(connectErr)
|
||||
for _, secret := range []string{"user=", "migrator", "migration-secret", "database=app"} {
|
||||
if strings.Contains(err.Error(), secret) {
|
||||
t.Fatalf("sanitizeConnectError() = %v, still contains %q", err, secret)
|
||||
}
|
||||
}
|
||||
if !errors.Is(err, original.Unwrap()) {
|
||||
t.Fatalf("sanitizeConnectError() = %v, lost the network cause", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeConnectErrorKeepsOtherErrors(t *testing.T) {
|
||||
original := errors.New("relation does not exist")
|
||||
if err := sanitizeConnectError(original); err != original { //nolint:errorlint // Identity is the property under test.
|
||||
t.Fatalf("sanitizeConnectError() = %v, want the original error", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user