mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-21 23:37:31 +00:00
b5564572bc
This implementation increases parallel write capabilities of the eventstore. Please have a look at the technical advisories: [05](https://zitadel.com/docs/support/advisory/a10005) and [06](https://zitadel.com/docs/support/advisory/a10006). The implementation of eventstore.push is rewritten and stored events are migrated to a new table `eventstore.events2`. If you are using cockroach: make sure that the database user of ZITADEL has `VIEWACTIVITY` grant. This is used to query events.
147 lines
5.0 KiB
Go
147 lines
5.0 KiB
Go
package projection
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
)
|
|
|
|
const (
|
|
FlowTriggerTable = "projections.flow_triggers2"
|
|
FlowTypeCol = "flow_type"
|
|
FlowChangeDateCol = "change_date"
|
|
FlowSequenceCol = "sequence"
|
|
FlowTriggerTypeCol = "trigger_type"
|
|
FlowResourceOwnerCol = "resource_owner"
|
|
FlowInstanceIDCol = "instance_id"
|
|
FlowActionTriggerSequenceCol = "trigger_sequence"
|
|
FlowActionIDCol = "action_id"
|
|
FlowOwnerRemovedCol = "owner_removed"
|
|
)
|
|
|
|
type flowProjection struct{}
|
|
|
|
func newFlowProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
return handler.NewHandler(ctx, &config, new(flowProjection))
|
|
}
|
|
|
|
func (*flowProjection) Name() string {
|
|
return FlowTriggerTable
|
|
}
|
|
|
|
func (*flowProjection) Init() *old_handler.Check {
|
|
return handler.NewTableCheck(
|
|
handler.NewTable([]*handler.InitColumn{
|
|
handler.NewColumn(FlowTypeCol, handler.ColumnTypeEnum),
|
|
handler.NewColumn(FlowChangeDateCol, handler.ColumnTypeTimestamp),
|
|
handler.NewColumn(FlowSequenceCol, handler.ColumnTypeInt64),
|
|
handler.NewColumn(FlowTriggerTypeCol, handler.ColumnTypeEnum),
|
|
handler.NewColumn(FlowResourceOwnerCol, handler.ColumnTypeText),
|
|
handler.NewColumn(FlowInstanceIDCol, handler.ColumnTypeText),
|
|
handler.NewColumn(FlowActionTriggerSequenceCol, handler.ColumnTypeInt64),
|
|
handler.NewColumn(FlowActionIDCol, handler.ColumnTypeText),
|
|
handler.NewColumn(FlowOwnerRemovedCol, handler.ColumnTypeBool, handler.Default(false)),
|
|
},
|
|
handler.NewPrimaryKey(FlowInstanceIDCol, FlowTypeCol, FlowTriggerTypeCol, FlowResourceOwnerCol, FlowActionIDCol),
|
|
handler.WithIndex(handler.NewIndex("owner_removed", []string{FlowOwnerRemovedCol})),
|
|
),
|
|
)
|
|
}
|
|
|
|
func (p *flowProjection) Reducers() []handler.AggregateReducer {
|
|
return []handler.AggregateReducer{
|
|
{
|
|
Aggregate: org.AggregateType,
|
|
EventReducers: []handler.EventReducer{
|
|
{
|
|
Event: org.TriggerActionsSetEventType,
|
|
Reduce: p.reduceTriggerActionsSetEventType,
|
|
},
|
|
{
|
|
Event: org.FlowClearedEventType,
|
|
Reduce: p.reduceFlowClearedEventType,
|
|
},
|
|
{
|
|
Event: org.OrgRemovedEventType,
|
|
Reduce: p.reduceOwnerRemoved,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Aggregate: instance.AggregateType,
|
|
EventReducers: []handler.EventReducer{
|
|
{
|
|
Event: instance.InstanceRemovedEventType,
|
|
Reduce: reduceInstanceRemovedHelper(FlowInstanceIDCol),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (p *flowProjection) reduceTriggerActionsSetEventType(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*org.TriggerActionsSetEvent)
|
|
if !ok {
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-uYq4r", "reduce.wrong.event.type %s", org.TriggerActionsSetEventType)
|
|
}
|
|
stmts := make([]func(reader eventstore.Event) handler.Exec, len(e.ActionIDs)+1)
|
|
stmts[0] = handler.AddDeleteStatement(
|
|
[]handler.Condition{
|
|
handler.NewCond(FlowTypeCol, e.FlowType),
|
|
handler.NewCond(FlowTriggerTypeCol, e.TriggerType),
|
|
handler.NewCond(FlowResourceOwnerCol, e.Aggregate().ResourceOwner),
|
|
handler.NewCond(FlowInstanceIDCol, e.Aggregate().InstanceID),
|
|
},
|
|
)
|
|
for i, id := range e.ActionIDs {
|
|
stmts[i+1] = handler.AddCreateStatement(
|
|
[]handler.Column{
|
|
handler.NewCol(FlowResourceOwnerCol, e.Aggregate().ResourceOwner),
|
|
handler.NewCol(FlowInstanceIDCol, e.Aggregate().InstanceID),
|
|
handler.NewCol(FlowTypeCol, e.FlowType),
|
|
handler.NewCol(FlowChangeDateCol, e.CreationDate()),
|
|
handler.NewCol(FlowSequenceCol, e.Sequence()),
|
|
handler.NewCol(FlowTriggerTypeCol, e.TriggerType),
|
|
handler.NewCol(FlowActionIDCol, id),
|
|
handler.NewCol(FlowActionTriggerSequenceCol, i),
|
|
},
|
|
)
|
|
}
|
|
return handler.NewMultiStatement(e, stmts...), nil
|
|
}
|
|
|
|
func (p *flowProjection) reduceFlowClearedEventType(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*org.FlowClearedEvent)
|
|
if !ok {
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-uYq4r", "reduce.wrong.event.type %s", org.FlowClearedEventType)
|
|
}
|
|
return handler.NewDeleteStatement(
|
|
e,
|
|
[]handler.Condition{
|
|
handler.NewCond(FlowTypeCol, e.FlowType),
|
|
handler.NewCond(FlowResourceOwnerCol, e.Aggregate().ResourceOwner),
|
|
handler.NewCond(FlowInstanceIDCol, e.Aggregate().InstanceID),
|
|
},
|
|
), nil
|
|
}
|
|
|
|
func (p *flowProjection) reduceOwnerRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
if !ok {
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-Yd7WC", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
|
}
|
|
|
|
return handler.NewDeleteStatement(
|
|
e,
|
|
[]handler.Condition{
|
|
handler.NewCond(FlowInstanceIDCol, e.Aggregate().InstanceID),
|
|
handler.NewCond(FlowResourceOwnerCol, e.Aggregate().ID),
|
|
},
|
|
), nil
|
|
}
|