mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-16 12:58:00 +00:00
32bad3feb3
Some checks are pending
ZITADEL CI/CD / core (push) Waiting to run
ZITADEL CI/CD / console (push) Waiting to run
ZITADEL CI/CD / version (push) Waiting to run
ZITADEL CI/CD / compile (push) Blocked by required conditions
ZITADEL CI/CD / core-unit-test (push) Blocked by required conditions
ZITADEL CI/CD / core-integration-test (push) Blocked by required conditions
ZITADEL CI/CD / lint (push) Blocked by required conditions
ZITADEL CI/CD / container (push) Blocked by required conditions
ZITADEL CI/CD / e2e (push) Blocked by required conditions
ZITADEL CI/CD / release (push) Blocked by required conditions
Code Scanning / CodeQL-Build (go) (push) Waiting to run
Code Scanning / CodeQL-Build (javascript) (push) Waiting to run
# Which Problems Are Solved Milestones used existing events from a number of aggregates. OIDC session is one of them. We noticed in load-tests that the reduction of the oidc_session.added event into the milestone projection is a costly business with payload based conditionals. A milestone is reached once, but even then we remain subscribed to the OIDC events. This requires the projections.current_states to be updated continuously. # How the Problems Are Solved The milestone creation is refactored to use dedicated events instead. The command side decides when a milestone is reached and creates the reached event once for each milestone when required. # Additional Changes In order to prevent reached milestones being created twice, a migration script is provided. When the old `projections.milestones` table exist, the state is read from there and `v2` milestone aggregate events are created, with the original reached and pushed dates. # Additional Context - Closes https://github.com/zitadel/zitadel/issues/8800
102 lines
3.0 KiB
Go
102 lines
3.0 KiB
Go
package projection
|
|
|
|
import (
|
|
"context"
|
|
|
|
"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/milestone"
|
|
)
|
|
|
|
const (
|
|
MilestonesProjectionTable = "projections.milestones2"
|
|
|
|
MilestoneColumnInstanceID = "instance_id"
|
|
MilestoneColumnType = "type"
|
|
MilestoneColumnReachedDate = "reached_date"
|
|
MilestoneColumnPushedDate = "last_pushed_date"
|
|
)
|
|
|
|
type milestoneProjection struct{}
|
|
|
|
func newMilestoneProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
return handler.NewHandler(ctx, &config, &milestoneProjection{})
|
|
}
|
|
|
|
func (*milestoneProjection) Name() string {
|
|
return MilestonesProjectionTable
|
|
}
|
|
|
|
func (*milestoneProjection) Init() *old_handler.Check {
|
|
return handler.NewMultiTableCheck(
|
|
handler.NewTable([]*handler.InitColumn{
|
|
handler.NewColumn(MilestoneColumnInstanceID, handler.ColumnTypeText),
|
|
handler.NewColumn(MilestoneColumnType, handler.ColumnTypeEnum),
|
|
handler.NewColumn(MilestoneColumnReachedDate, handler.ColumnTypeTimestamp, handler.Nullable()),
|
|
handler.NewColumn(MilestoneColumnPushedDate, handler.ColumnTypeTimestamp, handler.Nullable()),
|
|
},
|
|
handler.NewPrimaryKey(MilestoneColumnInstanceID, MilestoneColumnType),
|
|
),
|
|
)
|
|
}
|
|
|
|
// Reducers implements handler.Projection.
|
|
func (p *milestoneProjection) Reducers() []handler.AggregateReducer {
|
|
return []handler.AggregateReducer{
|
|
{
|
|
Aggregate: milestone.AggregateType,
|
|
EventReducers: []handler.EventReducer{
|
|
{
|
|
Event: milestone.ReachedEventType,
|
|
Reduce: p.reduceReached,
|
|
},
|
|
{
|
|
Event: milestone.PushedEventType,
|
|
Reduce: p.reducePushed,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (p *milestoneProjection) reduceReached(event eventstore.Event) (*handler.Statement, error) {
|
|
e, err := assertEvent[*milestone.ReachedEvent](event)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return handler.NewCreateStatement(event, []handler.Column{
|
|
handler.NewCol(MilestoneColumnInstanceID, e.Agg.InstanceID),
|
|
handler.NewCol(MilestoneColumnType, e.MilestoneType),
|
|
handler.NewCol(MilestoneColumnReachedDate, e.GetReachedDate()),
|
|
}), nil
|
|
}
|
|
|
|
func (p *milestoneProjection) reducePushed(event eventstore.Event) (*handler.Statement, error) {
|
|
e, err := assertEvent[*milestone.PushedEvent](event)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if e.Agg.Version != milestone.AggregateVersion {
|
|
return handler.NewNoOpStatement(event), nil // Skip v1 events.
|
|
}
|
|
if e.MilestoneType != milestone.InstanceDeleted {
|
|
return handler.NewUpdateStatement(
|
|
event,
|
|
[]handler.Column{
|
|
handler.NewCol(MilestoneColumnPushedDate, e.GetPushedDate()),
|
|
},
|
|
[]handler.Condition{
|
|
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
|
|
handler.NewCond(MilestoneColumnType, e.MilestoneType),
|
|
},
|
|
), nil
|
|
}
|
|
return handler.NewDeleteStatement(
|
|
event,
|
|
[]handler.Condition{
|
|
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
|
|
},
|
|
), nil
|
|
}
|