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
This commit is contained in:
Tim Möhlmann 2024-03-05 17:12:49 +02:00 committed by GitHub
parent 8f898775c9
commit dfcc26de1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 12 additions and 10 deletions

View File

@ -48,7 +48,6 @@ func (m *InstanceFeaturesWriteModel) Query() *eventstore.SearchQueryBuilder {
AwaitOpenTransactions(). AwaitOpenTransactions().
AddQuery(). AddQuery().
AggregateTypes(feature_v2.AggregateType). AggregateTypes(feature_v2.AggregateType).
AggregateIDs(m.AggregateID).
EventTypes( EventTypes(
feature_v1.DefaultLoginInstanceEventType, feature_v1.DefaultLoginInstanceEventType,
feature_v2.InstanceResetEventType, feature_v2.InstanceResetEventType,

View File

@ -8,7 +8,7 @@ with domain as (
) features ) features
from domain d from domain d
cross join projections.system_features s 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 group by instance_id
) )
select select

View File

@ -5,7 +5,7 @@ with features as (
) features ) features
from (select $1::text instance_id) x from (select $1::text instance_id) x
cross join projections.system_features s 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 group by instance_id
) )
select select

View File

@ -54,7 +54,6 @@ func (m *InstanceFeaturesReadModel) Query() *eventstore.SearchQueryBuilder {
AwaitOpenTransactions(). AwaitOpenTransactions().
AddQuery(). AddQuery().
AggregateTypes(feature_v2.AggregateType). AggregateTypes(feature_v2.AggregateType).
AggregateIDs(m.AggregateID).
EventTypes( EventTypes(
feature_v1.DefaultLoginInstanceEventType, feature_v1.DefaultLoginInstanceEventType,
feature_v2.InstanceResetEventType, feature_v2.InstanceResetEventType,

View File

@ -13,7 +13,7 @@ import (
) )
const ( const (
InstanceFeatureTable = "projections.instance_features" InstanceFeatureTable = "projections.instance_features2"
InstanceFeatureInstanceIDCol = "instance_id" InstanceFeatureInstanceIDCol = "instance_id"
InstanceFeatureKeyCol = "key" InstanceFeatureKeyCol = "key"

View File

@ -38,7 +38,7 @@ func TestInstanceFeaturesProjection_reduces(t *testing.T) {
executer: &testExecuter{ executer: &testExecuter{
executions: []execution{ 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{}{ expectedArgs: []interface{}{
"agg-id", "agg-id",
"legacy_introspection", "legacy_introspection",
@ -69,9 +69,9 @@ func TestInstanceFeaturesProjection_reduces(t *testing.T) {
executer: &testExecuter{ executer: &testExecuter{
executions: []execution{ 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{}{ expectedArgs: []interface{}{
"agg-id", "instance-id",
"login_default_org", "login_default_org",
anyArg{}, anyArg{},
anyArg{}, anyArg{},
@ -100,7 +100,7 @@ func TestInstanceFeaturesProjection_reduces(t *testing.T) {
executer: &testExecuter{ executer: &testExecuter{
executions: []execution{ executions: []execution{
{ {
expectedStmt: "DELETE FROM projections.instance_features WHERE (instance_id = $1)", expectedStmt: "DELETE FROM projections.instance_features2 WHERE (instance_id = $1)",
expectedArgs: []interface{}{ expectedArgs: []interface{}{
"agg-id", "agg-id",
}, },
@ -126,7 +126,7 @@ func TestInstanceFeaturesProjection_reduces(t *testing.T) {
executer: &testExecuter{ executer: &testExecuter{
executions: []execution{ executions: []execution{
{ {
expectedStmt: "DELETE FROM projections.instance_features WHERE (instance_id = $1)", expectedStmt: "DELETE FROM projections.instance_features2 WHERE (instance_id = $1)",
expectedArgs: []interface{}{ expectedArgs: []interface{}{
"agg-id", "agg-id",
}, },

View File

@ -22,6 +22,10 @@ func DefaultLoginInstanceEventToV2(e *SetEvent[Boolean]) *feature_v2.SetEvent[bo
BaseEvent: e.BaseEvent, BaseEvent: e.BaseEvent,
Value: e.Value.Boolean, 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 v2e.BaseEvent.EventType = feature_v2.InstanceLoginDefaultOrgEventType
return v2e return v2e
} }