feat(backend): add transactional outbox and embedded goose migrations

This commit is contained in:
shira committed 2026-10-05 23:32:43 +03:00
1 parent 653f3775f8
commit 15e2e50561
40 files changed
+2471 -67

No files matched your search

@@ -0,0 +1,57 @@
package outbound
import (
"context"
"errors"
"lincodex/backend/internal/domain"
)
var (
// ErrOutboxEventConflict reports that the event ID, or the same event type
// for the same aggregate version, is already recorded. The surrounding
// transaction is aborted and rolled back.
ErrOutboxEventConflict = errors.New("outbox event already recorded")
// ErrNestedTransaction reports a WithinTransaction call from inside another
// transaction callback. Nested calls would silently use a second connection
// and commit independently of the outer transaction.
ErrNestedTransaction = errors.New("transaction already in progress")
)
// Transactor runs a business operation in one database transaction.
type Transactor interface {
// WithinTransaction commits when fn returns nil and rolls back when fn
// returns an error or panics. The error is returned unchanged in identity
// and a panic is re-raised after the rollback. fn must use only the
// UnitOfWork and context it receives and must not retain them after
// returning. Calling WithinTransaction with that context returns
// ErrNestedTransaction. After a database error inside fn the transaction is
// aborted, so fn should return that error.
//
// A network failure during commit leaves the outcome unknown: the
// transaction may or may not have committed. Callers that retry must make
// the operation idempotent, for example through aggregate version checks
// and the outbox uniqueness rules.
WithinTransaction(ctx context.Context, fn func(ctx context.Context, unit UnitOfWork) error) error
}
// UnitOfWork exposes repositories bound to one transaction. Product
// repositories are added here as their usecases appear.
type UnitOfWork interface {
Outbox() OutboxWriter
}
// OutboxWriter appends domain events to the transactional outbox. Events become
// visible to the relay only after the surrounding transaction commits.
//
// The outbox rejects a second event of the same type for the same aggregate
// version. That rule is a backstop, not concurrency control: aggregate updates
// must still check the expected version (UPDATE ... WHERE version = $n) so a
// stale writer fails before it records an event.
type OutboxWriter interface {
// Append returns an error wrapping ErrOutboxEventConflict for a duplicate
// and domain.ErrInvalidDomainEvent for an event not built by
// domain.NewDomainEvent.
Append(ctx context.Context, events ...domain.DomainEvent) error
}