From dfcc26de1e7402f6d5a1515fd1ffa6195f22dd0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20M=C3=B6hlmann?= Date: Tue, 5 Mar 2024 17:12:49 +0200 Subject: [PATCH] fix: assign instance ID to aggregate ID when converting from v1 to v2 feature (#7505) * fix: assign instance ID to aggregate ID when converting from v1 to v2 feature This change fixes a mismatch between v1 and v2 aggregate IDs for instance feature events. The old v1 used a random aggregate ID, while v2 uses the instance ID as aggregate ID. The adapter was not correctly mapping, which resulted in the projections.instance_features table being filled with wrong instance IDs. Closes #7501 * fix unit test --- internal/command/instance_features_model.go | 1 - internal/query/instance_by_domain.sql | 2 +- internal/query/instance_by_id.sql | 2 +- internal/query/instance_features_model.go | 1 - internal/query/projection/instance_features.go | 2 +- internal/query/projection/instance_features_test.go | 10 +++++----- internal/repository/feature/feature.go | 4 ++++ 7 files changed, 12 insertions(+), 10 deletions(-) diff --git a/internal/command/instance_features_model.go b/internal/command/instance_features_model.go index ff5ae99ba9..7f618099d9 100644 --- a/internal/command/instance_features_model.go +++ b/internal/command/instance_features_model.go @@ -48,7 +48,6 @@ func (m *InstanceFeaturesWriteModel) Query() *eventstore.SearchQueryBuilder { AwaitOpenTransactions(). AddQuery(). AggregateTypes(feature_v2.AggregateType). - AggregateIDs(m.AggregateID). EventTypes( feature_v1.DefaultLoginInstanceEventType, feature_v2.InstanceResetEventType, diff --git a/internal/query/instance_by_domain.sql b/internal/query/instance_by_domain.sql index ae6cb24249..ffcf8a2f79 100644 --- a/internal/query/instance_by_domain.sql +++ b/internal/query/instance_by_domain.sql @@ -8,7 +8,7 @@ with domain as ( ) features from domain d cross join projections.system_features s - full outer join projections.instance_features i using (key, instance_id) + full outer join projections.instance_features2 i using (key, instance_id) group by instance_id ) select diff --git a/internal/query/instance_by_id.sql b/internal/query/instance_by_id.sql index 54d2b7a949..08398846cc 100644 --- a/internal/query/instance_by_id.sql +++ b/internal/query/instance_by_id.sql @@ -5,7 +5,7 @@ with features as ( ) features from (select $1::text instance_id) x cross join projections.system_features s - full outer join projections.instance_features i using (key, instance_id) + full outer join projections.instance_features2 i using (key, instance_id) group by instance_id ) select diff --git a/internal/query/instance_features_model.go b/internal/query/instance_features_model.go index ff23b82afe..4d6a043d8c 100644 --- a/internal/query/instance_features_model.go +++ b/internal/query/instance_features_model.go @@ -54,7 +54,6 @@ func (m *InstanceFeaturesReadModel) Query() *eventstore.SearchQueryBuilder { AwaitOpenTransactions(). AddQuery(). AggregateTypes(feature_v2.AggregateType). - AggregateIDs(m.AggregateID). EventTypes( feature_v1.DefaultLoginInstanceEventType, feature_v2.InstanceResetEventType, diff --git a/internal/query/projection/instance_features.go b/internal/query/projection/instance_features.go index bf7f0d0413..fae8824429 100644 --- a/internal/query/projection/instance_features.go +++ b/internal/query/projection/instance_features.go @@ -13,7 +13,7 @@ import ( ) const ( - InstanceFeatureTable = "projections.instance_features" + InstanceFeatureTable = "projections.instance_features2" InstanceFeatureInstanceIDCol = "instance_id" InstanceFeatureKeyCol = "key" diff --git a/internal/query/projection/instance_features_test.go b/internal/query/projection/instance_features_test.go index f43233ae67..4a4a46727f 100644 --- a/internal/query/projection/instance_features_test.go +++ b/internal/query/projection/instance_features_test.go @@ -38,7 +38,7 @@ func TestInstanceFeaturesProjection_reduces(t *testing.T) { executer: &testExecuter{ executions: []execution{ { - expectedStmt: "INSERT INTO projections.instance_features (instance_id, key, creation_date, change_date, sequence, value) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (instance_id, key) DO UPDATE SET (creation_date, change_date, sequence, value) = (projections.instance_features.creation_date, EXCLUDED.change_date, EXCLUDED.sequence, EXCLUDED.value)", + expectedStmt: "INSERT INTO projections.instance_features2 (instance_id, key, creation_date, change_date, sequence, value) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (instance_id, key) DO UPDATE SET (creation_date, change_date, sequence, value) = (projections.instance_features2.creation_date, EXCLUDED.change_date, EXCLUDED.sequence, EXCLUDED.value)", expectedArgs: []interface{}{ "agg-id", "legacy_introspection", @@ -69,9 +69,9 @@ func TestInstanceFeaturesProjection_reduces(t *testing.T) { executer: &testExecuter{ executions: []execution{ { - expectedStmt: "INSERT INTO projections.instance_features (instance_id, key, creation_date, change_date, sequence, value) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (instance_id, key) DO UPDATE SET (creation_date, change_date, sequence, value) = (projections.instance_features.creation_date, EXCLUDED.change_date, EXCLUDED.sequence, EXCLUDED.value)", + expectedStmt: "INSERT INTO projections.instance_features2 (instance_id, key, creation_date, change_date, sequence, value) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (instance_id, key) DO UPDATE SET (creation_date, change_date, sequence, value) = (projections.instance_features2.creation_date, EXCLUDED.change_date, EXCLUDED.sequence, EXCLUDED.value)", expectedArgs: []interface{}{ - "agg-id", + "instance-id", "login_default_org", anyArg{}, anyArg{}, @@ -100,7 +100,7 @@ func TestInstanceFeaturesProjection_reduces(t *testing.T) { executer: &testExecuter{ executions: []execution{ { - expectedStmt: "DELETE FROM projections.instance_features WHERE (instance_id = $1)", + expectedStmt: "DELETE FROM projections.instance_features2 WHERE (instance_id = $1)", expectedArgs: []interface{}{ "agg-id", }, @@ -126,7 +126,7 @@ func TestInstanceFeaturesProjection_reduces(t *testing.T) { executer: &testExecuter{ executions: []execution{ { - expectedStmt: "DELETE FROM projections.instance_features WHERE (instance_id = $1)", + expectedStmt: "DELETE FROM projections.instance_features2 WHERE (instance_id = $1)", expectedArgs: []interface{}{ "agg-id", }, diff --git a/internal/repository/feature/feature.go b/internal/repository/feature/feature.go index 25ed66050a..2ec52c464b 100644 --- a/internal/repository/feature/feature.go +++ b/internal/repository/feature/feature.go @@ -22,6 +22,10 @@ func DefaultLoginInstanceEventToV2(e *SetEvent[Boolean]) *feature_v2.SetEvent[bo BaseEvent: e.BaseEvent, Value: e.Value.Boolean, } + + // v1 used a random aggregate ID. + // v2 uses the instance ID as aggregate ID. + v2e.BaseEvent.Agg.ID = e.Agg.InstanceID v2e.BaseEvent.EventType = feature_v2.InstanceLoginDefaultOrgEventType return v2e }