fix(projections): delete app on remove project event (#2772)

* fix(projections): delete app on remove project event

* test(projection): project removed event in app

* more effitient mig
This commit is contained in:
Silvan 2021-12-02 15:07:45 +01:00 committed by GitHub
parent b43362ad15
commit 7a5cb80569
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 0 deletions

View File

@ -56,6 +56,10 @@ func (p *AppProjection) reducers() []handler.AggregateReducer {
Event: project.ApplicationRemovedType,
Reduce: p.reduceAppRemoved,
},
{
Event: project.ProjectRemovedType,
Reduce: p.reduceProjectRemoved,
},
{
Event: project.APIConfigAddedType,
Reduce: p.reduceAPIConfigAdded,
@ -213,6 +217,20 @@ func (p *AppProjection) reduceAppRemoved(event eventstore.EventReader) (*handler
), nil
}
func (p *AppProjection) reduceProjectRemoved(event eventstore.EventReader) (*handler.Statement, error) {
e, ok := event.(*project.ProjectRemovedEvent)
if !ok {
logging.LogWithFields("HANDL-ZxQnj", "seq", event.Sequence(), "expectedType", project.ProjectRemovedType).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-DlUlO", "reduce.wrong.event.type")
}
return crdb.NewDeleteStatement(
e,
[]handler.Condition{
handler.NewCond(AppColumnProjectID, e.Aggregate().ID),
},
), nil
}
func (p *AppProjection) reduceAPIConfigAdded(event eventstore.EventReader) (*handler.Statement, error) {
e, ok := event.(*project.APIConfigAddedEvent)
if !ok {

View File

@ -186,6 +186,33 @@ func TestAppProjection_reduces(t *testing.T) {
},
},
},
{
name: "project.reduceProjectRemoved",
args: args{
event: getEvent(testEvent(
repository.EventType(project.ProjectRemovedType),
project.AggregateType,
[]byte(`{}`),
), project.ProjectRemovedEventMapper),
},
reduce: (&AppProjection{}).reduceProjectRemoved,
want: wantReduce{
aggregateType: eventstore.AggregateType("project"),
sequence: 15,
previousSequence: 10,
projection: AppProjectionTable,
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "DELETE FROM zitadel.projections.apps WHERE (project_id = $1)",
expectedArgs: []interface{}{
"agg-id",
},
},
},
},
},
},
{
name: "project.reduceAPIConfigAdded",
args: args{

View File

@ -0,0 +1,6 @@
DELETE FROM zitadel.projections.apps
WHERE project_id IN (
SELECT aggregate_id
FROM eventstore.events
WHERE aggregate_type = 'project' AND event_type = 'project.removed'
);