mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 21:37:32 +00:00
perf(milestones): refactor (#8788)
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
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
This commit is contained in:
@@ -54,10 +54,6 @@ var (
|
||||
name: projection.MilestoneColumnType,
|
||||
table: milestonesTable,
|
||||
}
|
||||
MilestonePrimaryDomainColID = Column{
|
||||
name: projection.MilestoneColumnPrimaryDomain,
|
||||
table: milestonesTable,
|
||||
}
|
||||
MilestoneReachedDateColID = Column{
|
||||
name: projection.MilestoneColumnReachedDate,
|
||||
table: milestonesTable,
|
||||
@@ -76,7 +72,10 @@ func (q *Queries) SearchMilestones(ctx context.Context, instanceIDs []string, qu
|
||||
if len(instanceIDs) == 0 {
|
||||
instanceIDs = []string{authz.GetInstance(ctx).InstanceID()}
|
||||
}
|
||||
stmt, args, err := queries.toQuery(query).Where(sq.Eq{MilestoneInstanceIDColID.identifier(): instanceIDs}).ToSql()
|
||||
stmt, args, err := queries.toQuery(query).Where(
|
||||
sq.Eq{MilestoneInstanceIDColID.identifier(): instanceIDs},
|
||||
sq.Eq{InstanceDomainIsPrimaryCol.identifier(): true},
|
||||
).ToSql()
|
||||
if err != nil {
|
||||
return nil, zerrors.ThrowInternal(err, "QUERY-A9i5k", "Errors.Query.SQLStatement")
|
||||
}
|
||||
@@ -96,13 +95,14 @@ func (q *Queries) SearchMilestones(ctx context.Context, instanceIDs []string, qu
|
||||
func prepareMilestonesQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Rows) (*Milestones, error)) {
|
||||
return sq.Select(
|
||||
MilestoneInstanceIDColID.identifier(),
|
||||
MilestonePrimaryDomainColID.identifier(),
|
||||
InstanceDomainDomainCol.identifier(),
|
||||
MilestoneReachedDateColID.identifier(),
|
||||
MilestonePushedDateColID.identifier(),
|
||||
MilestoneTypeColID.identifier(),
|
||||
countColumn.identifier(),
|
||||
).
|
||||
From(milestonesTable.identifier() + db.Timetravel(call.Took(ctx))).
|
||||
LeftJoin(join(InstanceDomainInstanceIDCol, MilestoneInstanceIDColID)).
|
||||
PlaceholderFormat(sq.Dollar),
|
||||
func(rows *sql.Rows) (*Milestones, error) {
|
||||
milestones := make([]*Milestone, 0)
|
||||
|
@@ -11,13 +11,14 @@ import (
|
||||
|
||||
var (
|
||||
expectedMilestoneQuery = regexp.QuoteMeta(`
|
||||
SELECT projections.milestones.instance_id,
|
||||
projections.milestones.primary_domain,
|
||||
projections.milestones.reached_date,
|
||||
projections.milestones.last_pushed_date,
|
||||
projections.milestones.type,
|
||||
SELECT projections.milestones2.instance_id,
|
||||
projections.instance_domains.domain,
|
||||
projections.milestones2.reached_date,
|
||||
projections.milestones2.last_pushed_date,
|
||||
projections.milestones2.type,
|
||||
COUNT(*) OVER ()
|
||||
FROM projections.milestones AS OF SYSTEM TIME '-1 ms'
|
||||
FROM projections.milestones2 AS OF SYSTEM TIME '-1 ms'
|
||||
LEFT JOIN projections.instance_domains ON projections.milestones2.instance_id = projections.instance_domains.instance_id
|
||||
`)
|
||||
|
||||
milestoneCols = []string{
|
||||
|
@@ -14,8 +14,9 @@ func testEvent(
|
||||
eventType eventstore.EventType,
|
||||
aggregateType eventstore.AggregateType,
|
||||
data []byte,
|
||||
opts ...eventOption,
|
||||
) *repository.Event {
|
||||
return timedTestEvent(eventType, aggregateType, data, time.Now())
|
||||
return timedTestEvent(eventType, aggregateType, data, time.Now(), opts...)
|
||||
}
|
||||
|
||||
func toSystemEvent(event *repository.Event) *repository.Event {
|
||||
@@ -28,8 +29,9 @@ func timedTestEvent(
|
||||
aggregateType eventstore.AggregateType,
|
||||
data []byte,
|
||||
creationDate time.Time,
|
||||
opts ...eventOption,
|
||||
) *repository.Event {
|
||||
return &repository.Event{
|
||||
e := &repository.Event{
|
||||
Seq: 15,
|
||||
CreationDate: creationDate,
|
||||
Typ: eventType,
|
||||
@@ -42,6 +44,18 @@ func timedTestEvent(
|
||||
ID: "event-id",
|
||||
EditorUser: "editor-user",
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(e)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
type eventOption func(e *repository.Event)
|
||||
|
||||
func withVersion(v eventstore.Version) eventOption {
|
||||
return func(e *repository.Event) {
|
||||
e.Version = v
|
||||
}
|
||||
}
|
||||
|
||||
func baseEvent(*testing.T) eventstore.Event {
|
||||
|
@@ -2,35 +2,26 @@ package projection
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
internal_authz "github.com/zitadel/zitadel/internal/api/authz"
|
||||
"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/milestone"
|
||||
"github.com/zitadel/zitadel/internal/repository/oidcsession"
|
||||
"github.com/zitadel/zitadel/internal/repository/project"
|
||||
)
|
||||
|
||||
const (
|
||||
MilestonesProjectionTable = "projections.milestones"
|
||||
MilestonesProjectionTable = "projections.milestones2"
|
||||
|
||||
MilestoneColumnInstanceID = "instance_id"
|
||||
MilestoneColumnType = "type"
|
||||
MilestoneColumnPrimaryDomain = "primary_domain"
|
||||
MilestoneColumnReachedDate = "reached_date"
|
||||
MilestoneColumnPushedDate = "last_pushed_date"
|
||||
MilestoneColumnIgnoreClientIDs = "ignore_client_ids"
|
||||
MilestoneColumnInstanceID = "instance_id"
|
||||
MilestoneColumnType = "type"
|
||||
MilestoneColumnReachedDate = "reached_date"
|
||||
MilestoneColumnPushedDate = "last_pushed_date"
|
||||
)
|
||||
|
||||
type milestoneProjection struct {
|
||||
systemUsers map[string]*internal_authz.SystemAPIUser
|
||||
}
|
||||
type milestoneProjection struct{}
|
||||
|
||||
func newMilestoneProjection(ctx context.Context, config handler.Config, systemUsers map[string]*internal_authz.SystemAPIUser) *handler.Handler {
|
||||
return handler.NewHandler(ctx, &config, &milestoneProjection{systemUsers: systemUsers})
|
||||
func newMilestoneProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
||||
return handler.NewHandler(ctx, &config, &milestoneProjection{})
|
||||
}
|
||||
|
||||
func (*milestoneProjection) Name() string {
|
||||
@@ -44,8 +35,6 @@ func (*milestoneProjection) Init() *old_handler.Check {
|
||||
handler.NewColumn(MilestoneColumnType, handler.ColumnTypeEnum),
|
||||
handler.NewColumn(MilestoneColumnReachedDate, handler.ColumnTypeTimestamp, handler.Nullable()),
|
||||
handler.NewColumn(MilestoneColumnPushedDate, handler.ColumnTypeTimestamp, handler.Nullable()),
|
||||
handler.NewColumn(MilestoneColumnPrimaryDomain, handler.ColumnTypeText, handler.Nullable()),
|
||||
handler.NewColumn(MilestoneColumnIgnoreClientIDs, handler.ColumnTypeTextArray, handler.Nullable()),
|
||||
},
|
||||
handler.NewPrimaryKey(MilestoneColumnInstanceID, MilestoneColumnType),
|
||||
),
|
||||
@@ -55,183 +44,47 @@ func (*milestoneProjection) Init() *old_handler.Check {
|
||||
// Reducers implements handler.Projection.
|
||||
func (p *milestoneProjection) Reducers() []handler.AggregateReducer {
|
||||
return []handler.AggregateReducer{
|
||||
{
|
||||
Aggregate: instance.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: instance.InstanceAddedEventType,
|
||||
Reduce: p.reduceInstanceAdded,
|
||||
},
|
||||
{
|
||||
Event: instance.InstanceDomainPrimarySetEventType,
|
||||
Reduce: p.reduceInstanceDomainPrimarySet,
|
||||
},
|
||||
{
|
||||
Event: instance.InstanceRemovedEventType,
|
||||
Reduce: p.reduceInstanceRemoved,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Aggregate: project.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: project.ProjectAddedType,
|
||||
Reduce: p.reduceProjectAdded,
|
||||
},
|
||||
{
|
||||
Event: project.ApplicationAddedType,
|
||||
Reduce: p.reduceApplicationAdded,
|
||||
},
|
||||
{
|
||||
Event: project.OIDCConfigAddedType,
|
||||
Reduce: p.reduceOIDCConfigAdded,
|
||||
},
|
||||
{
|
||||
Event: project.APIConfigAddedType,
|
||||
Reduce: p.reduceAPIConfigAdded,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Aggregate: oidcsession.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: oidcsession.AddedType,
|
||||
Reduce: p.reduceOIDCSessionAdded,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Aggregate: milestone.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: milestone.ReachedEventType,
|
||||
Reduce: p.reduceReached,
|
||||
},
|
||||
{
|
||||
Event: milestone.PushedEventType,
|
||||
Reduce: p.reduceMilestonePushed,
|
||||
Reduce: p.reducePushed,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *milestoneProjection) reduceInstanceAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*instance.InstanceAddedEvent](event)
|
||||
func (p *milestoneProjection) reduceReached(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*milestone.ReachedEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
allTypes := milestone.AllTypes()
|
||||
statements := make([]func(eventstore.Event) handler.Exec, 0, len(allTypes))
|
||||
for _, msType := range allTypes {
|
||||
createColumns := []handler.Column{
|
||||
handler.NewCol(MilestoneColumnInstanceID, e.Aggregate().InstanceID),
|
||||
handler.NewCol(MilestoneColumnType, msType),
|
||||
}
|
||||
if msType == milestone.InstanceCreated {
|
||||
createColumns = append(createColumns, handler.NewCol(MilestoneColumnReachedDate, event.CreatedAt()))
|
||||
}
|
||||
statements = append(statements, handler.AddCreateStatement(createColumns))
|
||||
}
|
||||
return handler.NewMultiStatement(e, statements...), nil
|
||||
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) reduceInstanceDomainPrimarySet(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*instance.DomainPrimarySetEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return handler.NewUpdateStatement(
|
||||
e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(MilestoneColumnPrimaryDomain, e.Domain),
|
||||
},
|
||||
[]handler.Condition{
|
||||
handler.NewCond(MilestoneColumnInstanceID, e.Aggregate().InstanceID),
|
||||
handler.NewIsNullCond(MilestoneColumnPushedDate),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
|
||||
func (p *milestoneProjection) reduceProjectAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
if _, err := assertEvent[*project.ProjectAddedEvent](event); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p.reduceReachedIfUserEventFunc(milestone.ProjectCreated)(event)
|
||||
}
|
||||
|
||||
func (p *milestoneProjection) reduceApplicationAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
if _, err := assertEvent[*project.ApplicationAddedEvent](event); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p.reduceReachedIfUserEventFunc(milestone.ApplicationCreated)(event)
|
||||
}
|
||||
|
||||
func (p *milestoneProjection) reduceOIDCConfigAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*project.OIDCConfigAddedEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p.reduceAppConfigAdded(e, e.ClientID)
|
||||
}
|
||||
|
||||
func (p *milestoneProjection) reduceAPIConfigAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*project.APIConfigAddedEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p.reduceAppConfigAdded(e, e.ClientID)
|
||||
}
|
||||
|
||||
func (p *milestoneProjection) reduceOIDCSessionAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*oidcsession.AddedEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
statements := []func(eventstore.Event) handler.Exec{
|
||||
handler.AddUpdateStatement(
|
||||
[]handler.Column{
|
||||
handler.NewCol(MilestoneColumnReachedDate, event.CreatedAt()),
|
||||
},
|
||||
[]handler.Condition{
|
||||
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
|
||||
handler.NewCond(MilestoneColumnType, milestone.AuthenticationSucceededOnInstance),
|
||||
handler.NewIsNullCond(MilestoneColumnReachedDate),
|
||||
},
|
||||
),
|
||||
}
|
||||
// We ignore authentications without app, for example JWT profile or PAT
|
||||
if e.ClientID != "" {
|
||||
statements = append(statements, handler.AddUpdateStatement(
|
||||
[]handler.Column{
|
||||
handler.NewCol(MilestoneColumnReachedDate, event.CreatedAt()),
|
||||
},
|
||||
[]handler.Condition{
|
||||
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
|
||||
handler.NewCond(MilestoneColumnType, milestone.AuthenticationSucceededOnApplication),
|
||||
handler.Not(handler.NewTextArrayContainsCond(MilestoneColumnIgnoreClientIDs, e.ClientID)),
|
||||
handler.NewIsNullCond(MilestoneColumnReachedDate),
|
||||
},
|
||||
))
|
||||
}
|
||||
return handler.NewMultiStatement(e, statements...), nil
|
||||
}
|
||||
|
||||
func (p *milestoneProjection) reduceInstanceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
||||
if _, err := assertEvent[*instance.InstanceRemovedEvent](event); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p.reduceReachedFunc(milestone.InstanceDeleted)(event)
|
||||
}
|
||||
|
||||
func (p *milestoneProjection) reduceMilestonePushed(event eventstore.Event) (*handler.Statement, error) {
|
||||
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, event.CreatedAt()),
|
||||
handler.NewCol(MilestoneColumnPushedDate, e.GetPushedDate()),
|
||||
},
|
||||
[]handler.Condition{
|
||||
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
|
||||
@@ -246,58 +99,3 @@ func (p *milestoneProjection) reduceMilestonePushed(event eventstore.Event) (*ha
|
||||
},
|
||||
), nil
|
||||
}
|
||||
|
||||
func (p *milestoneProjection) reduceReachedIfUserEventFunc(msType milestone.Type) func(event eventstore.Event) (*handler.Statement, error) {
|
||||
return func(event eventstore.Event) (*handler.Statement, error) {
|
||||
if p.isSystemEvent(event) {
|
||||
return handler.NewNoOpStatement(event), nil
|
||||
}
|
||||
return p.reduceReachedFunc(msType)(event)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *milestoneProjection) reduceReachedFunc(msType milestone.Type) func(event eventstore.Event) (*handler.Statement, error) {
|
||||
return func(event eventstore.Event) (*handler.Statement, error) {
|
||||
return handler.NewUpdateStatement(event, []handler.Column{
|
||||
handler.NewCol(MilestoneColumnReachedDate, event.CreatedAt()),
|
||||
},
|
||||
[]handler.Condition{
|
||||
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
|
||||
handler.NewCond(MilestoneColumnType, msType),
|
||||
handler.NewIsNullCond(MilestoneColumnReachedDate),
|
||||
}), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *milestoneProjection) reduceAppConfigAdded(event eventstore.Event, clientID string) (*handler.Statement, error) {
|
||||
if !p.isSystemEvent(event) {
|
||||
return handler.NewNoOpStatement(event), nil
|
||||
}
|
||||
return handler.NewUpdateStatement(
|
||||
event,
|
||||
[]handler.Column{
|
||||
handler.NewArrayAppendCol(MilestoneColumnIgnoreClientIDs, clientID),
|
||||
},
|
||||
[]handler.Condition{
|
||||
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
|
||||
handler.NewCond(MilestoneColumnType, milestone.AuthenticationSucceededOnApplication),
|
||||
handler.NewIsNullCond(MilestoneColumnReachedDate),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
|
||||
func (p *milestoneProjection) isSystemEvent(event eventstore.Event) bool {
|
||||
if userId, err := strconv.Atoi(event.Creator()); err == nil && userId > 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
// check if it is a hard coded event creator
|
||||
for _, creator := range []string{"", "system", "OIDC", "LOGIN", "SYSTEM"} {
|
||||
if creator == event.Creator() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
_, ok := p.systemUsers[event.Creator()]
|
||||
return ok
|
||||
}
|
||||
|
@@ -4,13 +4,11 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/database"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
||||
"github.com/zitadel/zitadel/internal/repository/instance"
|
||||
"github.com/zitadel/zitadel/internal/repository/milestone"
|
||||
"github.com/zitadel/zitadel/internal/repository/oidcsession"
|
||||
"github.com/zitadel/zitadel/internal/repository/project"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
@@ -19,6 +17,8 @@ func TestMilestonesProjection_reduces(t *testing.T) {
|
||||
event func(t *testing.T) eventstore.Event
|
||||
}
|
||||
now := time.Now()
|
||||
date, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
|
||||
require.NoError(t, err)
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
@@ -29,292 +29,54 @@ func TestMilestonesProjection_reduces(t *testing.T) {
|
||||
name: "reduceInstanceAdded",
|
||||
args: args{
|
||||
event: getEvent(timedTestEvent(
|
||||
instance.InstanceAddedEventType,
|
||||
instance.AggregateType,
|
||||
[]byte(`{}`),
|
||||
milestone.ReachedEventType,
|
||||
milestone.AggregateType,
|
||||
[]byte(`{"type": "instance_created"}`),
|
||||
now,
|
||||
), instance.InstanceAddedEventMapper),
|
||||
withVersion(milestone.AggregateVersion),
|
||||
), milestone.ReachedEventMapper),
|
||||
},
|
||||
reduce: (&milestoneProjection{}).reduceInstanceAdded,
|
||||
reduce: (&milestoneProjection{}).reduceReached,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("instance"),
|
||||
aggregateType: eventstore.AggregateType("milestone"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "INSERT INTO projections.milestones (instance_id, type, reached_date) VALUES ($1, $2, $3)",
|
||||
expectedStmt: "INSERT INTO projections.milestones2 (instance_id, type, reached_date) VALUES ($1, $2, $3)",
|
||||
expectedArgs: []interface{}{
|
||||
"instance-id",
|
||||
milestone.InstanceCreated,
|
||||
now,
|
||||
},
|
||||
},
|
||||
{
|
||||
expectedStmt: "INSERT INTO projections.milestones (instance_id, type) VALUES ($1, $2)",
|
||||
expectedArgs: []interface{}{
|
||||
"instance-id",
|
||||
milestone.AuthenticationSucceededOnInstance,
|
||||
},
|
||||
},
|
||||
{
|
||||
expectedStmt: "INSERT INTO projections.milestones (instance_id, type) VALUES ($1, $2)",
|
||||
expectedArgs: []interface{}{
|
||||
"instance-id",
|
||||
milestone.ProjectCreated,
|
||||
},
|
||||
},
|
||||
{
|
||||
expectedStmt: "INSERT INTO projections.milestones (instance_id, type) VALUES ($1, $2)",
|
||||
expectedArgs: []interface{}{
|
||||
"instance-id",
|
||||
milestone.ApplicationCreated,
|
||||
},
|
||||
},
|
||||
{
|
||||
expectedStmt: "INSERT INTO projections.milestones (instance_id, type) VALUES ($1, $2)",
|
||||
expectedArgs: []interface{}{
|
||||
"instance-id",
|
||||
milestone.AuthenticationSucceededOnApplication,
|
||||
},
|
||||
},
|
||||
{
|
||||
expectedStmt: "INSERT INTO projections.milestones (instance_id, type) VALUES ($1, $2)",
|
||||
expectedArgs: []interface{}{
|
||||
"instance-id",
|
||||
milestone.InstanceDeleted,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceInstancePrimaryDomainSet",
|
||||
args: args{
|
||||
event: getEvent(testEvent(
|
||||
instance.InstanceDomainPrimarySetEventType,
|
||||
instance.AggregateType,
|
||||
[]byte(`{"domain": "my.domain"}`),
|
||||
), instance.DomainPrimarySetEventMapper),
|
||||
},
|
||||
reduce: (&milestoneProjection{}).reduceInstanceDomainPrimarySet,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("instance"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE projections.milestones SET primary_domain = $1 WHERE (instance_id = $2) AND (last_pushed_date IS NULL)",
|
||||
expectedArgs: []interface{}{
|
||||
"my.domain",
|
||||
"instance-id",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceProjectAdded",
|
||||
name: "reduceInstanceAdded with reached date",
|
||||
args: args{
|
||||
event: getEvent(timedTestEvent(
|
||||
project.ProjectAddedType,
|
||||
project.AggregateType,
|
||||
[]byte(`{}`),
|
||||
milestone.ReachedEventType,
|
||||
milestone.AggregateType,
|
||||
[]byte(`{"type": "instance_created", "reachedDate":"2006-01-02T15:04:05Z"}`),
|
||||
now,
|
||||
), project.ProjectAddedEventMapper),
|
||||
withVersion(milestone.AggregateVersion),
|
||||
), milestone.ReachedEventMapper),
|
||||
},
|
||||
reduce: (&milestoneProjection{}).reduceProjectAdded,
|
||||
reduce: (&milestoneProjection{}).reduceReached,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("project"),
|
||||
aggregateType: eventstore.AggregateType("milestone"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE projections.milestones SET reached_date = $1 WHERE (instance_id = $2) AND (type = $3) AND (reached_date IS NULL)",
|
||||
expectedStmt: "INSERT INTO projections.milestones2 (instance_id, type, reached_date) VALUES ($1, $2, $3)",
|
||||
expectedArgs: []interface{}{
|
||||
now,
|
||||
"instance-id",
|
||||
milestone.ProjectCreated,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceApplicationAdded",
|
||||
args: args{
|
||||
event: getEvent(timedTestEvent(
|
||||
project.ApplicationAddedType,
|
||||
project.AggregateType,
|
||||
[]byte(`{}`),
|
||||
now,
|
||||
), project.ApplicationAddedEventMapper),
|
||||
},
|
||||
reduce: (&milestoneProjection{}).reduceApplicationAdded,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("project"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE projections.milestones SET reached_date = $1 WHERE (instance_id = $2) AND (type = $3) AND (reached_date IS NULL)",
|
||||
expectedArgs: []interface{}{
|
||||
now,
|
||||
"instance-id",
|
||||
milestone.ApplicationCreated,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceOIDCConfigAdded user event",
|
||||
args: args{
|
||||
event: getEvent(testEvent(
|
||||
project.OIDCConfigAddedType,
|
||||
project.AggregateType,
|
||||
[]byte(`{}`),
|
||||
), project.OIDCConfigAddedEventMapper),
|
||||
},
|
||||
reduce: (&milestoneProjection{}).reduceOIDCConfigAdded,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("project"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceOIDCConfigAdded system event",
|
||||
args: args{
|
||||
event: getEvent(toSystemEvent(testEvent(
|
||||
project.OIDCConfigAddedType,
|
||||
project.AggregateType,
|
||||
[]byte(`{"clientId": "client-id"}`),
|
||||
)), project.OIDCConfigAddedEventMapper),
|
||||
},
|
||||
reduce: (&milestoneProjection{}).reduceOIDCConfigAdded,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("project"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE projections.milestones SET ignore_client_ids = array_append(ignore_client_ids, $1) WHERE (instance_id = $2) AND (type = $3) AND (reached_date IS NULL)",
|
||||
expectedArgs: []interface{}{
|
||||
"client-id",
|
||||
"instance-id",
|
||||
milestone.AuthenticationSucceededOnApplication,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceAPIConfigAdded user event",
|
||||
args: args{
|
||||
event: getEvent(testEvent(
|
||||
project.APIConfigAddedType,
|
||||
project.AggregateType,
|
||||
[]byte(`{}`),
|
||||
), project.APIConfigAddedEventMapper),
|
||||
},
|
||||
reduce: (&milestoneProjection{}).reduceAPIConfigAdded,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("project"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceAPIConfigAdded system event",
|
||||
args: args{
|
||||
event: getEvent(toSystemEvent(testEvent(
|
||||
project.APIConfigAddedType,
|
||||
project.AggregateType,
|
||||
[]byte(`{"clientId": "client-id"}`),
|
||||
)), project.APIConfigAddedEventMapper),
|
||||
},
|
||||
reduce: (&milestoneProjection{}).reduceAPIConfigAdded,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("project"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE projections.milestones SET ignore_client_ids = array_append(ignore_client_ids, $1) WHERE (instance_id = $2) AND (type = $3) AND (reached_date IS NULL)",
|
||||
expectedArgs: []interface{}{
|
||||
"client-id",
|
||||
"instance-id",
|
||||
milestone.AuthenticationSucceededOnApplication,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceOIDCSessionAdded",
|
||||
args: args{
|
||||
event: getEvent(timedTestEvent(
|
||||
oidcsession.AddedType,
|
||||
oidcsession.AggregateType,
|
||||
[]byte(`{"clientID": "client-id"}`),
|
||||
now,
|
||||
), eventstore.GenericEventMapper[oidcsession.AddedEvent]),
|
||||
},
|
||||
reduce: (&milestoneProjection{}).reduceOIDCSessionAdded,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("oidc_session"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE projections.milestones SET reached_date = $1 WHERE (instance_id = $2) AND (type = $3) AND (reached_date IS NULL)",
|
||||
expectedArgs: []interface{}{
|
||||
now,
|
||||
"instance-id",
|
||||
milestone.AuthenticationSucceededOnInstance,
|
||||
},
|
||||
},
|
||||
{
|
||||
expectedStmt: "UPDATE projections.milestones SET reached_date = $1 WHERE (instance_id = $2) AND (type = $3) AND (NOT (ignore_client_ids @> $4)) AND (reached_date IS NULL)",
|
||||
expectedArgs: []interface{}{
|
||||
now,
|
||||
"instance-id",
|
||||
milestone.AuthenticationSucceededOnApplication,
|
||||
database.TextArray[string]{"client-id"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceInstanceRemoved",
|
||||
args: args{
|
||||
event: getEvent(timedTestEvent(
|
||||
instance.InstanceRemovedEventType,
|
||||
instance.AggregateType,
|
||||
[]byte(`{}`),
|
||||
now,
|
||||
), instance.InstanceRemovedEventMapper),
|
||||
},
|
||||
reduce: (&milestoneProjection{}).reduceInstanceRemoved,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("instance"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE projections.milestones SET reached_date = $1 WHERE (instance_id = $2) AND (type = $3) AND (reached_date IS NULL)",
|
||||
expectedArgs: []interface{}{
|
||||
now,
|
||||
"instance-id",
|
||||
milestone.InstanceDeleted,
|
||||
milestone.InstanceCreated,
|
||||
date,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -327,18 +89,19 @@ func TestMilestonesProjection_reduces(t *testing.T) {
|
||||
event: getEvent(timedTestEvent(
|
||||
milestone.PushedEventType,
|
||||
milestone.AggregateType,
|
||||
[]byte(`{"type": "ProjectCreated"}`),
|
||||
[]byte(`{"type": "project_created"}`),
|
||||
now,
|
||||
withVersion(milestone.AggregateVersion),
|
||||
), milestone.PushedEventMapper),
|
||||
},
|
||||
reduce: (&milestoneProjection{}).reduceMilestonePushed,
|
||||
reduce: (&milestoneProjection{}).reducePushed,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("milestone"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE projections.milestones SET last_pushed_date = $1 WHERE (instance_id = $2) AND (type = $3)",
|
||||
expectedStmt: "UPDATE projections.milestones2 SET last_pushed_date = $1 WHERE (instance_id = $2) AND (type = $3)",
|
||||
expectedArgs: []interface{}{
|
||||
now,
|
||||
"instance-id",
|
||||
@@ -349,23 +112,53 @@ func TestMilestonesProjection_reduces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceMilestonePushed normal milestone with pushed date",
|
||||
args: args{
|
||||
event: getEvent(timedTestEvent(
|
||||
milestone.PushedEventType,
|
||||
milestone.AggregateType,
|
||||
[]byte(`{"type": "project_created", "pushedDate":"2006-01-02T15:04:05Z"}`),
|
||||
now,
|
||||
withVersion(milestone.AggregateVersion),
|
||||
), milestone.PushedEventMapper),
|
||||
},
|
||||
reduce: (&milestoneProjection{}).reducePushed,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("milestone"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE projections.milestones2 SET last_pushed_date = $1 WHERE (instance_id = $2) AND (type = $3)",
|
||||
expectedArgs: []interface{}{
|
||||
date,
|
||||
"instance-id",
|
||||
milestone.ProjectCreated,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceMilestonePushed instance deleted milestone",
|
||||
args: args{
|
||||
event: getEvent(testEvent(
|
||||
milestone.PushedEventType,
|
||||
milestone.AggregateType,
|
||||
[]byte(`{"type": "InstanceDeleted"}`),
|
||||
[]byte(`{"type": "instance_deleted"}`),
|
||||
withVersion(milestone.AggregateVersion),
|
||||
), milestone.PushedEventMapper),
|
||||
},
|
||||
reduce: (&milestoneProjection{}).reduceMilestonePushed,
|
||||
reduce: (&milestoneProjection{}).reducePushed,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("milestone"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "DELETE FROM projections.milestones WHERE (instance_id = $1)",
|
||||
expectedStmt: "DELETE FROM projections.milestones2 WHERE (instance_id = $1)",
|
||||
expectedArgs: []interface{}{
|
||||
"instance-id",
|
||||
},
|
||||
|
@@ -156,7 +156,7 @@ func Create(ctx context.Context, sqlClient *database.DB, es handler.EventStore,
|
||||
DeviceAuthProjection = newDeviceAuthProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["device_auth"]))
|
||||
SessionProjection = newSessionProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["sessions"]))
|
||||
AuthRequestProjection = newAuthRequestProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["auth_requests"]))
|
||||
MilestoneProjection = newMilestoneProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["milestones"]), systemUsers)
|
||||
MilestoneProjection = newMilestoneProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["milestones"]))
|
||||
QuotaProjection = newQuotaProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["quotas"]))
|
||||
LimitsProjection = newLimitsProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["limits"]))
|
||||
RestrictionsProjection = newRestrictionsProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["restrictions"]))
|
||||
|
Reference in New Issue
Block a user