mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-17 10:55:12 +00:00
fix(eventstore): Make Eventstore Compatible with Relational Table Package (#10687)
Improves compatibility of eventstore and related database components with the new relational table package. ## Which problems are solved 1. **Incompatible Database Interfaces**: The existing eventstore was tightly coupled to the database package, which is incompatible with the new, more abstract relational table package in v3. This prevented the new command-side logic from pushing events to the legacy eventstore. 2. **Missing Health Checks**: The database interfaces in the new package lacked a Ping method, making it impossible to perform health checks on database connections. 3. **Event Publishing Logic**: The command handling logic in domain needed a way to collect and push events to the legacy eventstore after a command was successfully executed. ## How the problems are solved 1. **`LegacyEventstore` Interface**: * A new `LegacyEventstore` interface is introduced in the new `database/eventstore` . This interface exposes a `PushWithNewClient` method that accepts the new `database.QueryExecutor` interface, decoupling the v3 domain from the legacy implementation. * The `internal/eventstore.Eventstore` now implements this interface. A wrapper, PushWithClient, is added to convert the old database client types (`*sql.DB`, `*sql.Tx`) into the new `QueryExecutor` types before calling `PushWithNewClient`. 2. **Database Interface Updates**: * The `database.Pool` and `database.Client` interfaces in `storage/eventstore` have been updated to include a Ping method, allowing for consistent health checks across different database dialects. * The `postgres` and `sql` dialect implementations have been updated to support this new method. 3. **Command and Invoker Refactoring**: * The `Commander` interface in domain now includes an `Events() []legacy_es.Command` method. This allows commands to declare which events they will generate. * The `eventCollector` in the invoker logic has been redesigned. It now ensures a database transaction is started before executing a command. After successful execution, it calls the `Events()` method on the command to collect the generated events and appends them to a list. * The `eventStoreInvoker` then pushes all collected events to the legacy eventstore using the new `LegacyEventstore` interface, ensuring that events are only pushed if the entire command (and any sub-commands) executes successfully within the transaction. 4. **Testing**: * New unit tests have been added for the invoker to verify that events are correctly collected from single commands, batched commands, and nested commands. These changes create a clean bridge between the new v3 command-side logic and the existing v1 eventstore, allowing for incremental adoption of the new architecture while maintaining full functionality. ## Additional Information closes https://github.com/zitadel/zitadel/issues/10442
This commit is contained in:
+126
-111
@@ -1,131 +1,146 @@
|
||||
package domain
|
||||
|
||||
// import (
|
||||
// "context"
|
||||
// "fmt"
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
// "github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||
// )
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||
legacy_es "github.com/zitadel/zitadel/internal/eventstore"
|
||||
)
|
||||
|
||||
// // Commander is the all it needs to implement the command pattern.
|
||||
// // It is the interface all manipulations need to implement.
|
||||
// // If possible it should also be used for queries. We will find out if this is possible in the future.
|
||||
// type Commander interface {
|
||||
// Execute(ctx context.Context, opts *CommandOpts) (err error)
|
||||
// fmt.Stringer
|
||||
// }
|
||||
// Commander is all that is needed to implement the command pattern.
|
||||
// It is the interface all manipulations need to implement.
|
||||
// If possible it should also be used for queries. We will find out if this is possible in the future.
|
||||
type Commander interface {
|
||||
Execute(ctx context.Context, opts *CommandOpts) (err error)
|
||||
// Events returns the events that should be pushed to the event store after the command is executed.
|
||||
// If the command does not produce events, it should return nil or an empty slice.
|
||||
Events(ctx context.Context) []legacy_es.Command
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
// // Invoker is part of the command pattern.
|
||||
// // It is the interface that is used to execute commands.
|
||||
// type Invoker interface {
|
||||
// Invoke(ctx context.Context, command Commander, opts *CommandOpts) error
|
||||
// }
|
||||
// Invoker is part of the command pattern.
|
||||
// It is the interface that is used to execute commands.
|
||||
type Invoker interface {
|
||||
Invoke(ctx context.Context, command Commander, opts *CommandOpts) error
|
||||
}
|
||||
|
||||
// // CommandOpts are passed to each command
|
||||
// // the provide common fields used by commands like the database client.
|
||||
// type CommandOpts struct {
|
||||
// DB database.QueryExecutor
|
||||
// Invoker Invoker
|
||||
// }
|
||||
// CommandOpts are passed to each command
|
||||
// it provides common fields used by commands like the database client.
|
||||
type CommandOpts struct {
|
||||
DB database.QueryExecutor
|
||||
Invoker Invoker
|
||||
}
|
||||
|
||||
// type ensureTxOpts struct {
|
||||
// *database.TransactionOptions
|
||||
// }
|
||||
type ensureTxOpts struct {
|
||||
*database.TransactionOptions
|
||||
}
|
||||
|
||||
// type EnsureTransactionOpt func(*ensureTxOpts)
|
||||
type EnsureTransactionOpt func(*ensureTxOpts)
|
||||
|
||||
// // EnsureTx ensures that the DB is a transaction. If it is not, it will start a new transaction.
|
||||
// // The returned close function will end the transaction. If the DB is already a transaction, the close function
|
||||
// // will do nothing because another [Commander] is already responsible for ending the transaction.
|
||||
// func (o *CommandOpts) EnsureTx(ctx context.Context, opts ...EnsureTransactionOpt) (close func(context.Context, error) error, err error) {
|
||||
// beginner, ok := o.DB.(database.Beginner)
|
||||
// if !ok {
|
||||
// // db is already a transaction
|
||||
// return func(_ context.Context, err error) error {
|
||||
// return err
|
||||
// }, nil
|
||||
// }
|
||||
// EnsureTx ensures that the DB is a transaction. If it is not, it will start a new transaction.
|
||||
// The returned close function will end the transaction. If the DB is already a transaction, the close function
|
||||
// will do nothing because another [Commander] is already responsible for ending the transaction.
|
||||
func (o *CommandOpts) EnsureTx(ctx context.Context, opts ...EnsureTransactionOpt) (close func(context.Context, error) error, err error) {
|
||||
beginner, ok := o.DB.(database.Beginner)
|
||||
if !ok {
|
||||
// db is already a transaction
|
||||
return func(_ context.Context, err error) error {
|
||||
return err
|
||||
}, nil
|
||||
}
|
||||
|
||||
// txOpts := &ensureTxOpts{
|
||||
// TransactionOptions: new(database.TransactionOptions),
|
||||
// }
|
||||
// for _, opt := range opts {
|
||||
// opt(txOpts)
|
||||
// }
|
||||
txOpts := &ensureTxOpts{
|
||||
TransactionOptions: new(database.TransactionOptions),
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(txOpts)
|
||||
}
|
||||
|
||||
// tx, err := beginner.Begin(ctx, txOpts.TransactionOptions)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// o.DB = tx
|
||||
tx, err := beginner.Begin(ctx, txOpts.TransactionOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
o.DB = tx
|
||||
|
||||
// return func(ctx context.Context, err error) error {
|
||||
// return tx.End(ctx, err)
|
||||
// }, nil
|
||||
// }
|
||||
return func(ctx context.Context, err error) error {
|
||||
return tx.End(ctx, err)
|
||||
}, nil
|
||||
}
|
||||
|
||||
// // EnsureClient ensures that the o.DB is a client. If it is not, it will get a new client from the [database.Pool].
|
||||
// // The returned close function will release the client. If the o.DB is already a client or transaction, the close function
|
||||
// // will do nothing because another [Commander] is already responsible for releasing the client.
|
||||
// func (o *CommandOpts) EnsureClient(ctx context.Context) (close func(_ context.Context) error, err error) {
|
||||
// pool, ok := o.DB.(database.Pool)
|
||||
// if !ok {
|
||||
// // o.DB is already a client
|
||||
// return func(_ context.Context) error {
|
||||
// return nil
|
||||
// }, nil
|
||||
// }
|
||||
// client, err := pool.Acquire(ctx)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// o.DB = client
|
||||
// return func(ctx context.Context) error {
|
||||
// return client.Release(ctx)
|
||||
// }, nil
|
||||
// }
|
||||
// EnsureClient ensures that the o.DB is a client. If it is not, it will get a new client from the [database.Pool].
|
||||
// The returned close function will release the client. If the o.DB is already a client or transaction, the close function
|
||||
// will do nothing because another [Commander] is already responsible for releasing the client.
|
||||
func (o *CommandOpts) EnsureClient(ctx context.Context) (close func(_ context.Context) error, err error) {
|
||||
pool, ok := o.DB.(database.Pool)
|
||||
if !ok {
|
||||
// o.DB is already a client
|
||||
return func(_ context.Context) error {
|
||||
return nil
|
||||
}, nil
|
||||
}
|
||||
client, err := pool.Acquire(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
o.DB = client
|
||||
return func(ctx context.Context) error {
|
||||
return client.Release(ctx)
|
||||
}, nil
|
||||
}
|
||||
|
||||
// func (o *CommandOpts) Invoke(ctx context.Context, command Commander) error {
|
||||
// if o.Invoker == nil {
|
||||
// return command.Execute(ctx, o)
|
||||
// }
|
||||
// return o.Invoker.Invoke(ctx, command, o)
|
||||
// }
|
||||
func (o *CommandOpts) Invoke(ctx context.Context, command Commander) error {
|
||||
if o.Invoker == nil {
|
||||
return command.Execute(ctx, o)
|
||||
}
|
||||
return o.Invoker.Invoke(ctx, command, o)
|
||||
}
|
||||
|
||||
// func DefaultOpts(invoker Invoker) *CommandOpts {
|
||||
// if invoker == nil {
|
||||
// invoker = &noopInvoker{}
|
||||
// }
|
||||
// return &CommandOpts{
|
||||
// DB: pool,
|
||||
// Invoker: invoker,
|
||||
// }
|
||||
// }
|
||||
func DefaultOpts(invoker Invoker) *CommandOpts {
|
||||
if invoker == nil {
|
||||
invoker = &noopInvoker{}
|
||||
}
|
||||
return &CommandOpts{
|
||||
DB: pool,
|
||||
Invoker: invoker,
|
||||
}
|
||||
}
|
||||
|
||||
// // commandBatch is a batch of commands.
|
||||
// // It uses the [Invoker] provided by the opts to execute each command.
|
||||
// type commandBatch struct {
|
||||
// Commands []Commander
|
||||
// }
|
||||
// commandBatch is a batch of commands.
|
||||
// It uses the [Invoker] provided by the opts to execute each command.
|
||||
type commandBatch struct {
|
||||
Commands []Commander
|
||||
}
|
||||
|
||||
// func BatchCommands(cmds ...Commander) *commandBatch {
|
||||
// return &commandBatch{
|
||||
// Commands: cmds,
|
||||
// }
|
||||
// }
|
||||
// Events implements Commander.
|
||||
func (cmd *commandBatch) Events(ctx context.Context) []legacy_es.Command {
|
||||
commands := make([]legacy_es.Command, 0, len(cmd.Commands))
|
||||
for _, c := range cmd.Commands {
|
||||
if e := c.Events(ctx); len(e) > 0 {
|
||||
commands = append(commands, e...)
|
||||
}
|
||||
}
|
||||
return commands
|
||||
}
|
||||
|
||||
// // String implements [Commander].
|
||||
// func (cmd *commandBatch) String() string {
|
||||
// return "commandBatch"
|
||||
// }
|
||||
func BatchCommands(cmds ...Commander) *commandBatch {
|
||||
return &commandBatch{
|
||||
Commands: cmds,
|
||||
}
|
||||
}
|
||||
|
||||
// func (b *commandBatch) Execute(ctx context.Context, opts *CommandOpts) (err error) {
|
||||
// for _, cmd := range b.Commands {
|
||||
// if err = opts.Invoke(ctx, cmd); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
// String implements [Commander].
|
||||
func (cmd *commandBatch) String() string {
|
||||
return "commandBatch"
|
||||
}
|
||||
|
||||
// var _ Commander = (*commandBatch)(nil)
|
||||
func (b *commandBatch) Execute(ctx context.Context, opts *CommandOpts) (err error) {
|
||||
for _, cmd := range b.Commands {
|
||||
if err = opts.Invoke(ctx, cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ Commander = (*commandBatch)(nil)
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/eventstore"
|
||||
"github.com/zitadel/zitadel/backend/v3/telemetry/logging"
|
||||
"github.com/zitadel/zitadel/backend/v3/telemetry/tracing"
|
||||
)
|
||||
|
||||
// The variables could also be moved to a struct.
|
||||
// I just started with the singleton pattern and kept it like this.
|
||||
var (
|
||||
pool database.Pool
|
||||
// userCodeAlgorithm crypto.EncryptionAlgorithm
|
||||
tracer tracing.Tracer
|
||||
logger logging.Logger = *logging.NewLogger(slog.Default())
|
||||
legacyEventstore eventstore.LegacyEventstore
|
||||
|
||||
// userRepo func(database.QueryExecutor) UserRepository
|
||||
// instanceRepo func(database.QueryExecutor) InstanceRepository
|
||||
// cryptoRepo func(database.QueryExecutor) CryptoRepository
|
||||
// orgRepo func(database.QueryExecutor) OrgRepository
|
||||
|
||||
// instanceCache cache.Cache[instanceCacheIndex, string, *Instance]
|
||||
// orgCache cache.Cache[orgCacheIndex, string, *Org]
|
||||
|
||||
// generateID func() (string, error) = func() (string, error) {
|
||||
// return strconv.FormatUint(rand.Uint64(), 10), nil
|
||||
// }
|
||||
)
|
||||
|
||||
func SetPool(p database.Pool) {
|
||||
pool = p
|
||||
}
|
||||
|
||||
// func SetUserCodeAlgorithm(algorithm crypto.EncryptionAlgorithm) {
|
||||
// userCodeAlgorithm = algorithm
|
||||
// }
|
||||
|
||||
func SetTracer(t tracing.Tracer) {
|
||||
tracer = t
|
||||
}
|
||||
|
||||
func SetLogger(l logging.Logger) {
|
||||
logger = l
|
||||
}
|
||||
|
||||
func SetLegacyEventstore(es eventstore.LegacyEventstore) {
|
||||
legacyEventstore = es
|
||||
}
|
||||
|
||||
// func SetUserRepository(repo func(database.QueryExecutor) UserRepository) {
|
||||
// userRepo = repo
|
||||
// }
|
||||
|
||||
// func SetOrgRepository(repo func(database.QueryExecutor) OrgRepository) {
|
||||
// orgRepo = repo
|
||||
// }
|
||||
|
||||
// func SetInstanceRepository(repo func(database.QueryExecutor) InstanceRepository) {
|
||||
// instanceRepo = repo
|
||||
// }
|
||||
|
||||
// func SetCryptoRepository(repo func(database.QueryExecutor) CryptoRepository) {
|
||||
// cryptoRepo = repo
|
||||
// }
|
||||
@@ -60,68 +60,3 @@ type domainChanges interface {
|
||||
// This is used for reducing events.
|
||||
SetUpdatedAt(t time.Time) database.Change
|
||||
}
|
||||
|
||||
// import (
|
||||
// "math/rand/v2"
|
||||
// "strconv"
|
||||
|
||||
// "github.com/zitadel/zitadel/backend/v3/storage/cache"
|
||||
// "github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||
|
||||
// // "github.com/zitadel/zitadel/backend/v3/telemetry/logging"
|
||||
// "github.com/zitadel/zitadel/backend/v3/telemetry/tracing"
|
||||
// "github.com/zitadel/zitadel/internal/crypto"
|
||||
// )
|
||||
|
||||
// // The variables could also be moved to a struct.
|
||||
// // I just started with the singleton pattern and kept it like this.
|
||||
// var (
|
||||
// pool database.Pool
|
||||
// userCodeAlgorithm crypto.EncryptionAlgorithm
|
||||
// tracer tracing.Tracer
|
||||
// // logger logging.Logger
|
||||
|
||||
// userRepo func(database.QueryExecutor) UserRepository
|
||||
// // instanceRepo func(database.QueryExecutor) InstanceRepository
|
||||
// cryptoRepo func(database.QueryExecutor) CryptoRepository
|
||||
// orgRepo func(database.QueryExecutor) OrgRepository
|
||||
|
||||
// // instanceCache cache.Cache[instanceCacheIndex, string, *Instance]
|
||||
// orgCache cache.Cache[orgCacheIndex, string, *Org]
|
||||
|
||||
// generateID func() (string, error) = func() (string, error) {
|
||||
// return strconv.FormatUint(rand.Uint64(), 10), nil
|
||||
// }
|
||||
// )
|
||||
|
||||
// func SetPool(p database.Pool) {
|
||||
// pool = p
|
||||
// }
|
||||
|
||||
// func SetUserCodeAlgorithm(algorithm crypto.EncryptionAlgorithm) {
|
||||
// userCodeAlgorithm = algorithm
|
||||
// }
|
||||
|
||||
// func SetTracer(t tracing.Tracer) {
|
||||
// tracer = t
|
||||
// }
|
||||
|
||||
// // func SetLogger(l logging.Logger) {
|
||||
// // logger = l
|
||||
// // }
|
||||
|
||||
// func SetUserRepository(repo func(database.QueryExecutor) UserRepository) {
|
||||
// userRepo = repo
|
||||
// }
|
||||
|
||||
// func SetOrgRepository(repo func(database.QueryExecutor) OrgRepository) {
|
||||
// orgRepo = repo
|
||||
// }
|
||||
|
||||
// // func SetInstanceRepository(repo func(database.QueryExecutor) InstanceRepository) {
|
||||
// // instanceRepo = repo
|
||||
// // }
|
||||
|
||||
// func SetCryptoRepository(repo func(database.QueryExecutor) CryptoRepository) {
|
||||
// cryptoRepo = repo
|
||||
// }
|
||||
|
||||
+117
-116
@@ -1,142 +1,143 @@
|
||||
package domain
|
||||
|
||||
// import (
|
||||
// "context"
|
||||
// "fmt"
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
// "github.com/zitadel/zitadel/backend/v3/storage/eventstore"
|
||||
// )
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/eventstore"
|
||||
legacy_es "github.com/zitadel/zitadel/internal/eventstore"
|
||||
)
|
||||
|
||||
// // Invoke provides a way to execute commands within the domain package.
|
||||
// // It uses a chain of responsibility pattern to handle the command execution.
|
||||
// // The default chain includes logging, tracing, and event publishing.
|
||||
// // If you want to invoke multiple commands in a single transaction, you can use the [commandBatch].
|
||||
// func Invoke(ctx context.Context, cmd Commander) error {
|
||||
// invoker := newEventStoreInvoker(newLoggingInvoker(newTraceInvoker(nil)))
|
||||
// opts := &CommandOpts{
|
||||
// Invoker: invoker.collector,
|
||||
// DB: pool,
|
||||
// }
|
||||
// return invoker.Invoke(ctx, cmd, opts)
|
||||
// }
|
||||
// Invoke provides a way to execute commands within the domain package.
|
||||
// It uses a chain of responsibility pattern to handle the command execution.
|
||||
// The default chain includes logging, tracing, and event publishing.
|
||||
// If you want to invoke multiple commands in a single transaction, you can use the [commandBatch].
|
||||
func Invoke(ctx context.Context, cmd Commander) error {
|
||||
invoker := newEventStoreInvoker(newLoggingInvoker(newTraceInvoker(nil)))
|
||||
opts := &CommandOpts{
|
||||
Invoker: invoker.collector,
|
||||
DB: pool,
|
||||
}
|
||||
return invoker.Invoke(ctx, cmd, opts)
|
||||
}
|
||||
|
||||
// // eventStoreInvoker checks if the command implements the [eventer] interface.
|
||||
// // If it does, it collects the events and publishes them to the event store.
|
||||
// type eventStoreInvoker struct {
|
||||
// collector *eventCollector
|
||||
// }
|
||||
// eventStoreInvoker checks if the [Commander].Events function returns any events.
|
||||
// If it does, it collects the events and publishes them to the event store.
|
||||
type eventStoreInvoker struct {
|
||||
collector *eventCollector
|
||||
}
|
||||
|
||||
// func newEventStoreInvoker(next Invoker) *eventStoreInvoker {
|
||||
// return &eventStoreInvoker{collector: &eventCollector{next: next}}
|
||||
// }
|
||||
func newEventStoreInvoker(next Invoker) *eventStoreInvoker {
|
||||
return &eventStoreInvoker{collector: &eventCollector{next: next}}
|
||||
}
|
||||
|
||||
// func (i *eventStoreInvoker) Invoke(ctx context.Context, command Commander, opts *CommandOpts) (err error) {
|
||||
// err = i.collector.Invoke(ctx, command, opts)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// if len(i.collector.events) > 0 {
|
||||
// err = eventstore.Publish(ctx, i.collector.events, opts.DB)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
func (i *eventStoreInvoker) Invoke(ctx context.Context, command Commander, opts *CommandOpts) (err error) {
|
||||
err = i.collector.Invoke(ctx, command, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(i.collector.events) > 0 {
|
||||
err = eventstore.Publish(ctx, legacyEventstore, opts.DB, i.collector.events...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// // eventCollector collects events from all commands. The [eventStoreInvoker] pushes the collected events after all commands are executed.
|
||||
// type eventCollector struct {
|
||||
// next Invoker
|
||||
// events []*eventstore.Event
|
||||
// }
|
||||
// eventCollector collects events from all commands. The [eventStoreInvoker] pushes the collected events after all commands are executed.
|
||||
// The events are collected after the command got executed, the collector ensures that the command is executed in the same transaction as writing the events.
|
||||
type eventCollector struct {
|
||||
next Invoker
|
||||
events []legacy_es.Command
|
||||
}
|
||||
|
||||
// type eventer interface {
|
||||
// Events() []*eventstore.Event
|
||||
// }
|
||||
func (i *eventCollector) Invoke(ctx context.Context, command Commander, opts *CommandOpts) (err error) {
|
||||
close, err := opts.EnsureTx(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { err = close(ctx, err) }()
|
||||
|
||||
// func (i *eventCollector) Invoke(ctx context.Context, command Commander, opts *CommandOpts) (err error) {
|
||||
// if e, ok := command.(eventer); ok && len(e.Events()) > 0 {
|
||||
// // we need to ensure all commands are executed in the same transaction
|
||||
// close, err := opts.EnsureTx(ctx)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// defer func() { err = close(ctx, err) }()
|
||||
if i.next != nil {
|
||||
err = i.next.Invoke(ctx, command, opts)
|
||||
} else {
|
||||
err = command.Execute(ctx, opts)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i.events = append(command.Events(ctx), i.events...)
|
||||
|
||||
// i.events = append(i.events, e.Events()...)
|
||||
// }
|
||||
// if i.next != nil {
|
||||
// return i.next.Invoke(ctx, command, opts)
|
||||
// }
|
||||
// return command.Execute(ctx, opts)
|
||||
// }
|
||||
return
|
||||
}
|
||||
|
||||
// // traceInvoker decorates each command with tracing.
|
||||
// type traceInvoker struct {
|
||||
// next Invoker
|
||||
// }
|
||||
// traceInvoker decorates each command with tracing.
|
||||
type traceInvoker struct {
|
||||
next Invoker
|
||||
}
|
||||
|
||||
// func newTraceInvoker(next Invoker) *traceInvoker {
|
||||
// return &traceInvoker{next: next}
|
||||
// }
|
||||
func newTraceInvoker(next Invoker) *traceInvoker {
|
||||
return &traceInvoker{next: next}
|
||||
}
|
||||
|
||||
// func (i *traceInvoker) Invoke(ctx context.Context, command Commander, opts *CommandOpts) (err error) {
|
||||
// ctx, span := tracer.Start(ctx, fmt.Sprintf("%T", command))
|
||||
// defer func() {
|
||||
// if err != nil {
|
||||
// span.RecordError(err)
|
||||
// }
|
||||
// span.End()
|
||||
// }()
|
||||
func (i *traceInvoker) Invoke(ctx context.Context, command Commander, opts *CommandOpts) (err error) {
|
||||
ctx, span := tracer.Start(ctx, fmt.Sprintf("%T", command))
|
||||
defer func() {
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
}
|
||||
span.End()
|
||||
}()
|
||||
|
||||
// if i.next != nil {
|
||||
// return i.next.Invoke(ctx, command, opts)
|
||||
// }
|
||||
// return command.Execute(ctx, opts)
|
||||
// }
|
||||
if i.next != nil {
|
||||
return i.next.Invoke(ctx, command, opts)
|
||||
}
|
||||
return command.Execute(ctx, opts)
|
||||
}
|
||||
|
||||
// // loggingInvoker decorates each command with logging.
|
||||
// // It is an example implementation and logs the command name at the beginning and success or failure after the command got executed.
|
||||
// type loggingInvoker struct {
|
||||
// next Invoker
|
||||
// }
|
||||
// loggingInvoker decorates each command with logging.
|
||||
// It is an example implementation and logs the command name at the beginning and success or failure after the command got executed.
|
||||
type loggingInvoker struct {
|
||||
next Invoker
|
||||
}
|
||||
|
||||
// func newLoggingInvoker(next Invoker) *loggingInvoker {
|
||||
// return &loggingInvoker{next: next}
|
||||
// }
|
||||
func newLoggingInvoker(next Invoker) *loggingInvoker {
|
||||
return &loggingInvoker{next: next}
|
||||
}
|
||||
|
||||
// func (i *loggingInvoker) Invoke(ctx context.Context, command Commander, opts *CommandOpts) (err error) {
|
||||
// logger.InfoContext(ctx, "Invoking command", "command", command.String())
|
||||
func (i *loggingInvoker) Invoke(ctx context.Context, command Commander, opts *CommandOpts) (err error) {
|
||||
logger.InfoContext(ctx, "Invoking command", "command", command.String())
|
||||
|
||||
// if i.next != nil {
|
||||
// err = i.next.Invoke(ctx, command, opts)
|
||||
// } else {
|
||||
// err = command.Execute(ctx, opts)
|
||||
// }
|
||||
if i.next != nil {
|
||||
err = i.next.Invoke(ctx, command, opts)
|
||||
} else {
|
||||
err = command.Execute(ctx, opts)
|
||||
}
|
||||
|
||||
// if err != nil {
|
||||
// logger.ErrorContext(ctx, "Command invocation failed", "command", command.String(), "error", err)
|
||||
// return err
|
||||
// }
|
||||
// logger.InfoContext(ctx, "Command invocation succeeded", "command", command.String())
|
||||
// return nil
|
||||
// }
|
||||
if err != nil {
|
||||
logger.ErrorContext(ctx, "Command invocation failed", "command", command.String(), "error", err)
|
||||
return err
|
||||
}
|
||||
logger.InfoContext(ctx, "Command invocation succeeded", "command", command.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
// type noopInvoker struct {
|
||||
// next Invoker
|
||||
// }
|
||||
type noopInvoker struct {
|
||||
next Invoker
|
||||
}
|
||||
|
||||
// func (i *noopInvoker) Invoke(ctx context.Context, command Commander, opts *CommandOpts) error {
|
||||
// if i.next != nil {
|
||||
// return i.next.Invoke(ctx, command, opts)
|
||||
// }
|
||||
// return command.Execute(ctx, opts)
|
||||
// }
|
||||
func (i *noopInvoker) Invoke(ctx context.Context, command Commander, opts *CommandOpts) error {
|
||||
if i.next != nil {
|
||||
return i.next.Invoke(ctx, command, opts)
|
||||
}
|
||||
return command.Execute(ctx, opts)
|
||||
}
|
||||
|
||||
// // cacheInvoker could be used in the future to do the caching.
|
||||
// // My goal would be to have two interfaces:
|
||||
// // - cacheSetter: which caches an object
|
||||
// // - cacheGetter: which gets an object from the cache, this should also skip the command execution
|
||||
// cacheInvoker could be used in the future to do the caching.
|
||||
// My goal would be to have two interfaces:
|
||||
// - cacheSetter: which caches an object
|
||||
// - cacheGetter: which gets an object from the cache, this should also skip the command execution
|
||||
// type cacheInvoker struct {
|
||||
// next Invoker
|
||||
// }
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/mock/gomock"
|
||||
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/database/dbmock"
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/eventstore"
|
||||
legacy_es "github.com/zitadel/zitadel/internal/eventstore"
|
||||
)
|
||||
|
||||
type testLegacyEventstore struct{}
|
||||
|
||||
func (es *testLegacyEventstore) PushWithNewClient(ctx context.Context, client database.QueryExecutor, commands ...legacy_es.Command) ([]legacy_es.Event, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var _ eventstore.LegacyEventstore = (*testLegacyEventstore)(nil)
|
||||
|
||||
type invokeTestCommand struct {
|
||||
events []legacy_es.Command
|
||||
execute func(ctx context.Context, opts *CommandOpts) error
|
||||
}
|
||||
|
||||
// Events implements Commander.
|
||||
func (i *invokeTestCommand) Events(ctx context.Context) []legacy_es.Command {
|
||||
return i.events
|
||||
}
|
||||
|
||||
// Execute implements Commander.
|
||||
func (i *invokeTestCommand) Execute(ctx context.Context, opts *CommandOpts) (err error) {
|
||||
if i.execute == nil {
|
||||
return nil
|
||||
}
|
||||
return i.execute(ctx, opts)
|
||||
}
|
||||
|
||||
// String implements Commander.
|
||||
func (i *invokeTestCommand) String() string {
|
||||
return "invokeTestCommand"
|
||||
}
|
||||
|
||||
var _ Commander = (*invokeTestCommand)(nil)
|
||||
|
||||
type invokeTestEvent struct {
|
||||
id string
|
||||
}
|
||||
|
||||
// Aggregate implements [legacy_es.Command].
|
||||
func (i *invokeTestEvent) Aggregate() *legacy_es.Aggregate {
|
||||
return &legacy_es.Aggregate{
|
||||
ID: i.id,
|
||||
Type: "test",
|
||||
ResourceOwner: "test",
|
||||
InstanceID: "test",
|
||||
Version: legacy_es.Version("v1"),
|
||||
}
|
||||
}
|
||||
|
||||
// Creator implements [legacy_es.Command].
|
||||
func (i *invokeTestEvent) Creator() string {
|
||||
return "test"
|
||||
}
|
||||
|
||||
// Fields implements [legacy_es.Command].
|
||||
func (i *invokeTestEvent) Fields() []*legacy_es.FieldOperation {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Payload implements [legacy_es.Command].
|
||||
func (i *invokeTestEvent) Payload() any {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Revision implements [legacy_es.Command].
|
||||
func (i *invokeTestEvent) Revision() uint16 {
|
||||
return 1
|
||||
}
|
||||
|
||||
// Type implements [legacy_es.Command].
|
||||
func (i *invokeTestEvent) Type() legacy_es.EventType {
|
||||
return "test"
|
||||
}
|
||||
|
||||
// UniqueConstraints implements [legacy_es.Command].
|
||||
func (i *invokeTestEvent) UniqueConstraints() []*legacy_es.UniqueConstraint {
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ legacy_es.Command = (*invokeTestEvent)(nil)
|
||||
|
||||
func Test_eventCollector_Invoke(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
command Commander
|
||||
expectedErr error
|
||||
assertCollectedEvents func(t *testing.T, events []legacy_es.Command)
|
||||
}{
|
||||
{
|
||||
name: "simple command with events",
|
||||
expectedErr: nil,
|
||||
command: &invokeTestCommand{
|
||||
events: []legacy_es.Command{&invokeTestEvent{id: "1"}},
|
||||
},
|
||||
assertCollectedEvents: func(t *testing.T, events []legacy_es.Command) {
|
||||
require.Len(t, events, 1)
|
||||
assert.Equal(t, "1", events[0].Aggregate().ID)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "simple command without events",
|
||||
expectedErr: nil,
|
||||
command: &invokeTestCommand{},
|
||||
assertCollectedEvents: func(t *testing.T, events []legacy_es.Command) {
|
||||
assert.Len(t, events, 0)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "command with sub commands",
|
||||
expectedErr: nil,
|
||||
command: &invokeTestCommand{
|
||||
events: []legacy_es.Command{&invokeTestEvent{id: "1"}},
|
||||
execute: func(ctx context.Context, opts *CommandOpts) error {
|
||||
return opts.Invoke(ctx, &invokeTestCommand{
|
||||
events: []legacy_es.Command{&invokeTestEvent{id: "2"}},
|
||||
})
|
||||
},
|
||||
},
|
||||
assertCollectedEvents: func(t *testing.T, events []legacy_es.Command) {
|
||||
require.Len(t, events, 2)
|
||||
assert.Equal(t, "1", events[0].Aggregate().ID)
|
||||
assert.Equal(t, "2", events[1].Aggregate().ID)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "only sub commands with events",
|
||||
expectedErr: nil,
|
||||
command: &invokeTestCommand{
|
||||
execute: func(ctx context.Context, opts *CommandOpts) error {
|
||||
return opts.Invoke(ctx, &invokeTestCommand{
|
||||
events: []legacy_es.Command{&invokeTestEvent{id: "2"}},
|
||||
})
|
||||
},
|
||||
},
|
||||
assertCollectedEvents: func(t *testing.T, events []legacy_es.Command) {
|
||||
require.Len(t, events, 1)
|
||||
assert.Equal(t, "2", events[0].Aggregate().ID)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
SetLegacyEventstore(new(testLegacyEventstore))
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
|
||||
pool := dbmock.NewMockPool(ctrl)
|
||||
|
||||
tx := dbmock.NewMockTransaction(ctrl)
|
||||
tx.EXPECT().End(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
|
||||
pool.EXPECT().Begin(gomock.Any(), gomock.Any()).Return(tx, nil).AnyTimes()
|
||||
|
||||
i := newEventStoreInvoker(nil)
|
||||
opts := CommandOpts{
|
||||
Invoker: i,
|
||||
DB: pool,
|
||||
}
|
||||
err := opts.Invoke(t.Context(), tt.command)
|
||||
require.ErrorIs(t, err, tt.expectedErr)
|
||||
tt.assertCollectedEvents(t, i.collector.events)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,19 @@
|
||||
package domain
|
||||
|
||||
// import (
|
||||
// "context"
|
||||
import (
|
||||
"context"
|
||||
|
||||
// "github.com/zitadel/zitadel/backend/v3/storage/eventstore"
|
||||
// )
|
||||
legacy_es "github.com/zitadel/zitadel/internal/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/repository/org"
|
||||
)
|
||||
|
||||
// // AddOrgCommand adds a new organization.
|
||||
// // I'm unsure if we should add the Admins here or if this should be a separate command.
|
||||
// type AddOrgCommand struct {
|
||||
// ID string `json:"id"`
|
||||
// Name string `json:"name"`
|
||||
// Admins []*AddMemberCommand `json:"admins"`
|
||||
// }
|
||||
// AddOrgCommand adds a new organization.
|
||||
// I'm unsure if we should add the Admins here or if this should be a separate command.
|
||||
type AddOrgCommand struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
// Admins []*AddMemberCommand `json:"admins"`
|
||||
}
|
||||
|
||||
// func NewAddOrgCommand(name string, admins ...*AddMemberCommand) *AddOrgCommand {
|
||||
// return &AddOrgCommand{
|
||||
@@ -63,17 +64,12 @@ package domain
|
||||
// return nil
|
||||
// }
|
||||
|
||||
// // Events implements [eventer].
|
||||
// func (cmd *AddOrgCommand) Events() []*eventstore.Event {
|
||||
// return []*eventstore.Event{
|
||||
// {
|
||||
// AggregateType: "org",
|
||||
// AggregateID: cmd.ID,
|
||||
// Type: "org.added",
|
||||
// Payload: cmd,
|
||||
// },
|
||||
// }
|
||||
// }
|
||||
// Events implements [eventer].
|
||||
func (cmd *AddOrgCommand) Events(ctx context.Context) []legacy_es.Command {
|
||||
return []legacy_es.Command{
|
||||
org.NewOrgAddedEvent(ctx, &org.NewAggregate(cmd.ID).Aggregate, cmd.Name),
|
||||
}
|
||||
}
|
||||
|
||||
// var (
|
||||
// _ Commander = (*AddOrgCommand)(nil)
|
||||
|
||||
@@ -12,6 +12,8 @@ type Pool interface {
|
||||
|
||||
Acquire(ctx context.Context) (Client, error)
|
||||
Close(ctx context.Context) error
|
||||
|
||||
Ping(ctx context.Context) error
|
||||
}
|
||||
|
||||
type PoolTest interface {
|
||||
@@ -27,6 +29,8 @@ type Client interface {
|
||||
Migrator
|
||||
|
||||
Release(ctx context.Context) error
|
||||
|
||||
Ping(ctx context.Context) error
|
||||
}
|
||||
|
||||
// Querier is a database client that can execute queries and return rows.
|
||||
|
||||
@@ -27,7 +27,7 @@ func (c *pgxConn) Begin(ctx context.Context, opts *database.TransactionOptions)
|
||||
if err != nil {
|
||||
return nil, wrapError(err)
|
||||
}
|
||||
return &pgxTx{tx}, nil
|
||||
return &Transaction{tx}, nil
|
||||
}
|
||||
|
||||
// Query implements sql.Client.
|
||||
@@ -56,6 +56,11 @@ func (c *pgxConn) Exec(ctx context.Context, sql string, args ...any) (int64, err
|
||||
return res.RowsAffected(), nil
|
||||
}
|
||||
|
||||
// Ping implements [database.Pool].
|
||||
func (c *pgxConn) Ping(ctx context.Context) error {
|
||||
return wrapError(c.Conn.Ping(ctx))
|
||||
}
|
||||
|
||||
// Migrate implements [database.Migrator].
|
||||
func (c *pgxConn) Migrate(ctx context.Context) error {
|
||||
if isMigrated {
|
||||
|
||||
@@ -62,7 +62,7 @@ func (c *pgxPool) Begin(ctx context.Context, opts *database.TransactionOptions)
|
||||
if err != nil {
|
||||
return nil, wrapError(err)
|
||||
}
|
||||
return &pgxTx{tx}, nil
|
||||
return &Transaction{tx}, nil
|
||||
}
|
||||
|
||||
// Close implements [database.Pool].
|
||||
@@ -71,6 +71,11 @@ func (c *pgxPool) Close(_ context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Ping implements [database.Pool].
|
||||
func (c *pgxPool) Ping(ctx context.Context) error {
|
||||
return wrapError(c.Pool.Ping(ctx))
|
||||
}
|
||||
|
||||
// Migrate implements [database.Migrator].
|
||||
func (c *pgxPool) Migrate(ctx context.Context) error {
|
||||
if isMigrated {
|
||||
|
||||
@@ -9,24 +9,30 @@ import (
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||
)
|
||||
|
||||
type pgxTx struct{ pgx.Tx }
|
||||
type Transaction struct{ pgx.Tx }
|
||||
|
||||
var _ database.Transaction = (*pgxTx)(nil)
|
||||
func PGxTx(tx pgx.Tx) *Transaction {
|
||||
return &Transaction{
|
||||
Tx: tx,
|
||||
}
|
||||
}
|
||||
|
||||
var _ database.Transaction = (*Transaction)(nil)
|
||||
|
||||
// Commit implements [database.Transaction].
|
||||
func (tx *pgxTx) Commit(ctx context.Context) error {
|
||||
func (tx *Transaction) Commit(ctx context.Context) error {
|
||||
err := tx.Tx.Commit(ctx)
|
||||
return wrapError(err)
|
||||
}
|
||||
|
||||
// Rollback implements [database.Transaction].
|
||||
func (tx *pgxTx) Rollback(ctx context.Context) error {
|
||||
func (tx *Transaction) Rollback(ctx context.Context) error {
|
||||
err := tx.Tx.Rollback(ctx)
|
||||
return wrapError(err)
|
||||
}
|
||||
|
||||
// End implements [database.Transaction].
|
||||
func (tx *pgxTx) End(ctx context.Context, err error) error {
|
||||
func (tx *Transaction) End(ctx context.Context, err error) error {
|
||||
if err != nil {
|
||||
rollbackErr := tx.Rollback(ctx)
|
||||
if rollbackErr != nil {
|
||||
@@ -39,7 +45,7 @@ func (tx *pgxTx) End(ctx context.Context, err error) error {
|
||||
|
||||
// Query implements [database.Transaction].
|
||||
// Subtle: this method shadows the method (Tx).Query of pgxTx.Tx.
|
||||
func (tx *pgxTx) Query(ctx context.Context, sql string, args ...any) (database.Rows, error) {
|
||||
func (tx *Transaction) Query(ctx context.Context, sql string, args ...any) (database.Rows, error) {
|
||||
rows, err := tx.Tx.Query(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return nil, wrapError(err)
|
||||
@@ -49,13 +55,13 @@ func (tx *pgxTx) Query(ctx context.Context, sql string, args ...any) (database.R
|
||||
|
||||
// QueryRow implements [database.Transaction].
|
||||
// Subtle: this method shadows the method (Tx).QueryRow of pgxTx.Tx.
|
||||
func (tx *pgxTx) QueryRow(ctx context.Context, sql string, args ...any) database.Row {
|
||||
func (tx *Transaction) QueryRow(ctx context.Context, sql string, args ...any) database.Row {
|
||||
return &Row{tx.Tx.QueryRow(ctx, sql, args...)}
|
||||
}
|
||||
|
||||
// Exec implements [database.Transaction].
|
||||
// Subtle: this method shadows the method (Pool).Exec of pgxPool.Pool.
|
||||
func (tx *pgxTx) Exec(ctx context.Context, sql string, args ...any) (int64, error) {
|
||||
func (tx *Transaction) Exec(ctx context.Context, sql string, args ...any) (int64, error) {
|
||||
res, err := tx.Tx.Exec(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return 0, wrapError(err)
|
||||
@@ -65,12 +71,12 @@ func (tx *pgxTx) Exec(ctx context.Context, sql string, args ...any) (int64, erro
|
||||
|
||||
// Begin implements [database.Transaction].
|
||||
// As postgres does not support nested transactions we use savepoints to emulate them.
|
||||
func (tx *pgxTx) Begin(ctx context.Context) (database.Transaction, error) {
|
||||
func (tx *Transaction) Begin(ctx context.Context) (database.Transaction, error) {
|
||||
savepoint, err := tx.Tx.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, wrapError(err)
|
||||
}
|
||||
return &pgxTx{savepoint}, nil
|
||||
return &Transaction{savepoint}, nil
|
||||
}
|
||||
|
||||
func transactionOptionsToPgx(opts *database.TransactionOptions) pgx.TxOptions {
|
||||
|
||||
@@ -11,6 +11,10 @@ type sqlConn struct {
|
||||
*sql.Conn
|
||||
}
|
||||
|
||||
func SQLConn(conn *sql.Conn) database.Client {
|
||||
return &sqlConn{Conn: conn}
|
||||
}
|
||||
|
||||
var _ database.Client = (*sqlConn)(nil)
|
||||
|
||||
// Release implements [database.Client].
|
||||
@@ -24,7 +28,7 @@ func (c *sqlConn) Begin(ctx context.Context, opts *database.TransactionOptions)
|
||||
if err != nil {
|
||||
return nil, wrapError(err)
|
||||
}
|
||||
return &sqlTx{tx}, nil
|
||||
return &Transaction{tx}, nil
|
||||
}
|
||||
|
||||
// Query implements sql.Client.
|
||||
@@ -54,6 +58,11 @@ func (c *sqlConn) Exec(ctx context.Context, sql string, args ...any) (int64, err
|
||||
return res.RowsAffected()
|
||||
}
|
||||
|
||||
// Ping implements [database.Pool].
|
||||
func (c *sqlConn) Ping(ctx context.Context) error {
|
||||
return wrapError(c.PingContext(ctx))
|
||||
}
|
||||
|
||||
// Migrate implements [database.Migrator].
|
||||
func (c *sqlConn) Migrate(ctx context.Context) error {
|
||||
return ErrMigrate
|
||||
|
||||
@@ -61,7 +61,12 @@ func (c *sqlPool) Begin(ctx context.Context, opts *database.TransactionOptions)
|
||||
if err != nil {
|
||||
return nil, wrapError(err)
|
||||
}
|
||||
return &sqlTx{tx}, nil
|
||||
return &Transaction{tx}, nil
|
||||
}
|
||||
|
||||
// Ping implements [database.Pool].
|
||||
func (c *sqlPool) Ping(ctx context.Context) error {
|
||||
return wrapError(c.PingContext(ctx))
|
||||
}
|
||||
|
||||
// Close implements [database.Pool].
|
||||
|
||||
@@ -8,28 +8,28 @@ import (
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||
)
|
||||
|
||||
type sqlTx struct{ *sql.Tx }
|
||||
type Transaction struct{ *sql.Tx }
|
||||
|
||||
var _ database.Transaction = (*sqlTx)(nil)
|
||||
var _ database.Transaction = (*Transaction)(nil)
|
||||
|
||||
func SQLTx(tx *sql.Tx) *sqlTx {
|
||||
return &sqlTx{
|
||||
func SQLTx(tx *sql.Tx) *Transaction {
|
||||
return &Transaction{
|
||||
Tx: tx,
|
||||
}
|
||||
}
|
||||
|
||||
// Commit implements [database.Transaction].
|
||||
func (tx *sqlTx) Commit(ctx context.Context) error {
|
||||
func (tx *Transaction) Commit(ctx context.Context) error {
|
||||
return wrapError(tx.Tx.Commit())
|
||||
}
|
||||
|
||||
// Rollback implements [database.Transaction].
|
||||
func (tx *sqlTx) Rollback(ctx context.Context) error {
|
||||
func (tx *Transaction) Rollback(ctx context.Context) error {
|
||||
return wrapError(tx.Tx.Rollback())
|
||||
}
|
||||
|
||||
// End implements [database.Transaction].
|
||||
func (tx *sqlTx) End(ctx context.Context, err error) error {
|
||||
func (tx *Transaction) End(ctx context.Context, err error) error {
|
||||
if err != nil {
|
||||
rollbackErr := tx.Rollback(ctx)
|
||||
if rollbackErr != nil {
|
||||
@@ -42,7 +42,7 @@ func (tx *sqlTx) End(ctx context.Context, err error) error {
|
||||
|
||||
// Query implements [database.Transaction].
|
||||
// Subtle: this method shadows the method (Tx).Query of pgxTx.Tx.
|
||||
func (tx *sqlTx) Query(ctx context.Context, sql string, args ...any) (database.Rows, error) {
|
||||
func (tx *Transaction) Query(ctx context.Context, sql string, args ...any) (database.Rows, error) {
|
||||
//nolint:rowserrcheck // Rows.Close is called by the caller
|
||||
rows, err := tx.QueryContext(ctx, sql, args...)
|
||||
if err != nil {
|
||||
@@ -53,13 +53,13 @@ func (tx *sqlTx) Query(ctx context.Context, sql string, args ...any) (database.R
|
||||
|
||||
// QueryRow implements [database.Transaction].
|
||||
// Subtle: this method shadows the method (Tx).QueryRow of pgxTx.Tx.
|
||||
func (tx *sqlTx) QueryRow(ctx context.Context, sql string, args ...any) database.Row {
|
||||
func (tx *Transaction) QueryRow(ctx context.Context, sql string, args ...any) database.Row {
|
||||
return &Row{tx.QueryRowContext(ctx, sql, args...)}
|
||||
}
|
||||
|
||||
// Exec implements [database.Transaction].
|
||||
// Subtle: this method shadows the method (Pool).Exec of pgxPool.Pool.
|
||||
func (tx *sqlTx) Exec(ctx context.Context, sql string, args ...any) (int64, error) {
|
||||
func (tx *Transaction) Exec(ctx context.Context, sql string, args ...any) (int64, error) {
|
||||
res, err := tx.ExecContext(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return 0, wrapError(err)
|
||||
@@ -69,7 +69,7 @@ func (tx *sqlTx) Exec(ctx context.Context, sql string, args ...any) (int64, erro
|
||||
|
||||
// Begin implements [database.Transaction].
|
||||
// As postgres does not support nested transactions we use savepoints to emulate them.
|
||||
func (tx *sqlTx) Begin(ctx context.Context) (database.Transaction, error) {
|
||||
func (tx *Transaction) Begin(ctx context.Context) (database.Transaction, error) {
|
||||
_, err := tx.ExecContext(ctx, createSavepoint)
|
||||
if err != nil {
|
||||
return nil, wrapError(err)
|
||||
|
||||
@@ -95,6 +95,10 @@ func (e *IntegrityViolationError) Is(target error) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
func (e *IntegrityViolationError) Unwrap() error {
|
||||
return e.original
|
||||
}
|
||||
|
||||
// CheckError is returned when a check constraint fails.
|
||||
// It wraps the [IntegrityViolationError] to provide more context.
|
||||
// It is used to indicate that a check constraint was violated during an insert or update operation.
|
||||
|
||||
@@ -4,21 +4,15 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
AggregateType string `json:"aggregateType"`
|
||||
AggregateID string `json:"aggregateId"`
|
||||
Type string `json:"type"`
|
||||
Payload any `json:"payload,omitempty"`
|
||||
type LegacyEventstore interface {
|
||||
PushWithNewClient(ctx context.Context, client database.QueryExecutor, commands ...eventstore.Command) ([]eventstore.Event, error)
|
||||
}
|
||||
|
||||
func Publish(ctx context.Context, events []*Event, db database.Executor) error {
|
||||
for _, event := range events {
|
||||
_, err := db.Exec(ctx, `INSERT INTO events (aggregate_type, aggregate_id) VALUES ($1, $2)`, event.AggregateType, event.AggregateID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
// Publish writes events to the eventstore using the provided pusher and database client.
|
||||
func Publish(ctx context.Context, es LegacyEventstore, client database.QueryExecutor, commands ...eventstore.Command) error {
|
||||
_, err := es.PushWithNewClient(ctx, client, commands...)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user