From c2a5b785fb627a92d4bde6cb44fb8bdfe11ee116 Mon Sep 17 00:00:00 2001 From: Stefan Benz <46600784+stebenz@users.noreply.github.com> Date: Thu, 20 Oct 2022 13:36:52 +0100 Subject: [PATCH] feat: instance remove (#4345) * feat(instance): add remove instance event with projections cleanup * fix(instance): corrected used id to clean up projections * fix merge * fix: correct unit test projection names * fix: current sequence of lists and query for ensuring keypair based projections Co-authored-by: Livio Spring Co-authored-by: Fabi <38692350+hifabienne@users.noreply.github.com> --- internal/api/grpc/system/instance.go | 10 +++ internal/api/oidc/key.go | 13 +-- internal/api/saml/certificate.go | 3 +- internal/command/instance.go | 48 +++++++++++ internal/command/instance_domain.go | 13 ++- internal/command/instance_domain_model.go | 62 ++++++++++++++ internal/domain/request.go | 2 +- .../eventstore/handler/handler_projection.go | 20 ++--- internal/query/current_sequence.go | 2 +- internal/query/projection/action.go | 12 ++- internal/query/projection/action_test.go | 34 ++++++-- internal/query/projection/app.go | 10 +++ internal/query/projection/app_test.go | 43 ++++++---- internal/query/projection/authn_key.go | 10 +++ internal/query/projection/authn_key_test.go | 44 ++++++---- internal/query/projection/custom_text.go | 4 + internal/query/projection/custom_text_test.go | 34 ++++++-- .../query/projection/debug_notification.go | 4 + .../debug_notification_provider_test.go | 34 ++++++-- internal/query/projection/domain_policy.go | 4 + .../query/projection/domain_policy_test.go | 33 ++++++-- internal/query/projection/event_test.go | 5 +- internal/query/projection/flow.go | 10 +++ internal/query/projection/flow_test.go | 31 ++++++- internal/query/projection/idp.go | 4 + .../query/projection/idp_login_policy_link.go | 4 + .../projection/idp_login_policy_link_test.go | 38 ++++++--- internal/query/projection/idp_test.go | 50 ++++++------ internal/query/projection/idp_user_link.go | 4 + .../query/projection/idp_user_link_test.go | 35 ++++++-- internal/query/projection/instance.go | 19 +++++ internal/query/projection/instance_domain.go | 4 + .../query/projection/instance_domain_test.go | 30 ++++++- internal/query/projection/instance_member.go | 4 + .../query/projection/instance_member_test.go | 33 ++++++-- internal/query/projection/instance_test.go | 81 ++++++++++++------- internal/query/projection/key.go | 10 +++ internal/query/projection/key_test.go | 32 +++++++- internal/query/projection/label_policy.go | 4 + .../query/projection/label_policy_test.go | 57 +++++++------ internal/query/projection/lockout_policy.go | 4 + .../query/projection/lockout_policy_test.go | 33 ++++++-- internal/query/projection/login_name.go | 4 + internal/query/projection/login_name_test.go | 44 ++++++---- internal/query/projection/login_policy.go | 4 + .../query/projection/login_policy_test.go | 41 ++++++---- internal/query/projection/mail_template.go | 9 +++ .../query/projection/mail_template_test.go | 33 ++++++-- .../query/projection/message_text_test.go | 45 ++++++----- internal/query/projection/message_texts.go | 4 + internal/query/projection/oidc_settings.go | 9 +++ .../query/projection/oidc_settings_test.go | 30 ++++++- internal/query/projection/org.go | 10 +++ internal/query/projection/org_domain.go | 10 +++ internal/query/projection/org_domain_test.go | 34 ++++++-- internal/query/projection/org_member.go | 10 +++ internal/query/projection/org_member_test.go | 35 ++++++-- .../query/projection/org_metadata_test.go | 6 +- internal/query/projection/org_test.go | 35 ++++++-- .../query/projection/password_age_policy.go | 4 + .../projection/password_age_policy_test.go | 33 ++++++-- .../projection/password_complexity_policy.go | 4 + .../password_complexity_policy_test.go | 33 ++++++-- internal/query/projection/privacy_policy.go | 4 + .../query/projection/privacy_policy_test.go | 32 ++++++-- internal/query/projection/project.go | 10 +++ internal/query/projection/project_grant.go | 10 +++ .../query/projection/project_grant_member.go | 18 +++++ .../projection/project_grant_member_test.go | 36 ++++++--- .../query/projection/project_grant_test.go | 36 +++++++-- internal/query/projection/project_member.go | 10 +++ .../query/projection/project_member_test.go | 36 +++++++-- internal/query/projection/project_role.go | 10 +++ .../query/projection/project_role_test.go | 34 ++++++-- internal/query/projection/project_test.go | 35 ++++++-- .../query/projection/secret_generator_test.go | 5 +- internal/query/projection/sms.go | 4 + internal/query/projection/sms_test.go | 34 ++++++-- internal/query/projection/smtp.go | 4 + internal/query/projection/smtp_test.go | 31 ++++++- internal/query/projection/user.go | 10 +++ internal/query/projection/user_auth_method.go | 10 +++ .../query/projection/user_auth_method_test.go | 35 ++++++-- internal/query/projection/user_grant.go | 10 +++ internal/query/projection/user_grant_test.go | 41 +++++++--- internal/query/projection/user_metadata.go | 10 +++ .../query/projection/user_metadata_test.go | 33 ++++++-- .../projection/user_personal_access_token.go | 10 +++ .../user_personal_access_token_test.go | 32 +++++++- internal/query/projection/user_test.go | 66 +++++++-------- 90 files changed, 1548 insertions(+), 426 deletions(-) diff --git a/internal/api/grpc/system/instance.go b/internal/api/grpc/system/instance.go index 668d903f2..b5213b7d9 100644 --- a/internal/api/grpc/system/instance.go +++ b/internal/api/grpc/system/instance.go @@ -62,6 +62,16 @@ func (s *Server) UpdateInstance(ctx context.Context, req *system_pb.UpdateInstan }, nil } +func (s *Server) RemoveInstance(ctx context.Context, req *system_pb.RemoveInstanceRequest) (*system_pb.RemoveInstanceResponse, error) { + details, err := s.command.RemoveInstance(ctx, req.InstanceId) + if err != nil { + return nil, err + } + return &system_pb.RemoveInstanceResponse{ + Details: object.AddToDetailsPb(details.Sequence, details.EventDate, details.ResourceOwner), + }, nil +} + func (s *Server) ExistsDomain(ctx context.Context, req *system_pb.ExistsDomainRequest) (*system_pb.ExistsDomainResponse, error) { domainQuery, err := query.NewInstanceDomainDomainSearchQuery(query.TextEqualsIgnoreCase, req.Domain) if err != nil { diff --git a/internal/api/oidc/key.go b/internal/api/oidc/key.go index f19b2d80c..ccb5ef839 100644 --- a/internal/api/oidc/key.go +++ b/internal/api/oidc/key.go @@ -14,6 +14,7 @@ import ( "github.com/zitadel/zitadel/internal/errors" "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/query" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/keypair" "github.com/zitadel/zitadel/internal/telemetry/tracing" ) @@ -29,7 +30,7 @@ const ( gracefulPeriod = 10 * time.Minute ) -//SigningKey wraps the query.PrivateKey to implement the op.SigningKey interface +// SigningKey wraps the query.PrivateKey to implement the op.SigningKey interface type SigningKey struct { algorithm jose.SignatureAlgorithm id string @@ -48,7 +49,7 @@ func (s *SigningKey) ID() string { return s.id } -//PublicKey wraps the query.PublicKey to implement the op.Key interface +// PublicKey wraps the query.PublicKey to implement the op.Key interface type PublicKey struct { key query.PublicKey } @@ -69,7 +70,7 @@ func (s *PublicKey) ID() string { return s.key.ID() } -//KeySet implements the op.Storage interface +// KeySet implements the op.Storage interface func (o *OPStorage) KeySet(ctx context.Context) (keys []op.Key, err error) { ctx, span := tracing.NewSpan(ctx) defer func() { span.EndWithError(err) }() @@ -87,7 +88,7 @@ func (o *OPStorage) KeySet(ctx context.Context) (keys []op.Key, err error) { return keys, err } -//SignatureAlgorithms implements the op.Storage interface +// SignatureAlgorithms implements the op.Storage interface func (o *OPStorage) SignatureAlgorithms(ctx context.Context) ([]jose.SignatureAlgorithm, error) { key, err := o.SigningKey(ctx) if err != nil { @@ -96,7 +97,7 @@ func (o *OPStorage) SignatureAlgorithms(ctx context.Context) ([]jose.SignatureAl return []jose.SignatureAlgorithm{key.SignatureAlgorithm()}, nil } -//SigningKey implements the op.Storage interface +// SigningKey implements the op.Storage interface func (o *OPStorage) SigningKey(ctx context.Context) (key op.SigningKey, err error) { err = retry(func() error { key, err = o.getSigningKey(ctx) @@ -186,7 +187,7 @@ func (o *OPStorage) getMaxKeySequence(ctx context.Context) (uint64, error) { eventstore.NewSearchQueryBuilder(eventstore.ColumnsMaxSequence). ResourceOwner(authz.GetInstance(ctx).InstanceID()). AddQuery(). - AggregateTypes(keypair.AggregateType). + AggregateTypes(keypair.AggregateType, instance.AggregateType). Builder(), ) } diff --git a/internal/api/saml/certificate.go b/internal/api/saml/certificate.go index 432fcd9e5..8d34aeb4c 100644 --- a/internal/api/saml/certificate.go +++ b/internal/api/saml/certificate.go @@ -15,6 +15,7 @@ import ( "github.com/zitadel/zitadel/internal/errors" "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/query" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/keypair" ) @@ -156,7 +157,7 @@ func (p *Storage) getMaxKeySequence(ctx context.Context) (uint64, error) { eventstore.NewSearchQueryBuilder(eventstore.ColumnsMaxSequence). ResourceOwner(authz.GetInstance(ctx).InstanceID()). AddQuery(). - AggregateTypes(keypair.AggregateType). + AggregateTypes(keypair.AggregateType, instance.AggregateType). Builder(), ) } diff --git a/internal/command/instance.go b/internal/command/instance.go index 2c9283c60..7bf855a63 100644 --- a/internal/command/instance.go +++ b/internal/command/instance.go @@ -606,3 +606,51 @@ func getSystemConfigWriteModel(ctx context.Context, filter preparation.FilterToQ err = writeModel.Reduce() return writeModel, err } + +func (c *Commands) RemoveInstance(ctx context.Context, id string) (*domain.ObjectDetails, error) { + instanceAgg := instance.NewAggregate(id) + cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, c.prepareRemoveInstance(instanceAgg)) + if err != nil { + return nil, err + } + + events, err := c.eventstore.Push(ctx, cmds...) + if err != nil { + return nil, err + } + + return &domain.ObjectDetails{ + Sequence: events[len(events)-1].Sequence(), + EventDate: events[len(events)-1].CreationDate(), + ResourceOwner: events[len(events)-1].Aggregate().InstanceID, + }, nil +} + +func (c *Commands) prepareRemoveInstance(a *instance.Aggregate) preparation.Validation { + return func() (preparation.CreateCommands, error) { + return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) { + writeModel, err := c.getInstanceWriteModelByID(ctx, a.ID) + if err != nil { + return nil, errors.ThrowPreconditionFailed(err, "COMMA-pax9m3", "Errors.Instance.NotFound") + } + events := []eventstore.Command{instance.NewInstanceRemovedEvent(ctx, &a.Aggregate, writeModel.Name)} + + domainsWriteModel, err := c.getInstanceDomainsWriteModel(ctx, a.ID) + if err == nil { + for _, domainName := range domainsWriteModel.Domains { + events = append(events, instance.NewDomainRemovedEvent(ctx, &a.Aggregate, domainName)) + } + } + return events, nil + }, nil + } +} + +func (c *Commands) getInstanceWriteModelByID(ctx context.Context, orgID string) (*InstanceWriteModel, error) { + instanceWriteModel := NewInstanceWriteModel(orgID) + err := c.eventstore.FilterToQueryReducer(ctx, instanceWriteModel) + if err != nil { + return nil, err + } + return instanceWriteModel, nil +} diff --git a/internal/command/instance_domain.go b/internal/command/instance_domain.go index 29360f6d8..54e4121bc 100644 --- a/internal/command/instance_domain.go +++ b/internal/command/instance_domain.go @@ -154,8 +154,8 @@ func (c *Commands) updateConsoleRedirectURIs(ctx context.Context, filter prepara ) } -//checkUpdateConsoleRedirectURIs validates if the required console uri is present in the redirect_uris and post_logout_redirect_uris -//it will return true only if present in both list, otherwise false +// checkUpdateConsoleRedirectURIs validates if the required console uri is present in the redirect_uris and post_logout_redirect_uris +// it will return true only if present in both list, otherwise false func (c *Commands) checkUpdateConsoleRedirectURIs(instanceDomain string, redirectURIs, postLogoutRedirectURIs []string) bool { redirectURI := http.BuildHTTP(instanceDomain, c.externalPort, c.externalSecure) + consoleRedirectPath if !containsURI(redirectURIs, redirectURI) { @@ -226,3 +226,12 @@ func containsURI(uris []string, uri string) bool { } return false } + +func (c *Commands) getInstanceDomainsWriteModel(ctx context.Context, instanceID string) (*InstanceDomainsWriteModel, error) { + domainsWriteModel := NewInstanceDomainsWriteModel(instanceID) + err := c.eventstore.FilterToQueryReducer(ctx, domainsWriteModel) + if err != nil { + return nil, err + } + return domainsWriteModel, nil +} diff --git a/internal/command/instance_domain_model.go b/internal/command/instance_domain_model.go index cc8c44400..06af63f8b 100644 --- a/internal/command/instance_domain_model.go +++ b/internal/command/instance_domain_model.go @@ -69,3 +69,65 @@ func (wm *InstanceDomainWriteModel) Query() *eventstore.SearchQueryBuilder { instance.InstanceDomainRemovedEventType). Builder() } + +type InstanceDomainsWriteModel struct { + eventstore.WriteModel + + Domains []string +} + +func (wm *InstanceDomainsWriteModel) Query() *eventstore.SearchQueryBuilder { + return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent). + ResourceOwner(wm.ResourceOwner). + AddQuery(). + AggregateTypes(instance.AggregateType). + AggregateIDs(wm.AggregateID). + EventTypes( + instance.InstanceDomainAddedEventType, + instance.InstanceDomainRemovedEventType). + Builder() +} + +func NewInstanceDomainsWriteModel(instanceID string) *InstanceDomainsWriteModel { + return &InstanceDomainsWriteModel{ + WriteModel: eventstore.WriteModel{ + AggregateID: instanceID, + ResourceOwner: instanceID, + }, + Domains: []string{}, + } +} + +func (wm *InstanceDomainsWriteModel) AppendEvents(events ...eventstore.Event) { + for _, event := range events { + switch e := event.(type) { + case *instance.DomainAddedEvent: + wm.WriteModel.AppendEvents(e) + case *instance.DomainRemovedEvent: + wm.WriteModel.AppendEvents(e) + } + } +} + +func (wm *InstanceDomainsWriteModel) Reduce() error { + for _, event := range wm.Events { + switch e := event.(type) { + case *instance.DomainAddedEvent: + wm.Domains = append(wm.Domains, e.Domain) + case *instance.DomainRemovedEvent: + wm.Domains = removeDomainFromDomains(wm.Domains, e.Domain) + } + } + return wm.WriteModel.Reduce() +} + +func removeDomainFromDomains(items []string, domain string) []string { + for i := len(items) - 1; i >= 0; i-- { + if items[i] == domain { + items[i] = items[len(items)-1] + items[len(items)-1] = "" + items = items[:len(items)-1] + } + } + return items +} diff --git a/internal/domain/request.go b/internal/domain/request.go index 4dafcf0ca..21806a881 100644 --- a/internal/domain/request.go +++ b/internal/domain/request.go @@ -11,7 +11,7 @@ const ( SelectIDPScope = "urn:zitadel:iam:org:idp:id:" ) -//TODO: Change AuthRequest to interface and let oidcauthreqesut implement it +// TODO: Change AuthRequest to interface and let oidcauthreqesut implement it type Request interface { Type() AuthRequestType IsValid() bool diff --git a/internal/eventstore/handler/handler_projection.go b/internal/eventstore/handler/handler_projection.go index abb52604a..b8084a26b 100644 --- a/internal/eventstore/handler/handler_projection.go +++ b/internal/eventstore/handler/handler_projection.go @@ -23,20 +23,20 @@ type ProjectionHandlerConfig struct { ConcurrentInstances uint } -//Update updates the projection with the given statements +// Update updates the projection with the given statements type Update func(context.Context, []*Statement, Reduce) (index int, err error) -//Reduce reduces the given event to a statement -//which is used to update the projection +// Reduce reduces the given event to a statement +// which is used to update the projection type Reduce func(eventstore.Event) (*Statement, error) -//SearchQuery generates the search query to lookup for events +// SearchQuery generates the search query to lookup for events type SearchQuery func(ctx context.Context, instanceIDs []string) (query *eventstore.SearchQueryBuilder, queryLimit uint64, err error) -//Lock is used for mutex handling if needed on the projection +// Lock is used for mutex handling if needed on the projection type Lock func(context.Context, time.Duration, ...string) <-chan error -//Unlock releases the mutex of the projection +// Unlock releases the mutex of the projection type Unlock func(...string) error type ProjectionHandler struct { @@ -93,8 +93,8 @@ func NewProjectionHandler( return h } -//Trigger handles all events for the provided instances (or current instance from context if non specified) -//by calling FetchEvents and Process until the amount of events is smaller than the BulkLimit +// Trigger handles all events for the provided instances (or current instance from context if non specified) +// by calling FetchEvents and Process until the amount of events is smaller than the BulkLimit func (h *ProjectionHandler) Trigger(ctx context.Context, instances ...string) error { ids := []string{authz.GetInstance(ctx).InstanceID()} if len(instances) > 0 { @@ -118,7 +118,7 @@ func (h *ProjectionHandler) Trigger(ctx context.Context, instances ...string) er } } -//Process handles multiple events by reducing them to statements and updating the projection +// Process handles multiple events by reducing them to statements and updating the projection func (h *ProjectionHandler) Process(ctx context.Context, events ...eventstore.Event) (index int, err error) { if len(events) == 0 { return 0, nil @@ -144,7 +144,7 @@ func (h *ProjectionHandler) Process(ctx context.Context, events ...eventstore.Ev return index, err } -//FetchEvents checks the current sequences and filters for newer events +// FetchEvents checks the current sequences and filters for newer events func (h *ProjectionHandler) FetchEvents(ctx context.Context, instances ...string) ([]eventstore.Event, bool, error) { eventQuery, eventsLimit, err := h.searchQuery(ctx, instances) if err != nil { diff --git a/internal/query/current_sequence.go b/internal/query/current_sequence.go index 7b286d2ed..30c598ce3 100644 --- a/internal/query/current_sequence.go +++ b/internal/query/current_sequence.go @@ -73,7 +73,7 @@ func (q *Queries) latestSequence(ctx context.Context, projections ...table) (*La stmt, args, err := query. Where(or). Where(sq.Eq{CurrentSequenceColInstanceID.identifier(): authz.GetInstance(ctx).InstanceID()}). - OrderBy(CurrentSequenceColCurrentSequence.identifier()). + OrderBy(CurrentSequenceColCurrentSequence.identifier() + " DESC"). ToSql() if err != nil { return nil, errors.ThrowInternal(err, "QUERY-5CfX9", "Errors.Query.SQLStatement") diff --git a/internal/query/projection/action.go b/internal/query/projection/action.go index 4fcf2782e..05fd15b0e 100644 --- a/internal/query/projection/action.go +++ b/internal/query/projection/action.go @@ -9,6 +9,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" "github.com/zitadel/zitadel/internal/repository/action" + "github.com/zitadel/zitadel/internal/repository/instance" ) const ( @@ -83,6 +84,15 @@ func (p *actionProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(ActionInstanceIDCol), + }, + }, + }, } } @@ -178,7 +188,7 @@ func (p *actionProjection) reduceActionReactivated(event eventstore.Event) (*han func (p *actionProjection) reduceActionRemoved(event eventstore.Event) (*handler.Statement, error) { e, ok := event.(*action.RemovedEvent) if !ok { - return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Dgh2d", "reduce.wrong.event.type% s", action.RemovedEventType) + return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Dgh2d", "reduce.wrong.event.type %s", action.RemovedEventType) } return crdb.NewDeleteStatement( e, diff --git a/internal/query/projection/action_test.go b/internal/query/projection/action_test.go index 7ae8e018f..d3fabf497 100644 --- a/internal/query/projection/action_test.go +++ b/internal/query/projection/action_test.go @@ -10,6 +10,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" "github.com/zitadel/zitadel/internal/repository/action" + "github.com/zitadel/zitadel/internal/repository/instance" ) func TestActionProjection_reduces(t *testing.T) { @@ -33,7 +34,6 @@ func TestActionProjection_reduces(t *testing.T) { }, reduce: (&actionProjection{}).reduceActionAdded, want: wantReduce{ - projection: ActionTable, aggregateType: eventstore.AggregateType("action"), sequence: 15, previousSequence: 10, @@ -70,7 +70,6 @@ func TestActionProjection_reduces(t *testing.T) { }, reduce: (&actionProjection{}).reduceActionChanged, want: wantReduce{ - projection: ActionTable, aggregateType: eventstore.AggregateType("action"), sequence: 15, previousSequence: 10, @@ -101,7 +100,6 @@ func TestActionProjection_reduces(t *testing.T) { }, reduce: (&actionProjection{}).reduceActionDeactivated, want: wantReduce{ - projection: ActionTable, aggregateType: eventstore.AggregateType("action"), sequence: 15, previousSequence: 10, @@ -131,7 +129,6 @@ func TestActionProjection_reduces(t *testing.T) { }, reduce: (&actionProjection{}).reduceActionReactivated, want: wantReduce{ - projection: ActionTable, aggregateType: eventstore.AggregateType("action"), sequence: 15, previousSequence: 10, @@ -161,7 +158,6 @@ func TestActionProjection_reduces(t *testing.T) { }, reduce: (&actionProjection{}).reduceActionRemoved, want: wantReduce{ - projection: ActionTable, aggregateType: eventstore.AggregateType("action"), sequence: 15, previousSequence: 10, @@ -177,6 +173,32 @@ func TestActionProjection_reduces(t *testing.T) { }, }, }, + { + name: "reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(ActionInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.actions2 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -188,7 +210,7 @@ func TestActionProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, ActionTable, tt.want) }) } } diff --git a/internal/query/projection/app.go b/internal/query/projection/app.go index 7ac25b33f..7d6e3bebc 100644 --- a/internal/query/projection/app.go +++ b/internal/query/projection/app.go @@ -9,6 +9,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/project" ) @@ -204,6 +205,15 @@ func (p *appProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(AppColumnInstanceID), + }, + }, + }, } } diff --git a/internal/query/projection/app_test.go b/internal/query/projection/app_test.go index 4ac9e1d3b..d4671518f 100644 --- a/internal/query/projection/app_test.go +++ b/internal/query/projection/app_test.go @@ -10,6 +10,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/project" ) @@ -40,7 +41,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -78,7 +78,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -111,7 +110,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -144,7 +142,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -177,7 +174,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -205,7 +201,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -219,6 +214,32 @@ func TestAppProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(AppColumnInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.apps3 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "project.reduceAPIConfigAdded", args: args{ @@ -238,7 +259,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -283,7 +303,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -324,7 +343,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{}, }, @@ -347,7 +365,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -403,7 +420,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -472,7 +488,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -525,7 +540,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{}, }, @@ -548,7 +562,6 @@ func TestAppProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, - projection: AppProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -583,7 +596,7 @@ func TestAppProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, AppProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/authn_key.go b/internal/query/projection/authn_key.go index 9217e64cb..4930ba6e7 100644 --- a/internal/query/projection/authn_key.go +++ b/internal/query/projection/authn_key.go @@ -9,6 +9,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/project" "github.com/zitadel/zitadel/internal/repository/user" ) @@ -109,6 +110,15 @@ func (p *authNKeyProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(AuthNKeyInstanceIDCol), + }, + }, + }, } } diff --git a/internal/query/projection/authn_key_test.go b/internal/query/projection/authn_key_test.go index 49b43d034..de7d8f91b 100644 --- a/internal/query/projection/authn_key_test.go +++ b/internal/query/projection/authn_key_test.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/project" "github.com/zitadel/zitadel/internal/repository/user" ) @@ -33,7 +34,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyAdded, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -70,7 +70,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyAdded, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("user"), sequence: 15, previousSequence: 10, @@ -107,7 +106,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -134,7 +132,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyEnabledChanged, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -154,7 +151,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyEnabledChanged, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -182,7 +178,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyEnabledChanged, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -210,7 +205,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("user"), sequence: 15, previousSequence: 10, @@ -226,6 +220,32 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, }, }, + { + name: "reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(AuthNKeyInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.authn_keys WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "reduceAuthNKeyEnabledChanged oidc no change", args: args{ @@ -237,7 +257,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyEnabledChanged, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -257,7 +276,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyEnabledChanged, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -285,7 +303,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyEnabledChanged, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -313,7 +330,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -340,7 +356,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -367,7 +382,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -394,7 +408,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("user"), sequence: 15, previousSequence: 10, @@ -421,7 +434,6 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { }, reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved, want: wantReduce{ - projection: AuthNKeyTable, aggregateType: eventstore.AggregateType("user"), sequence: 15, previousSequence: 10, @@ -448,7 +460,7 @@ func TestAuthNKeyProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, AuthNKeyTable, tt.want) }) } } diff --git a/internal/query/projection/custom_text.go b/internal/query/projection/custom_text.go index d22f3e54e..38200704d 100644 --- a/internal/query/projection/custom_text.go +++ b/internal/query/projection/custom_text.go @@ -89,6 +89,10 @@ func (p *customTextProjection) reducers() []handler.AggregateReducer { Event: instance.CustomTextTemplateRemovedEventType, Reduce: p.reduceTemplateRemoved, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(CustomTextInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/custom_text_test.go b/internal/query/projection/custom_text_test.go index c770f3ea0..92ad372ed 100644 --- a/internal/query/projection/custom_text_test.go +++ b/internal/query/projection/custom_text_test.go @@ -40,7 +40,6 @@ func TestCustomTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: CustomTextTable, executer: &testExecuter{ executions: []execution{ { @@ -80,7 +79,6 @@ func TestCustomTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: CustomTextTable, executer: &testExecuter{ executions: []execution{ { @@ -114,7 +112,6 @@ func TestCustomTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: CustomTextTable, executer: &testExecuter{ executions: []execution{ { @@ -129,6 +126,32 @@ func TestCustomTextProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(CustomTextInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.custom_texts WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "iam.reduceAdded", reduce: (&customTextProjection{}).reduceSet, @@ -148,7 +171,6 @@ func TestCustomTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: CustomTextTable, executer: &testExecuter{ executions: []execution{ { @@ -188,7 +210,6 @@ func TestCustomTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: CustomTextTable, executer: &testExecuter{ executions: []execution{ { @@ -222,7 +243,6 @@ func TestCustomTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: CustomTextTable, executer: &testExecuter{ executions: []execution{ { @@ -248,7 +268,7 @@ func TestCustomTextProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, CustomTextTable, tt.want) }) } } diff --git a/internal/query/projection/debug_notification.go b/internal/query/projection/debug_notification.go index 98aa59b4d..0ee1e9fa1 100644 --- a/internal/query/projection/debug_notification.go +++ b/internal/query/projection/debug_notification.go @@ -83,6 +83,10 @@ func (p *debugNotificationProviderProjection) reducers() []handler.AggregateRedu Event: instance.DebugNotificationProviderLogRemovedEventType, Reduce: p.reduceDebugNotificationProviderRemoved, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(DebugNotificationProviderInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/debug_notification_provider_test.go b/internal/query/projection/debug_notification_provider_test.go index 8a4f7ea78..8a4a457fd 100644 --- a/internal/query/projection/debug_notification_provider_test.go +++ b/internal/query/projection/debug_notification_provider_test.go @@ -37,7 +37,6 @@ func TestDebugNotificationProviderProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: DebugNotificationProviderTable, executer: &testExecuter{ executions: []execution{ { @@ -74,7 +73,6 @@ func TestDebugNotificationProviderProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: DebugNotificationProviderTable, executer: &testExecuter{ executions: []execution{ { @@ -105,7 +103,6 @@ func TestDebugNotificationProviderProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: DebugNotificationProviderTable, executer: &testExecuter{ executions: []execution{ { @@ -135,7 +132,6 @@ func TestDebugNotificationProviderProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: DebugNotificationProviderTable, executer: &testExecuter{ executions: []execution{ { @@ -172,7 +168,6 @@ func TestDebugNotificationProviderProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: DebugNotificationProviderTable, executer: &testExecuter{ executions: []execution{ { @@ -203,7 +198,6 @@ func TestDebugNotificationProviderProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: DebugNotificationProviderTable, executer: &testExecuter{ executions: []execution{ { @@ -217,6 +211,32 @@ func TestDebugNotificationProviderProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(DebugNotificationProviderInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.notification_providers WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -228,7 +248,7 @@ func TestDebugNotificationProviderProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, DebugNotificationProviderTable, tt.want) }) } } diff --git a/internal/query/projection/domain_policy.go b/internal/query/projection/domain_policy.go index 5fb6a1dfa..fd88e7311 100644 --- a/internal/query/projection/domain_policy.go +++ b/internal/query/projection/domain_policy.go @@ -88,6 +88,10 @@ func (p *domainPolicyProjection) reducers() []handler.AggregateReducer { Event: instance.DomainPolicyChangedEventType, Reduce: p.reduceChanged, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(DomainPolicyInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/domain_policy_test.go b/internal/query/projection/domain_policy_test.go index 88554b58e..927c57c6a 100644 --- a/internal/query/projection/domain_policy_test.go +++ b/internal/query/projection/domain_policy_test.go @@ -40,7 +40,6 @@ func TestDomainPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: DomainPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -81,7 +80,6 @@ func TestDomainPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: DomainPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -113,7 +111,6 @@ func TestDomainPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: DomainPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -126,6 +123,32 @@ func TestDomainPolicyProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(DomainPolicyInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.domain_policies WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "instance.reduceAdded", reduce: (&domainPolicyProjection{}).reduceAdded, @@ -144,7 +167,6 @@ func TestDomainPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: DomainPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -185,7 +207,6 @@ func TestDomainPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: DomainPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -214,7 +235,7 @@ func TestDomainPolicyProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, DomainPolicyTable, tt.want) }) } } diff --git a/internal/query/projection/event_test.go b/internal/query/projection/event_test.go index 5ec2cb02b..29e32e95c 100644 --- a/internal/query/projection/event_test.go +++ b/internal/query/projection/event_test.go @@ -48,7 +48,6 @@ func getEvent(event *repository.Event, mapper func(*repository.Event) (eventstor } type wantReduce struct { - projection string aggregateType eventstore.AggregateType sequence uint64 previousSequence uint64 @@ -56,7 +55,7 @@ type wantReduce struct { err func(error) bool } -func assertReduce(t *testing.T, stmt *handler.Statement, err error, want wantReduce) { +func assertReduce(t *testing.T, stmt *handler.Statement, err error, projection string, want wantReduce) { t.Helper() if want.err == nil && err != nil { t.Errorf("unexpected error of type %T: %v", err, err) @@ -80,7 +79,7 @@ func assertReduce(t *testing.T, stmt *handler.Statement, err error, want wantRed want.executer.Validate(t) return } - err = stmt.Execute(want.executer, want.projection) + err = stmt.Execute(want.executer, projection) if err != nil { t.Errorf("unexpected error: %v", err) } diff --git a/internal/query/projection/flow.go b/internal/query/projection/flow.go index 512e78bc2..78f61999a 100644 --- a/internal/query/projection/flow.go +++ b/internal/query/projection/flow.go @@ -7,6 +7,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/org" ) @@ -63,6 +64,15 @@ func (p *flowProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(FlowInstanceIDCol), + }, + }, + }, } } diff --git a/internal/query/projection/flow_test.go b/internal/query/projection/flow_test.go index 97af3ec3f..2716cfb01 100644 --- a/internal/query/projection/flow_test.go +++ b/internal/query/projection/flow_test.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/org" ) @@ -32,7 +33,6 @@ func TestFlowProjection_reduces(t *testing.T) { }, reduce: (&flowProjection{}).reduceTriggerActionsSetEventType, want: wantReduce{ - projection: FlowTriggerTable, aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, @@ -87,7 +87,6 @@ func TestFlowProjection_reduces(t *testing.T) { }, reduce: (&flowProjection{}).reduceFlowClearedEventType, want: wantReduce{ - projection: FlowTriggerTable, aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, @@ -104,6 +103,32 @@ func TestFlowProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(FlowInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.flows_triggers WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -115,7 +140,7 @@ func TestFlowProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, FlowTriggerTable, tt.want) }) } } diff --git a/internal/query/projection/idp.go b/internal/query/projection/idp.go index d1e687d0c..2faddc942 100644 --- a/internal/query/projection/idp.go +++ b/internal/query/projection/idp.go @@ -155,6 +155,10 @@ func (p *idpProjection) reducers() []handler.AggregateReducer { Event: instance.IDPJWTConfigChangedEventType, Reduce: p.reduceJWTConfigChanged, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(IDPInstanceIDCol), + }, }, }, { diff --git a/internal/query/projection/idp_login_policy_link.go b/internal/query/projection/idp_login_policy_link.go index 183942d5f..213fb72c8 100644 --- a/internal/query/projection/idp_login_policy_link.go +++ b/internal/query/projection/idp_login_policy_link.go @@ -103,6 +103,10 @@ func (p *idpLoginPolicyLinkProjection) reducers() []handler.AggregateReducer { Event: instance.IDPConfigRemovedEventType, Reduce: p.reduceIDPConfigRemoved, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(IDPUserLinkInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/idp_login_policy_link_test.go b/internal/query/projection/idp_login_policy_link_test.go index 197d35c10..1a4c7043f 100644 --- a/internal/query/projection/idp_login_policy_link_test.go +++ b/internal/query/projection/idp_login_policy_link_test.go @@ -39,7 +39,6 @@ func TestIDPLoginPolicyLinkProjection_reduces(t *testing.T) { aggregateType: instance.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPLoginPolicyLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -76,7 +75,6 @@ func TestIDPLoginPolicyLinkProjection_reduces(t *testing.T) { aggregateType: instance.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPLoginPolicyLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -107,7 +105,6 @@ func TestIDPLoginPolicyLinkProjection_reduces(t *testing.T) { aggregateType: instance.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPLoginPolicyLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -138,7 +135,6 @@ func TestIDPLoginPolicyLinkProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPLoginPolicyLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -175,7 +171,6 @@ func TestIDPLoginPolicyLinkProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPLoginPolicyLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -189,6 +184,32 @@ func TestIDPLoginPolicyLinkProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(IDPUserLinkInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.idp_login_policy_links3 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "org.reduceCascadeRemoved", args: args{ @@ -206,7 +227,6 @@ func TestIDPLoginPolicyLinkProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPLoginPolicyLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -234,7 +254,6 @@ func TestIDPLoginPolicyLinkProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPLoginPolicyLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -261,7 +280,6 @@ func TestIDPLoginPolicyLinkProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPLoginPolicyLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -290,7 +308,6 @@ func TestIDPLoginPolicyLinkProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPLoginPolicyLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -320,7 +337,6 @@ func TestIDPLoginPolicyLinkProjection_reduces(t *testing.T) { aggregateType: instance.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPLoginPolicyLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -345,7 +361,7 @@ func TestIDPLoginPolicyLinkProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, IDPLoginPolicyLinkTable, tt.want) }) } } diff --git a/internal/query/projection/idp_test.go b/internal/query/projection/idp_test.go index e1266f80a..6fe3a3abe 100644 --- a/internal/query/projection/idp_test.go +++ b/internal/query/projection/idp_test.go @@ -43,7 +43,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -85,7 +84,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -120,7 +118,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -153,7 +150,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -186,7 +182,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -200,6 +195,32 @@ func TestIDPProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(IDPInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.idps2 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "instance.reduceOIDCConfigAdded", args: args{ @@ -228,7 +249,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -288,7 +308,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -333,7 +352,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{}, }, @@ -359,7 +377,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -407,7 +424,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -448,7 +464,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{}, }, @@ -474,7 +489,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -516,7 +530,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -551,7 +564,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -584,7 +596,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -617,7 +628,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -659,7 +669,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -719,7 +728,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -764,7 +772,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{}, }, @@ -790,7 +797,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -838,7 +844,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{ { @@ -879,7 +884,6 @@ func TestIDPProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: IDPTable, executer: &testExecuter{ executions: []execution{}, }, @@ -896,7 +900,7 @@ func TestIDPProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, IDPTable, tt.want) }) } } diff --git a/internal/query/projection/idp_user_link.go b/internal/query/projection/idp_user_link.go index cf71362d3..d045f24c6 100644 --- a/internal/query/projection/idp_user_link.go +++ b/internal/query/projection/idp_user_link.go @@ -96,6 +96,10 @@ func (p *idpUserLinkProjection) reducers() []handler.AggregateReducer { Event: instance.IDPConfigRemovedEventType, Reduce: p.reduceIDPConfigRemoved, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(IDPUserLinkInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/idp_user_link_test.go b/internal/query/projection/idp_user_link_test.go index 59c74902b..e30953fc8 100644 --- a/internal/query/projection/idp_user_link_test.go +++ b/internal/query/projection/idp_user_link_test.go @@ -40,7 +40,6 @@ func TestIDPUserLinkProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPUserLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -78,7 +77,6 @@ func TestIDPUserLinkProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPUserLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -110,7 +108,6 @@ func TestIDPUserLinkProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPUserLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -139,7 +136,6 @@ func TestIDPUserLinkProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPUserLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -152,6 +148,32 @@ func TestIDPUserLinkProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(IDPUserLinkInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.idp_user_links2 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "reduceUserRemoved", args: args{ @@ -166,7 +188,6 @@ func TestIDPUserLinkProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPUserLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -195,7 +216,6 @@ func TestIDPUserLinkProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPUserLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -225,7 +245,6 @@ func TestIDPUserLinkProjection_reduces(t *testing.T) { aggregateType: instance.AggregateType, sequence: 15, previousSequence: 10, - projection: IDPUserLinkTable, executer: &testExecuter{ executions: []execution{ { @@ -250,7 +269,7 @@ func TestIDPUserLinkProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, IDPUserLinkTable, tt.want) }) } } diff --git a/internal/query/projection/instance.go b/internal/query/projection/instance.go index 4212513ab..43c0d84e1 100644 --- a/internal/query/projection/instance.go +++ b/internal/query/projection/instance.go @@ -66,6 +66,10 @@ func (p *instanceProjection) reducers() []handler.AggregateReducer { Event: instance.InstanceChangedEventType, Reduce: p.reduceInstanceChanged, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(InstanceColumnID), + }, { Event: instance.DefaultOrgSetEventType, Reduce: p.reduceDefaultOrgSet, @@ -104,6 +108,21 @@ func (p *instanceProjection) reduceInstanceAdded(event eventstore.Event) (*handl ), nil } +func reduceInstanceRemovedHelper(instanceIDCol string) func(event eventstore.Event) (*handler.Statement, error) { + return func(event eventstore.Event) (*handler.Statement, error) { + e, ok := event.(*instance.InstanceRemovedEvent) + if !ok { + return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-22nlS", "reduce.wrong.event.type %s", instance.InstanceRemovedEventType) + } + return crdb.NewDeleteStatement( + e, + []handler.Condition{ + handler.NewCond(instanceIDCol, e.Aggregate().ID), + }, + ), nil + } +} + func (p *instanceProjection) reduceInstanceChanged(event eventstore.Event) (*handler.Statement, error) { e, ok := event.(*instance.InstanceChangedEvent) if !ok { diff --git a/internal/query/projection/instance_domain.go b/internal/query/projection/instance_domain.go index 4e3034d3e..1511abd7d 100644 --- a/internal/query/projection/instance_domain.go +++ b/internal/query/projection/instance_domain.go @@ -64,6 +64,10 @@ func (p *instanceDomainProjection) reducers() []handler.AggregateReducer { Event: instance.InstanceDomainRemovedEventType, Reduce: p.reduceDomainRemoved, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(InstanceDomainInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/instance_domain_test.go b/internal/query/projection/instance_domain_test.go index 8410472b8..adc6a272d 100644 --- a/internal/query/projection/instance_domain_test.go +++ b/internal/query/projection/instance_domain_test.go @@ -31,7 +31,6 @@ func TestInstanceDomainProjection_reduces(t *testing.T) { }, reduce: (&instanceDomainProjection{}).reduceDomainAdded, want: wantReduce{ - projection: InstanceDomainTable, aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, @@ -64,7 +63,6 @@ func TestInstanceDomainProjection_reduces(t *testing.T) { }, reduce: (&instanceDomainProjection{}).reduceDomainRemoved, want: wantReduce{ - projection: InstanceDomainTable, aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, @@ -81,6 +79,32 @@ func TestInstanceDomainProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(InstanceDomainInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.instance_domains WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -92,7 +116,7 @@ func TestInstanceDomainProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, InstanceDomainTable, tt.want) }) } } diff --git a/internal/query/projection/instance_member.go b/internal/query/projection/instance_member.go index 6a908d340..d665768a4 100644 --- a/internal/query/projection/instance_member.go +++ b/internal/query/projection/instance_member.go @@ -58,6 +58,10 @@ func (p *instanceMemberProjection) reducers() []handler.AggregateReducer { Event: instance.MemberRemovedEventType, Reduce: p.reduceRemoved, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(AppColumnInstanceID), + }, }, }, { diff --git a/internal/query/projection/instance_member_test.go b/internal/query/projection/instance_member_test.go index b835ac6f7..63353c0d0 100644 --- a/internal/query/projection/instance_member_test.go +++ b/internal/query/projection/instance_member_test.go @@ -39,7 +39,6 @@ func TestInstanceMemberProjection_reduces(t *testing.T) { aggregateType: instance.AggregateType, sequence: 15, previousSequence: 10, - projection: InstanceMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -76,7 +75,6 @@ func TestInstanceMemberProjection_reduces(t *testing.T) { aggregateType: instance.AggregateType, sequence: 15, previousSequence: 10, - projection: InstanceMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -108,7 +106,6 @@ func TestInstanceMemberProjection_reduces(t *testing.T) { aggregateType: instance.AggregateType, sequence: 15, previousSequence: 10, - projection: InstanceMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -137,7 +134,6 @@ func TestInstanceMemberProjection_reduces(t *testing.T) { aggregateType: instance.AggregateType, sequence: 15, previousSequence: 10, - projection: InstanceMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -164,7 +160,6 @@ func TestInstanceMemberProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: InstanceMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -177,6 +172,32 @@ func TestInstanceMemberProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(MemberInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.instance_members2 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -188,7 +209,7 @@ func TestInstanceMemberProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, InstanceMemberProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/instance_test.go b/internal/query/projection/instance_test.go index b7b7683a0..251316885 100644 --- a/internal/query/projection/instance_test.go +++ b/internal/query/projection/instance_test.go @@ -19,37 +19,63 @@ func TestInstanceProjection_reduces(t *testing.T) { args args reduce func(event eventstore.Event) (*handler.Statement, error) want wantReduce - }{{ - name: "reduceInstanceAdded", - args: args{ - event: getEvent(testEvent( - repository.EventType(instance.InstanceAddedEventType), - instance.AggregateType, - []byte(`{"name": "Name"}`), - ), instance.InstanceAddedEventMapper), + }{ + { + name: "reduceInstanceAdded", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceAddedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceAddedEventMapper), + }, + reduce: (&instanceProjection{}).reduceInstanceAdded, + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "INSERT INTO projections.instances (id, creation_date, change_date, sequence, name) VALUES ($1, $2, $3, $4, $5)", + expectedArgs: []interface{}{ + "instance-id", + anyArg{}, + anyArg{}, + uint64(15), + "Name", + }, + }, + }, + }, + }, }, - reduce: (&instanceProjection{}).reduceInstanceAdded, - want: wantReduce{ - projection: InstanceProjectionTable, - aggregateType: eventstore.AggregateType("instance"), - sequence: 15, - previousSequence: 10, - executer: &testExecuter{ - executions: []execution{ - { - expectedStmt: "INSERT INTO projections.instances (id, creation_date, change_date, sequence, name) VALUES ($1, $2, $3, $4, $5)", - expectedArgs: []interface{}{ - "instance-id", - anyArg{}, - anyArg{}, - uint64(15), - "Name", + { + name: "reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(InstanceColumnID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.instances WHERE (id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, }, }, }, }, }, - }, { name: "reduceDefaultOrgSet", args: args{ @@ -61,7 +87,6 @@ func TestInstanceProjection_reduces(t *testing.T) { }, reduce: (&instanceProjection{}).reduceDefaultOrgSet, want: wantReduce{ - projection: InstanceProjectionTable, aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, @@ -91,7 +116,6 @@ func TestInstanceProjection_reduces(t *testing.T) { }, reduce: (&instanceProjection{}).reduceIAMProjectSet, want: wantReduce{ - projection: InstanceProjectionTable, aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, @@ -121,7 +145,6 @@ func TestInstanceProjection_reduces(t *testing.T) { }, reduce: (&instanceProjection{}).reduceDefaultLanguageSet, want: wantReduce{ - projection: InstanceProjectionTable, aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, @@ -151,7 +174,7 @@ func TestInstanceProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, InstanceProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/key.go b/internal/query/projection/key.go index 71b4287aa..d2b2fab30 100644 --- a/internal/query/projection/key.go +++ b/internal/query/projection/key.go @@ -9,6 +9,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/keypair" ) @@ -123,6 +124,15 @@ func (p *keyProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(KeyColumnInstanceID), + }, + }, + }, } } diff --git a/internal/query/projection/key_test.go b/internal/query/projection/key_test.go index a236cfa63..5aeb3c53c 100644 --- a/internal/query/projection/key_test.go +++ b/internal/query/projection/key_test.go @@ -13,6 +13,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/keypair" ) @@ -37,7 +38,6 @@ func TestKeyProjection_reduces(t *testing.T) { }, reduce: (&keyProjection{encryptionAlgorithm: crypto.CreateMockEncryptionAlg(gomock.NewController(t))}).reduceKeyPairAdded, want: wantReduce{ - projection: KeyProjectionTable, aggregateType: eventstore.AggregateType("key_pair"), sequence: 15, previousSequence: 10, @@ -94,13 +94,38 @@ func TestKeyProjection_reduces(t *testing.T) { }, reduce: (&keyProjection{}).reduceKeyPairAdded, want: wantReduce{ - projection: KeyProjectionTable, aggregateType: eventstore.AggregateType("key_pair"), sequence: 15, previousSequence: 10, executer: &testExecuter{}, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(KeyColumnInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.keys3 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "reduceCertificateAdded", args: args{ @@ -112,7 +137,6 @@ func TestKeyProjection_reduces(t *testing.T) { }, reduce: (&keyProjection{certEncryptionAlgorithm: crypto.CreateMockEncryptionAlg(gomock.NewController(t))}).reduceCertificateAdded, want: wantReduce{ - projection: KeyProjectionTable, aggregateType: eventstore.AggregateType("key_pair"), sequence: 15, previousSequence: 10, @@ -142,7 +166,7 @@ func TestKeyProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, KeyProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/label_policy.go b/internal/query/projection/label_policy.go index 0c443e954..08d4e2ea4 100644 --- a/internal/query/projection/label_policy.go +++ b/internal/query/projection/label_policy.go @@ -212,6 +212,10 @@ func (p *labelPolicyProjection) reducers() []handler.AggregateReducer { Event: instance.LabelPolicyAssetsRemovedEventType, Reduce: p.reduceAssetsRemoved, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(LabelPolicyInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/label_policy_test.go b/internal/query/projection/label_policy_test.go index 68cfbf471..a5a1743aa 100644 --- a/internal/query/projection/label_policy_test.go +++ b/internal/query/projection/label_policy_test.go @@ -36,7 +36,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -81,7 +80,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -115,7 +113,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -128,6 +125,32 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(LabelPolicyInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.label_policies WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "org.reduceActivated", args: args{ @@ -142,7 +165,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -174,7 +196,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -205,7 +226,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -236,7 +256,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -267,7 +286,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -298,7 +316,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -329,7 +346,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -360,7 +376,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -391,7 +406,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -422,7 +436,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -453,7 +466,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -484,7 +496,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -519,7 +530,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -564,7 +574,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -605,7 +614,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -637,7 +645,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -668,7 +675,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -699,7 +705,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -730,7 +735,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -761,7 +765,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -792,7 +795,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -823,7 +825,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -854,7 +855,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -885,7 +885,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -916,7 +915,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -947,7 +945,6 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LabelPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -979,7 +976,7 @@ func TestLabelPolicyProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, LabelPolicyTable, tt.want) }) } } diff --git a/internal/query/projection/lockout_policy.go b/internal/query/projection/lockout_policy.go index cfa9a253d..5130b4df9 100644 --- a/internal/query/projection/lockout_policy.go +++ b/internal/query/projection/lockout_policy.go @@ -86,6 +86,10 @@ func (p *lockoutPolicyProjection) reducers() []handler.AggregateReducer { Event: instance.LockoutPolicyChangedEventType, Reduce: p.reduceChanged, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(LockoutPolicyInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/lockout_policy_test.go b/internal/query/projection/lockout_policy_test.go index 7089c8bf2..1b07e5c4b 100644 --- a/internal/query/projection/lockout_policy_test.go +++ b/internal/query/projection/lockout_policy_test.go @@ -39,7 +39,6 @@ func TestLockoutPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LockoutPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -78,7 +77,6 @@ func TestLockoutPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LockoutPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -109,7 +107,6 @@ func TestLockoutPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LockoutPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -122,6 +119,32 @@ func TestLockoutPolicyProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(LockoutPolicyInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.lockout_policies WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "instance.reduceAdded", reduce: (&lockoutPolicyProjection{}).reduceAdded, @@ -139,7 +162,6 @@ func TestLockoutPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LockoutPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -178,7 +200,6 @@ func TestLockoutPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LockoutPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -206,7 +227,7 @@ func TestLockoutPolicyProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, LockoutPolicyTable, tt.want) }) } } diff --git a/internal/query/projection/login_name.go b/internal/query/projection/login_name.go index 772e23d58..cb7b29128 100644 --- a/internal/query/projection/login_name.go +++ b/internal/query/projection/login_name.go @@ -211,6 +211,10 @@ func (p *loginNameProjection) reducers() []handler.AggregateReducer { Event: instance.DomainPolicyChangedEventType, Reduce: p.reduceDomainPolicyChanged, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(LoginNameUserInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/login_name_test.go b/internal/query/projection/login_name_test.go index 2cc97006e..36602192e 100644 --- a/internal/query/projection/login_name_test.go +++ b/internal/query/projection/login_name_test.go @@ -38,7 +38,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -70,7 +69,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -102,7 +100,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -132,7 +129,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -161,7 +157,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -191,7 +186,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -221,7 +215,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -253,7 +246,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -281,7 +273,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{}, }, @@ -301,7 +292,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -330,7 +320,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -361,7 +350,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -391,7 +379,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -430,7 +417,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -462,7 +448,6 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -490,12 +475,37 @@ func TestLoginNameProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: LoginNameProjectionTable, executer: &testExecuter{ executions: []execution{}, }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(LoginNameUserInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.login_names WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -507,7 +517,7 @@ func TestLoginNameProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, LoginNameProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/login_policy.go b/internal/query/projection/login_policy.go index 9ba12ec80..cf992d2c8 100644 --- a/internal/query/projection/login_policy.go +++ b/internal/query/projection/login_policy.go @@ -145,6 +145,10 @@ func (p *loginPolicyProjection) reducers() []handler.AggregateReducer { Event: instance.LoginPolicySecondFactorRemovedEventType, Reduce: p.reduce2FARemoved, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(LoginPolicyInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/login_policy_test.go b/internal/query/projection/login_policy_test.go index ef5f5a33f..35ebc9377 100644 --- a/internal/query/projection/login_policy_test.go +++ b/internal/query/projection/login_policy_test.go @@ -54,7 +54,6 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LoginPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -119,7 +118,6 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LoginPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -166,7 +164,6 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LoginPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -198,7 +195,6 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LoginPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -228,7 +224,6 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LoginPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -257,7 +252,6 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LoginPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -289,7 +283,6 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: LoginPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -336,7 +329,6 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LoginPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -396,7 +388,6 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LoginPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -438,7 +429,6 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LoginPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -470,7 +460,6 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LoginPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -502,7 +491,6 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LoginPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -534,7 +522,6 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: LoginPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -550,6 +537,32 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(LoginPolicyInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.login_policies3 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -561,7 +574,7 @@ func TestLoginPolicyProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, LoginPolicyTable, tt.want) }) } } diff --git a/internal/query/projection/mail_template.go b/internal/query/projection/mail_template.go index 73619d106..d7bb7e553 100644 --- a/internal/query/projection/mail_template.go +++ b/internal/query/projection/mail_template.go @@ -84,6 +84,15 @@ func (p *mailTemplateProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(MailTemplateInstanceIDCol), + }, + }, + }, } } diff --git a/internal/query/projection/mail_template_test.go b/internal/query/projection/mail_template_test.go index 872e260b1..86188c8cc 100644 --- a/internal/query/projection/mail_template_test.go +++ b/internal/query/projection/mail_template_test.go @@ -38,7 +38,6 @@ func TestMailTemplateProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MailTemplateTable, executer: &testExecuter{ executions: []execution{ { @@ -74,7 +73,6 @@ func TestMailTemplateProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MailTemplateTable, executer: &testExecuter{ executions: []execution{ { @@ -104,7 +102,6 @@ func TestMailTemplateProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MailTemplateTable, executer: &testExecuter{ executions: []execution{ { @@ -117,6 +114,32 @@ func TestMailTemplateProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(MailTemplateInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.mail_templates WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "instance.reduceAdded", reduce: (&mailTemplateProjection{}).reduceAdded, @@ -133,7 +156,6 @@ func TestMailTemplateProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: MailTemplateTable, executer: &testExecuter{ executions: []execution{ { @@ -169,7 +191,6 @@ func TestMailTemplateProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: MailTemplateTable, executer: &testExecuter{ executions: []execution{ { @@ -196,7 +217,7 @@ func TestMailTemplateProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, MailTemplateTable, tt.want) }) } } diff --git a/internal/query/projection/message_text_test.go b/internal/query/projection/message_text_test.go index f1f44842f..1eb7fe549 100644 --- a/internal/query/projection/message_text_test.go +++ b/internal/query/projection/message_text_test.go @@ -41,7 +41,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -81,7 +80,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -121,7 +119,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -161,7 +158,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -201,7 +197,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -241,7 +236,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -281,7 +275,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -320,7 +313,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -338,6 +330,32 @@ func TestMessageTextProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(MessageTextInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.message_texts WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "org.reduceRemoved.PreHeader", args: args{ @@ -356,7 +374,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -392,7 +409,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -428,7 +444,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -464,7 +479,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -500,7 +514,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -536,7 +549,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -572,7 +584,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -606,7 +617,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -645,7 +655,6 @@ func TestMessageTextProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: MessageTextTable, executer: &testExecuter{ executions: []execution{ { @@ -674,7 +683,7 @@ func TestMessageTextProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, MessageTextTable, tt.want) }) } } diff --git a/internal/query/projection/message_texts.go b/internal/query/projection/message_texts.go index dc0ee89e6..37bd48597 100644 --- a/internal/query/projection/message_texts.go +++ b/internal/query/projection/message_texts.go @@ -100,6 +100,10 @@ func (p *messageTextProjection) reducers() []handler.AggregateReducer { Event: instance.CustomTextTemplateRemovedEventType, Reduce: p.reduceTemplateRemoved, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(MessageTextInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/oidc_settings.go b/internal/query/projection/oidc_settings.go index ad9ea7929..c603d254d 100644 --- a/internal/query/projection/oidc_settings.go +++ b/internal/query/projection/oidc_settings.go @@ -68,6 +68,15 @@ func (p *oidcSettingsProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(OIDCSettingsColumnInstanceID), + }, + }, + }, } } diff --git a/internal/query/projection/oidc_settings_test.go b/internal/query/projection/oidc_settings_test.go index 55e482572..832d06fd6 100644 --- a/internal/query/projection/oidc_settings_test.go +++ b/internal/query/projection/oidc_settings_test.go @@ -32,7 +32,6 @@ func TestOIDCSettingsProjection_reduces(t *testing.T) { }, reduce: (&oidcSettingsProjection{}).reduceOIDCSettingsChanged, want: wantReduce{ - projection: OIDCSettingsProjectionTable, aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, @@ -65,7 +64,6 @@ func TestOIDCSettingsProjection_reduces(t *testing.T) { }, reduce: (&oidcSettingsProjection{}).reduceOIDCSettingsAdded, want: wantReduce{ - projection: OIDCSettingsProjectionTable, aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, @@ -90,6 +88,32 @@ func TestOIDCSettingsProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(OIDCSettingsColumnInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.oidc_settings WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -101,7 +125,7 @@ func TestOIDCSettingsProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, OIDCSettingsProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/org.go b/internal/query/projection/org.go index 419d8b46f..b7693f7ec 100644 --- a/internal/query/projection/org.go +++ b/internal/query/projection/org.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/org" ) @@ -81,6 +82,15 @@ func (p *orgProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(OrgColumnInstanceID), + }, + }, + }, } } diff --git a/internal/query/projection/org_domain.go b/internal/query/projection/org_domain.go index 2d886d50e..6ac9af170 100644 --- a/internal/query/projection/org_domain.go +++ b/internal/query/projection/org_domain.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/org" ) @@ -79,6 +80,15 @@ func (p *orgDomainProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(OrgDomainInstanceIDCol), + }, + }, + }, } } diff --git a/internal/query/projection/org_domain_test.go b/internal/query/projection/org_domain_test.go index 169bb3879..4395d2abe 100644 --- a/internal/query/projection/org_domain_test.go +++ b/internal/query/projection/org_domain_test.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/org" ) @@ -32,7 +33,6 @@ func TestOrgDomainProjection_reduces(t *testing.T) { }, reduce: (&orgDomainProjection{}).reduceDomainAdded, want: wantReduce{ - projection: OrgDomainTable, aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, @@ -67,7 +67,6 @@ func TestOrgDomainProjection_reduces(t *testing.T) { }, reduce: (&orgDomainProjection{}).reduceDomainVerificationAdded, want: wantReduce{ - projection: OrgDomainTable, aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, @@ -99,7 +98,6 @@ func TestOrgDomainProjection_reduces(t *testing.T) { }, reduce: (&orgDomainProjection{}).reduceDomainVerified, want: wantReduce{ - projection: OrgDomainTable, aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, @@ -131,7 +129,6 @@ func TestOrgDomainProjection_reduces(t *testing.T) { }, reduce: (&orgDomainProjection{}).reducePrimaryDomainSet, want: wantReduce{ - projection: OrgDomainTable, aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, @@ -174,7 +171,6 @@ func TestOrgDomainProjection_reduces(t *testing.T) { }, reduce: (&orgDomainProjection{}).reduceDomainRemoved, want: wantReduce{ - projection: OrgDomainTable, aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, @@ -192,6 +188,32 @@ func TestOrgDomainProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(OrgDomainInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.org_domains WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -203,7 +225,7 @@ func TestOrgDomainProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, OrgDomainTable, tt.want) }) } } diff --git a/internal/query/projection/org_member.go b/internal/query/projection/org_member.go index c5d3abc45..e5daa07a4 100644 --- a/internal/query/projection/org_member.go +++ b/internal/query/projection/org_member.go @@ -7,6 +7,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/org" "github.com/zitadel/zitadel/internal/repository/user" ) @@ -71,6 +72,15 @@ func (p *orgMemberProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(MemberInstanceID), + }, + }, + }, } } diff --git a/internal/query/projection/org_member_test.go b/internal/query/projection/org_member_test.go index 39c3a56dd..2c8d9d083 100644 --- a/internal/query/projection/org_member_test.go +++ b/internal/query/projection/org_member_test.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/org" "github.com/zitadel/zitadel/internal/repository/user" ) @@ -39,7 +40,6 @@ func TestOrgMemberProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: OrgMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -76,7 +76,6 @@ func TestOrgMemberProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: OrgMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -109,7 +108,6 @@ func TestOrgMemberProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: OrgMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -139,7 +137,6 @@ func TestOrgMemberProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: OrgMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -167,7 +164,6 @@ func TestOrgMemberProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: OrgMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -194,7 +190,6 @@ func TestOrgMemberProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: OrgMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -207,6 +202,32 @@ func TestOrgMemberProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(MemberInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.org_members2 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -218,7 +239,7 @@ func TestOrgMemberProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, OrgMemberProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/org_metadata_test.go b/internal/query/projection/org_metadata_test.go index a37e90660..7578396ba 100644 --- a/internal/query/projection/org_metadata_test.go +++ b/internal/query/projection/org_metadata_test.go @@ -37,7 +37,6 @@ func TestOrgMetadataProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: OrgMetadataProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -73,7 +72,6 @@ func TestOrgMetadataProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: OrgMetadataProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -101,7 +99,6 @@ func TestOrgMetadataProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: OrgMetadataProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -128,7 +125,6 @@ func TestOrgMetadataProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: OrgMetadataProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -152,7 +148,7 @@ func TestOrgMetadataProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, OrgMetadataProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/org_test.go b/internal/query/projection/org_test.go index a7a3f789c..2dbd07159 100644 --- a/internal/query/projection/org_test.go +++ b/internal/query/projection/org_test.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/org" ) @@ -32,7 +33,6 @@ func TestOrgProjection_reduces(t *testing.T) { }, reduce: (&orgProjection{}).reducePrimaryDomainSet, want: wantReduce{ - projection: OrgProjectionTable, aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, @@ -62,7 +62,6 @@ func TestOrgProjection_reduces(t *testing.T) { }, reduce: (&orgProjection{}).reduceOrgReactivated, want: wantReduce{ - projection: OrgProjectionTable, aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, @@ -92,7 +91,6 @@ func TestOrgProjection_reduces(t *testing.T) { }, reduce: (&orgProjection{}).reduceOrgDeactivated, want: wantReduce{ - projection: OrgProjectionTable, aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, @@ -122,7 +120,6 @@ func TestOrgProjection_reduces(t *testing.T) { }, reduce: (&orgProjection{}).reduceOrgChanged, want: wantReduce{ - projection: OrgProjectionTable, aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, @@ -152,7 +149,6 @@ func TestOrgProjection_reduces(t *testing.T) { }, reduce: (&orgProjection{}).reduceOrgChanged, want: wantReduce{ - projection: OrgProjectionTable, aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, @@ -170,7 +166,6 @@ func TestOrgProjection_reduces(t *testing.T) { }, reduce: (&orgProjection{}).reduceOrgAdded, want: wantReduce{ - projection: OrgProjectionTable, aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, @@ -193,6 +188,32 @@ func TestOrgProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(OrgColumnInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.orgs WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -204,7 +225,7 @@ func TestOrgProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, OrgProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/password_age_policy.go b/internal/query/projection/password_age_policy.go index affea4eca..0a2594f7d 100644 --- a/internal/query/projection/password_age_policy.go +++ b/internal/query/projection/password_age_policy.go @@ -86,6 +86,10 @@ func (p *passwordAgeProjection) reducers() []handler.AggregateReducer { Event: instance.PasswordAgePolicyChangedEventType, Reduce: p.reduceChanged, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(AgePolicyInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/password_age_policy_test.go b/internal/query/projection/password_age_policy_test.go index a92c72767..a6f256f31 100644 --- a/internal/query/projection/password_age_policy_test.go +++ b/internal/query/projection/password_age_policy_test.go @@ -39,7 +39,6 @@ func TestPasswordAgeProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: PasswordAgeTable, executer: &testExecuter{ executions: []execution{ { @@ -78,7 +77,6 @@ func TestPasswordAgeProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: PasswordAgeTable, executer: &testExecuter{ executions: []execution{ { @@ -109,7 +107,6 @@ func TestPasswordAgeProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: PasswordAgeTable, executer: &testExecuter{ executions: []execution{ { @@ -122,6 +119,32 @@ func TestPasswordAgeProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(AgePolicyInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.password_age_policies WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "instance.reduceAdded", reduce: (&passwordAgeProjection{}).reduceAdded, @@ -139,7 +162,6 @@ func TestPasswordAgeProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: PasswordAgeTable, executer: &testExecuter{ executions: []execution{ { @@ -178,7 +200,6 @@ func TestPasswordAgeProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: PasswordAgeTable, executer: &testExecuter{ executions: []execution{ { @@ -206,7 +227,7 @@ func TestPasswordAgeProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, PasswordAgeTable, tt.want) }) } } diff --git a/internal/query/projection/password_complexity_policy.go b/internal/query/projection/password_complexity_policy.go index a31af8e16..4c8e4b477 100644 --- a/internal/query/projection/password_complexity_policy.go +++ b/internal/query/projection/password_complexity_policy.go @@ -92,6 +92,10 @@ func (p *passwordComplexityProjection) reducers() []handler.AggregateReducer { Event: instance.PasswordComplexityPolicyChangedEventType, Reduce: p.reduceChanged, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(ComplexityPolicyInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/password_complexity_policy_test.go b/internal/query/projection/password_complexity_policy_test.go index 4d8dddbfe..e8bd38d56 100644 --- a/internal/query/projection/password_complexity_policy_test.go +++ b/internal/query/projection/password_complexity_policy_test.go @@ -42,7 +42,6 @@ func TestPasswordComplexityProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: PasswordComplexityTable, executer: &testExecuter{ executions: []execution{ { @@ -87,7 +86,6 @@ func TestPasswordComplexityProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: PasswordComplexityTable, executer: &testExecuter{ executions: []execution{ { @@ -121,7 +119,6 @@ func TestPasswordComplexityProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: PasswordComplexityTable, executer: &testExecuter{ executions: []execution{ { @@ -134,6 +131,32 @@ func TestPasswordComplexityProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(ComplexityPolicyInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.password_complexity_policies WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "instance.reduceAdded", reduce: (&passwordComplexityProjection{}).reduceAdded, @@ -154,7 +177,6 @@ func TestPasswordComplexityProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: PasswordComplexityTable, executer: &testExecuter{ executions: []execution{ { @@ -199,7 +221,6 @@ func TestPasswordComplexityProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: PasswordComplexityTable, executer: &testExecuter{ executions: []execution{ { @@ -230,7 +251,7 @@ func TestPasswordComplexityProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, PasswordComplexityTable, tt.want) }) } } diff --git a/internal/query/projection/privacy_policy.go b/internal/query/projection/privacy_policy.go index 848d73e22..e29e58ab3 100644 --- a/internal/query/projection/privacy_policy.go +++ b/internal/query/projection/privacy_policy.go @@ -88,6 +88,10 @@ func (p *privacyPolicyProjection) reducers() []handler.AggregateReducer { Event: instance.PrivacyPolicyChangedEventType, Reduce: p.reduceChanged, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(PrivacyPolicyInstanceIDCol), + }, }, }, } diff --git a/internal/query/projection/privacy_policy_test.go b/internal/query/projection/privacy_policy_test.go index c4bb0dc8d..89fcb2e42 100644 --- a/internal/query/projection/privacy_policy_test.go +++ b/internal/query/projection/privacy_policy_test.go @@ -40,7 +40,6 @@ func TestPrivacyPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: PrivacyPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -81,7 +80,6 @@ func TestPrivacyPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: PrivacyPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -113,7 +111,6 @@ func TestPrivacyPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("org"), sequence: 15, previousSequence: 10, - projection: PrivacyPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -125,6 +122,31 @@ func TestPrivacyPolicyProjection_reduces(t *testing.T) { }, }, }, + }, { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(PrivacyPolicyInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.privacy_policies WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, }, { name: "instance.reduceAdded", @@ -144,7 +166,6 @@ func TestPrivacyPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: PrivacyPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -185,7 +206,6 @@ func TestPrivacyPolicyProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: PrivacyPolicyTable, executer: &testExecuter{ executions: []execution{ { @@ -214,7 +234,7 @@ func TestPrivacyPolicyProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, PrivacyPolicyTable, tt.want) }) } } diff --git a/internal/query/projection/project.go b/internal/query/projection/project.go index d51753c1d..bc0201d64 100644 --- a/internal/query/projection/project.go +++ b/internal/query/projection/project.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/project" ) @@ -86,6 +87,15 @@ func (p *projectProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(ProjectColumnInstanceID), + }, + }, + }, } } diff --git a/internal/query/projection/project_grant.go b/internal/query/projection/project_grant.go index e58e52c8f..1e4c8f5a3 100644 --- a/internal/query/projection/project_grant.go +++ b/internal/query/projection/project_grant.go @@ -9,6 +9,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/project" ) @@ -92,6 +93,15 @@ func (p *projectGrantProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(ProjectGrantColumnInstanceID), + }, + }, + }, } } diff --git a/internal/query/projection/project_grant_member.go b/internal/query/projection/project_grant_member.go index 414455e9f..3ba8f16b4 100644 --- a/internal/query/projection/project_grant_member.go +++ b/internal/query/projection/project_grant_member.go @@ -7,6 +7,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/member" "github.com/zitadel/zitadel/internal/repository/org" "github.com/zitadel/zitadel/internal/repository/project" @@ -91,6 +92,15 @@ func (p *projectGrantMemberProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(MemberInstanceID), + }, + }, + }, } } @@ -150,6 +160,14 @@ func (p *projectGrantMemberProjection) reduceUserRemoved(event eventstore.Event) return reduceMemberRemoved(e, withMemberCond(MemberUserIDCol, e.Aggregate().ID)) } +func (p *projectGrantMemberProjection) reduceInstanceRemoved(event eventstore.Event) (*handler.Statement, error) { + e, ok := event.(*instance.InstanceRemovedEvent) + if !ok { + return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Z2p6o", "reduce.wrong.event.type %s", instance.InstanceRemovedEventType) + } + return reduceMemberRemoved(e, withMemberCond(MemberInstanceID, e.Aggregate().ID)) +} + func (p *projectGrantMemberProjection) reduceOrgRemoved(event eventstore.Event) (*handler.Statement, error) { //TODO: as soon as org deletion is implemented: // Case: The user has resource owner A and project has resource owner B diff --git a/internal/query/projection/project_grant_member_test.go b/internal/query/projection/project_grant_member_test.go index e1ad1cebc..a8ad644e1 100644 --- a/internal/query/projection/project_grant_member_test.go +++ b/internal/query/projection/project_grant_member_test.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/org" "github.com/zitadel/zitadel/internal/repository/project" "github.com/zitadel/zitadel/internal/repository/user" @@ -41,7 +42,6 @@ func TestProjectGrantMemberProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectGrantMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -80,7 +80,6 @@ func TestProjectGrantMemberProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectGrantMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -115,7 +114,6 @@ func TestProjectGrantMemberProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectGrantMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -147,7 +145,6 @@ func TestProjectGrantMemberProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectGrantMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -176,7 +173,6 @@ func TestProjectGrantMemberProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectGrantMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -203,7 +199,6 @@ func TestProjectGrantMemberProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectGrantMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -215,6 +210,31 @@ func TestProjectGrantMemberProjection_reduces(t *testing.T) { }, }, }, + }, { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(MemberInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.project_grant_members2 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, }, { name: "project.ProjectRemovedEventType", @@ -230,7 +250,6 @@ func TestProjectGrantMemberProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectGrantMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -257,7 +276,6 @@ func TestProjectGrantMemberProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectGrantMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -282,7 +300,7 @@ func TestProjectGrantMemberProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, ProjectGrantMemberProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/project_grant_test.go b/internal/query/projection/project_grant_test.go index 8f20e9634..37ea862bd 100644 --- a/internal/query/projection/project_grant_test.go +++ b/internal/query/projection/project_grant_test.go @@ -9,6 +9,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/project" ) @@ -33,7 +34,6 @@ func TestProjectGrantProjection_reduces(t *testing.T) { }, reduce: (&projectGrantProjection{}).reduceProjectRemoved, want: wantReduce{ - projection: ProjectGrantProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -49,6 +49,32 @@ func TestProjectGrantProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(ProjectGrantColumnInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.project_grants2 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "reduceProjectGrantRemoved", args: args{ @@ -60,7 +86,6 @@ func TestProjectGrantProjection_reduces(t *testing.T) { }, reduce: (&projectGrantProjection{}).reduceProjectGrantRemoved, want: wantReduce{ - projection: ProjectGrantProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -88,7 +113,6 @@ func TestProjectGrantProjection_reduces(t *testing.T) { }, reduce: (&projectGrantProjection{}).reduceProjectGrantReactivated, want: wantReduce{ - projection: ProjectGrantProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -119,7 +143,6 @@ func TestProjectGrantProjection_reduces(t *testing.T) { }, reduce: (&projectGrantProjection{}).reduceProjectGrantDeactivated, want: wantReduce{ - projection: ProjectGrantProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -150,7 +173,6 @@ func TestProjectGrantProjection_reduces(t *testing.T) { }, reduce: (&projectGrantProjection{}).reduceProjectGrantChanged, want: wantReduce{ - projection: ProjectGrantProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -181,7 +203,6 @@ func TestProjectGrantProjection_reduces(t *testing.T) { }, reduce: (&projectGrantProjection{}).reduceProjectGrantCascadeChanged, want: wantReduce{ - projection: ProjectGrantProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -212,7 +233,6 @@ func TestProjectGrantProjection_reduces(t *testing.T) { }, reduce: (&projectGrantProjection{}).reduceProjectGrantAdded, want: wantReduce{ - projection: ProjectGrantProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -248,7 +268,7 @@ func TestProjectGrantProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, ProjectGrantProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/project_member.go b/internal/query/projection/project_member.go index 7d532ffea..8986cebae 100644 --- a/internal/query/projection/project_member.go +++ b/internal/query/projection/project_member.go @@ -7,6 +7,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/member" "github.com/zitadel/zitadel/internal/repository/org" "github.com/zitadel/zitadel/internal/repository/project" @@ -85,6 +86,15 @@ func (p *projectMemberProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(MemberInstanceID), + }, + }, + }, } } diff --git a/internal/query/projection/project_member_test.go b/internal/query/projection/project_member_test.go index fa0310898..fcf08a1d7 100644 --- a/internal/query/projection/project_member_test.go +++ b/internal/query/projection/project_member_test.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/org" "github.com/zitadel/zitadel/internal/repository/project" "github.com/zitadel/zitadel/internal/repository/user" @@ -40,7 +41,6 @@ func TestProjectMemberProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -77,7 +77,6 @@ func TestProjectMemberProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -110,7 +109,6 @@ func TestProjectMemberProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -140,7 +138,6 @@ func TestProjectMemberProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -168,7 +165,6 @@ func TestProjectMemberProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -195,7 +191,6 @@ func TestProjectMemberProjection_reduces(t *testing.T) { aggregateType: org.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -208,6 +203,32 @@ func TestProjectMemberProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(MemberInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.project_members2 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "project.ProjectRemovedEventType", args: args{ @@ -222,7 +243,6 @@ func TestProjectMemberProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: ProjectMemberProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -246,7 +266,7 @@ func TestProjectMemberProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, ProjectMemberProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/project_role.go b/internal/query/projection/project_role.go index e32a4b58a..0f98e069d 100644 --- a/internal/query/projection/project_role.go +++ b/internal/query/projection/project_role.go @@ -7,6 +7,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/project" ) @@ -74,6 +75,15 @@ func (p *projectRoleProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(ProjectRoleColumnInstanceID), + }, + }, + }, } } diff --git a/internal/query/projection/project_role_test.go b/internal/query/projection/project_role_test.go index a2cf0f67e..ec277551c 100644 --- a/internal/query/projection/project_role_test.go +++ b/internal/query/projection/project_role_test.go @@ -7,6 +7,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/project" ) @@ -31,7 +32,6 @@ func TestProjectRoleProjection_reduces(t *testing.T) { }, reduce: (&projectRoleProjection{}).reduceProjectRemoved, want: wantReduce{ - projection: ProjectRoleProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -47,6 +47,32 @@ func TestProjectRoleProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(ProjectRoleColumnInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.project_roles WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "reduceProjectRoleRemoved", args: args{ @@ -58,7 +84,6 @@ func TestProjectRoleProjection_reduces(t *testing.T) { }, reduce: (&projectRoleProjection{}).reduceProjectRoleRemoved, want: wantReduce{ - projection: ProjectRoleProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -86,7 +111,6 @@ func TestProjectRoleProjection_reduces(t *testing.T) { }, reduce: (&projectRoleProjection{}).reduceProjectRoleChanged, want: wantReduce{ - projection: ProjectRoleProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -118,7 +142,6 @@ func TestProjectRoleProjection_reduces(t *testing.T) { }, reduce: (&projectRoleProjection{}).reduceProjectRoleChanged, want: wantReduce{ - projection: ProjectRoleProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -136,7 +159,6 @@ func TestProjectRoleProjection_reduces(t *testing.T) { }, reduce: (&projectRoleProjection{}).reduceProjectRoleAdded, want: wantReduce{ - projection: ProjectRoleProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -171,7 +193,7 @@ func TestProjectRoleProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, ProjectRoleProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/project_test.go b/internal/query/projection/project_test.go index e10d591a5..c034bbd66 100644 --- a/internal/query/projection/project_test.go +++ b/internal/query/projection/project_test.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/project" ) @@ -32,7 +33,6 @@ func TestProjectProjection_reduces(t *testing.T) { }, reduce: (&projectProjection{}).reduceProjectRemoved, want: wantReduce{ - projection: ProjectProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -48,6 +48,32 @@ func TestProjectProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(ProjectColumnInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.projects2 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "reduceProjectReactivated", args: args{ @@ -59,7 +85,6 @@ func TestProjectProjection_reduces(t *testing.T) { }, reduce: (&projectProjection{}).reduceProjectReactivated, want: wantReduce{ - projection: ProjectProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -89,7 +114,6 @@ func TestProjectProjection_reduces(t *testing.T) { }, reduce: (&projectProjection{}).reduceProjectDeactivated, want: wantReduce{ - projection: ProjectProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -119,7 +143,6 @@ func TestProjectProjection_reduces(t *testing.T) { }, reduce: (&projectProjection{}).reduceProjectChanged, want: wantReduce{ - projection: ProjectProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -153,7 +176,6 @@ func TestProjectProjection_reduces(t *testing.T) { }, reduce: (&projectProjection{}).reduceProjectChanged, want: wantReduce{ - projection: ProjectProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -171,7 +193,6 @@ func TestProjectProjection_reduces(t *testing.T) { }, reduce: (&projectProjection{}).reduceProjectAdded, want: wantReduce{ - projection: ProjectProjectionTable, aggregateType: eventstore.AggregateType("project"), sequence: 15, previousSequence: 10, @@ -209,7 +230,7 @@ func TestProjectProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, ProjectProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/secret_generator_test.go b/internal/query/projection/secret_generator_test.go index 1dddb7b6f..ceb17ea8e 100644 --- a/internal/query/projection/secret_generator_test.go +++ b/internal/query/projection/secret_generator_test.go @@ -33,7 +33,6 @@ func TestSecretGeneratorProjection_reduces(t *testing.T) { }, reduce: (&secretGeneratorProjection{}).reduceSecretGeneratorRemoved, want: wantReduce{ - projection: SecretGeneratorProjectionTable, aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, @@ -61,7 +60,6 @@ func TestSecretGeneratorProjection_reduces(t *testing.T) { }, reduce: (&secretGeneratorProjection{}).reduceSecretGeneratorChanged, want: wantReduce{ - projection: SecretGeneratorProjectionTable, aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, @@ -97,7 +95,6 @@ func TestSecretGeneratorProjection_reduces(t *testing.T) { }, reduce: (&secretGeneratorProjection{}).reduceSecretGeneratorAdded, want: wantReduce{ - projection: SecretGeneratorProjectionTable, aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, @@ -136,7 +133,7 @@ func TestSecretGeneratorProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, SecretGeneratorProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/sms.go b/internal/query/projection/sms.go index 8d1058afb..886d8d965 100644 --- a/internal/query/projection/sms.go +++ b/internal/query/projection/sms.go @@ -98,6 +98,10 @@ func (p *smsConfigProjection) reducers() []handler.AggregateReducer { Event: instance.SMSConfigRemovedEventType, Reduce: p.reduceSMSConfigRemoved, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(SMSColumnInstanceID), + }, }, }, } diff --git a/internal/query/projection/sms_test.go b/internal/query/projection/sms_test.go index e492aff93..2ab84d47c 100644 --- a/internal/query/projection/sms_test.go +++ b/internal/query/projection/sms_test.go @@ -46,7 +46,6 @@ func TestSMSProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: SMSConfigProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -99,7 +98,6 @@ func TestSMSProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: SMSConfigProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -146,7 +144,6 @@ func TestSMSProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: SMSConfigProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -191,7 +188,6 @@ func TestSMSProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: SMSConfigProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -224,7 +220,6 @@ func TestSMSProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: SMSConfigProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -257,7 +252,6 @@ func TestSMSProjection_reduces(t *testing.T) { aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, - projection: SMSConfigProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -271,6 +265,32 @@ func TestSMSProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(SMSColumnInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.sms_configs WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -282,7 +302,7 @@ func TestSMSProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, SMSConfigProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/smtp.go b/internal/query/projection/smtp.go index ce0f03119..072d68f9f 100644 --- a/internal/query/projection/smtp.go +++ b/internal/query/projection/smtp.go @@ -74,6 +74,10 @@ func (p *smtpConfigProjection) reducers() []handler.AggregateReducer { Event: instance.SMTPConfigPasswordChangedEventType, Reduce: p.reduceSMTPConfigPasswordChanged, }, + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(SMTPConfigColumnInstanceID), + }, }, }, } diff --git a/internal/query/projection/smtp_test.go b/internal/query/projection/smtp_test.go index 4d0498146..de5044af7 100644 --- a/internal/query/projection/smtp_test.go +++ b/internal/query/projection/smtp_test.go @@ -38,7 +38,6 @@ func TestSMTPConfigProjection_reduces(t *testing.T) { }, reduce: (&smtpConfigProjection{}).reduceSMTPConfigChanged, want: wantReduce{ - projection: SMTPConfigProjectionTable, aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, @@ -84,7 +83,6 @@ func TestSMTPConfigProjection_reduces(t *testing.T) { }, reduce: (&smtpConfigProjection{}).reduceSMTPConfigAdded, want: wantReduce{ - projection: SMTPConfigProjectionTable, aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, @@ -128,7 +126,6 @@ func TestSMTPConfigProjection_reduces(t *testing.T) { }, reduce: (&smtpConfigProjection{}).reduceSMTPConfigPasswordChanged, want: wantReduce{ - projection: SMTPConfigProjectionTable, aggregateType: eventstore.AggregateType("instance"), sequence: 15, previousSequence: 10, @@ -148,6 +145,32 @@ func TestSMTPConfigProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(SMTPConfigColumnInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.smtp_configs WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -159,7 +182,7 @@ func TestSMTPConfigProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, SMTPConfigProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/user.go b/internal/query/projection/user.go index 6f646d46a..5fe55d876 100644 --- a/internal/query/projection/user.go +++ b/internal/query/projection/user.go @@ -9,6 +9,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/user" ) @@ -274,6 +275,15 @@ func (p *userProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(UserInstanceIDCol), + }, + }, + }, } } diff --git a/internal/query/projection/user_auth_method.go b/internal/query/projection/user_auth_method.go index 599562751..73f5ce6e3 100644 --- a/internal/query/projection/user_auth_method.go +++ b/internal/query/projection/user_auth_method.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/user" ) @@ -98,6 +99,15 @@ func (p *userAuthMethodProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(UserAuthMethodInstanceIDCol), + }, + }, + }, } } diff --git a/internal/query/projection/user_auth_method_test.go b/internal/query/projection/user_auth_method_test.go index 5b540a015..fe3cad91c 100644 --- a/internal/query/projection/user_auth_method_test.go +++ b/internal/query/projection/user_auth_method_test.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/user" ) @@ -37,7 +38,6 @@ func TestUserAuthMethodProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserAuthMethodTable, executer: &testExecuter{ executions: []execution{ { @@ -75,7 +75,6 @@ func TestUserAuthMethodProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserAuthMethodTable, executer: &testExecuter{ executions: []execution{ { @@ -112,7 +111,6 @@ func TestUserAuthMethodProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserAuthMethodTable, executer: &testExecuter{ executions: []execution{ { @@ -151,7 +149,6 @@ func TestUserAuthMethodProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserAuthMethodTable, executer: &testExecuter{ executions: []execution{ { @@ -188,7 +185,6 @@ func TestUserAuthMethodProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserAuthMethodTable, executer: &testExecuter{ executions: []execution{ { @@ -223,7 +219,6 @@ func TestUserAuthMethodProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserAuthMethodTable, executer: &testExecuter{ executions: []execution{ { @@ -243,6 +238,32 @@ func TestUserAuthMethodProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(UserAuthMethodInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.user_auth_methods3 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -254,7 +275,7 @@ func TestUserAuthMethodProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, UserAuthMethodTable, tt.want) }) } } diff --git a/internal/query/projection/user_grant.go b/internal/query/projection/user_grant.go index 546eb11a9..93a1ccb97 100644 --- a/internal/query/projection/user_grant.go +++ b/internal/query/projection/user_grant.go @@ -9,6 +9,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/project" "github.com/zitadel/zitadel/internal/repository/user" "github.com/zitadel/zitadel/internal/repository/usergrant" @@ -131,6 +132,15 @@ func (p *userGrantProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(UserGrantInstanceID), + }, + }, + }, } } diff --git a/internal/query/projection/user_grant_test.go b/internal/query/projection/user_grant_test.go index 2d960e5ea..d5e408b9a 100644 --- a/internal/query/projection/user_grant_test.go +++ b/internal/query/projection/user_grant_test.go @@ -9,6 +9,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/project" "github.com/zitadel/zitadel/internal/repository/user" "github.com/zitadel/zitadel/internal/repository/usergrant" @@ -42,7 +43,6 @@ func TestUserGrantProjection_reduces(t *testing.T) { aggregateType: usergrant.AggregateType, sequence: 15, previousSequence: 10, - projection: UserGrantProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -81,7 +81,6 @@ func TestUserGrantProjection_reduces(t *testing.T) { aggregateType: usergrant.AggregateType, sequence: 15, previousSequence: 10, - projection: UserGrantProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -113,7 +112,6 @@ func TestUserGrantProjection_reduces(t *testing.T) { aggregateType: usergrant.AggregateType, sequence: 15, previousSequence: 10, - projection: UserGrantProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -143,7 +141,6 @@ func TestUserGrantProjection_reduces(t *testing.T) { aggregateType: usergrant.AggregateType, sequence: 15, previousSequence: 10, - projection: UserGrantProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -156,6 +153,32 @@ func TestUserGrantProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(UserGrantInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.user_grants2 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, { name: "reduceCascadeRemoved", args: args{ @@ -170,7 +193,6 @@ func TestUserGrantProjection_reduces(t *testing.T) { aggregateType: usergrant.AggregateType, sequence: 15, previousSequence: 10, - projection: UserGrantProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -197,7 +219,6 @@ func TestUserGrantProjection_reduces(t *testing.T) { aggregateType: usergrant.AggregateType, sequence: 15, previousSequence: 10, - projection: UserGrantProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -227,7 +248,6 @@ func TestUserGrantProjection_reduces(t *testing.T) { aggregateType: usergrant.AggregateType, sequence: 15, previousSequence: 10, - projection: UserGrantProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -257,7 +277,6 @@ func TestUserGrantProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserGrantProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -284,7 +303,6 @@ func TestUserGrantProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: UserGrantProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -311,7 +329,6 @@ func TestUserGrantProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: UserGrantProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -338,7 +355,6 @@ func TestUserGrantProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: UserGrantProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -366,7 +382,6 @@ func TestUserGrantProjection_reduces(t *testing.T) { aggregateType: project.AggregateType, sequence: 15, previousSequence: 10, - projection: UserGrantProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -391,7 +406,7 @@ func TestUserGrantProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, UserGrantProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/user_metadata.go b/internal/query/projection/user_metadata.go index 5c00fbc4d..0410b0fa5 100644 --- a/internal/query/projection/user_metadata.go +++ b/internal/query/projection/user_metadata.go @@ -7,6 +7,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/user" ) @@ -74,6 +75,15 @@ func (p *userMetadataProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(UserMetadataColumnInstanceID), + }, + }, + }, } } diff --git a/internal/query/projection/user_metadata_test.go b/internal/query/projection/user_metadata_test.go index 340ac39f2..5cbfaa672 100644 --- a/internal/query/projection/user_metadata_test.go +++ b/internal/query/projection/user_metadata_test.go @@ -7,6 +7,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/user" ) @@ -37,7 +38,6 @@ func TestUserMetadataProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserMetadataProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -73,7 +73,6 @@ func TestUserMetadataProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserMetadataProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -101,7 +100,6 @@ func TestUserMetadataProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserMetadataProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -128,7 +126,6 @@ func TestUserMetadataProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserMetadataProjectionTable, executer: &testExecuter{ executions: []execution{ { @@ -141,6 +138,32 @@ func TestUserMetadataProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(UserMetadataColumnInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.user_metadata3 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -152,7 +175,7 @@ func TestUserMetadataProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, UserMetadataProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/user_personal_access_token.go b/internal/query/projection/user_personal_access_token.go index 737b36c44..2d2c3817a 100644 --- a/internal/query/projection/user_personal_access_token.go +++ b/internal/query/projection/user_personal_access_token.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler/crdb" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/user" ) @@ -74,6 +75,15 @@ func (p *personalAccessTokenProjection) reducers() []handler.AggregateReducer { }, }, }, + { + Aggregate: instance.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: instance.InstanceRemovedEventType, + Reduce: reduceInstanceRemovedHelper(PersonalAccessTokenColumnInstanceID), + }, + }, + }, } } diff --git a/internal/query/projection/user_personal_access_token_test.go b/internal/query/projection/user_personal_access_token_test.go index 8bd215d13..4e5dfb783 100644 --- a/internal/query/projection/user_personal_access_token_test.go +++ b/internal/query/projection/user_personal_access_token_test.go @@ -9,6 +9,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/user" ) @@ -33,7 +34,6 @@ func TestPersonalAccessTokenProjection_reduces(t *testing.T) { }, reduce: (&personalAccessTokenProjection{}).reducePersonalAccessTokenAdded, want: wantReduce{ - projection: PersonalAccessTokenProjectionTable, aggregateType: eventstore.AggregateType("user"), sequence: 15, previousSequence: 10, @@ -68,7 +68,6 @@ func TestPersonalAccessTokenProjection_reduces(t *testing.T) { }, reduce: (&personalAccessTokenProjection{}).reducePersonalAccessTokenRemoved, want: wantReduce{ - projection: PersonalAccessTokenProjectionTable, aggregateType: eventstore.AggregateType("user"), sequence: 15, previousSequence: 10, @@ -95,7 +94,6 @@ func TestPersonalAccessTokenProjection_reduces(t *testing.T) { }, reduce: (&personalAccessTokenProjection{}).reduceUserRemoved, want: wantReduce{ - projection: PersonalAccessTokenProjectionTable, aggregateType: eventstore.AggregateType("user"), sequence: 15, previousSequence: 10, @@ -111,6 +109,32 @@ func TestPersonalAccessTokenProjection_reduces(t *testing.T) { }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(PersonalAccessTokenColumnInstanceID), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.personal_access_tokens2 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -122,7 +146,7 @@ func TestPersonalAccessTokenProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, PersonalAccessTokenProjectionTable, tt.want) }) } } diff --git a/internal/query/projection/user_test.go b/internal/query/projection/user_test.go index 54cebee31..fd55583d7 100644 --- a/internal/query/projection/user_test.go +++ b/internal/query/projection/user_test.go @@ -9,6 +9,7 @@ import ( "github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/repository" + "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/user" ) @@ -46,7 +47,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -116,7 +116,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -181,7 +180,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -251,7 +249,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -321,7 +318,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -386,7 +382,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -446,7 +441,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -475,7 +469,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -504,7 +497,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -533,7 +525,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -562,7 +553,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -593,7 +583,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -624,7 +613,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -655,7 +643,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -686,7 +673,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -716,7 +702,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -749,7 +734,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -787,7 +771,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -837,7 +820,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -882,7 +864,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -931,7 +912,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -978,7 +958,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1026,7 +1005,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1074,7 +1052,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1119,7 +1096,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1166,7 +1142,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1215,7 +1190,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1262,7 +1236,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1307,7 +1280,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1354,7 +1326,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1392,7 +1363,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1433,7 +1403,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1481,7 +1450,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1528,7 +1496,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1569,7 +1536,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1609,7 +1575,6 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{ { @@ -1647,12 +1612,37 @@ func TestUserProjection_reduces(t *testing.T) { aggregateType: user.AggregateType, sequence: 15, previousSequence: 10, - projection: UserTable, executer: &testExecuter{ executions: []execution{}, }, }, }, + { + name: "instance.reduceInstanceRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(instance.InstanceRemovedEventType), + instance.AggregateType, + []byte(`{"name": "Name"}`), + ), instance.InstanceRemovedEventMapper), + }, + reduce: reduceInstanceRemovedHelper(UserInstanceIDCol), + want: wantReduce{ + aggregateType: eventstore.AggregateType("instance"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM projections.users4 WHERE (instance_id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1664,7 +1654,7 @@ func TestUserProjection_reduces(t *testing.T) { event = tt.args.event(t) got, err = tt.reduce(event) - assertReduce(t, got, err, tt.want) + assertReduce(t, got, err, UserTable, tt.want) }) } }