diff --git a/engine.go b/engine.go index f417b70c9213fcd64a1ec1ead18492d74c034d45..3e3bcfbbd1d093f3cdbcfbb7bdabf6f46e7531e6 100644 --- a/engine.go +++ b/engine.go @@ -288,6 +288,7 @@ type EventHandlerConstruct interface{} type Options struct { WithTransaction bool RecursiveDelete bool // 删除根实体是否递归删除所有子实体 + DryRun bool // dryrun 模式,不执行持久化,不发送事件 Locker ILock Executor IExecutor EventPersist EventPersist // 是否保存领域事件到 DB @@ -318,6 +319,14 @@ func (t RecursiveDeleteOption) ApplyToOptions(opts *Options) { const WithRecursiveDelete = RecursiveDeleteOption(true) +type DryRunOption bool + +func (t DryRunOption) ApplyToOptions(opts *Options) { + opts.DryRun = bool(t) +} + +const WithDryRun = DryRunOption(true) + type LoggerOption struct { logger logr.Logger } @@ -981,6 +990,9 @@ func (e *Stage) makeEventPersistAction(events []*DomainEvent) (*Action, error) { } func (e *Stage) dispatchEvents(ctx context.Context, events []*DomainEvent) (err error) { + if e.options.DryRun { + return nil + } if !e.options.WithTransaction { e.logger.Info("engine not support transaction") return e.eventBus.Dispatch(ctx, events...) @@ -1109,6 +1121,9 @@ func execHook(ctx context.Context, entity IEntity, ct changeType, isBefore bool) } func (e *Stage) exec(ctx context.Context, actions []*Action) error { + if e.options.DryRun { + return nil + } for _, a := range actions { if err := e.executor.Exec(ctx, a); err != nil { return err diff --git a/eventbus/mysql/po.go b/eventbus/mysql/po.go index a1e3028eb32bd0b64ddcda7b49f00f037b96fc6f..269b20193920d9e3bed9176ff9eeee8d0a925673 100644 --- a/eventbus/mysql/po.go +++ b/eventbus/mysql/po.go @@ -58,9 +58,9 @@ func (o *EventPO) TableName() string { return "ddd_domain_event" } +// Transaction /* CREATE TABLE `ddd_event_transaction` ( - `id` int NOT NULL AUTO_INCREMENT, `service` varchar(30) NOT NULL, `events` text, @@ -68,7 +68,6 @@ CREATE TABLE `ddd_event_transaction` ( `created_at` datetime(3) DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_ddd_event_transaction_created_at` (`created_at`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; */ type Transaction struct { @@ -96,7 +95,7 @@ type FailedInfo struct { // ServicePO 服务存储模型 /* - CREATE TABLE `ddd_eventbus_service` ( +CREATE TABLE `ddd_eventbus_service` ( `name` varchar(30) NOT NULL, `failed` text, `retry` text, @@ -106,7 +105,7 @@ type FailedInfo struct { PRIMARY KEY (`name`), KEY `idx_ddd_eventbus_service_created_at` (`created_at`), KEY `idx_ddd_eventbus_service_updated_at` (`updated_at`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; */ type ServicePO struct { Name string `gorm:"primaryKey"`