From 47268c738a6ca0e4e4dfd4f104ca7ab98ae714c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20M=C3=B6hlmann?= Date: Wed, 18 Dec 2024 17:48:22 +0200 Subject: [PATCH] fix(setup): make step 39 repeatable (#9085) # Which Problems Are Solved When downgrading zitadel and upgrading it again, it might be that orgs deleted in this period still have stale entries in the fields table. # How the Problems Are Solved - Make the cleanup repeatable - Scope the query by instance so that an index is used. (cherry picked from commit da706a8b30dca5ab4d7d96ae4225dcc9d55d85a5) --- cmd/setup/39.go | 36 ++++++++++++++++++++++++++++++------ cmd/setup/39.sql | 3 ++- cmd/setup/config.go | 1 - cmd/setup/setup.go | 5 +++-- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/cmd/setup/39.go b/cmd/setup/39.go index 01b0ddfcbf..8d78299e95 100644 --- a/cmd/setup/39.go +++ b/cmd/setup/39.go @@ -3,9 +3,12 @@ package setup import ( "context" _ "embed" + "fmt" + + "github.com/zitadel/logging" - "github.com/zitadel/zitadel/internal/database" "github.com/zitadel/zitadel/internal/eventstore" + "github.com/zitadel/zitadel/internal/repository/instance" ) var ( @@ -14,14 +17,35 @@ var ( ) type DeleteStaleOrgFields struct { - dbClient *database.DB + eventstore *eventstore.Eventstore } func (mig *DeleteStaleOrgFields) Execute(ctx context.Context, _ eventstore.Event) error { - _, err := mig.dbClient.ExecContext(ctx, deleteStaleOrgFields) - return err + instances, err := mig.eventstore.InstanceIDs( + ctx, + eventstore.NewSearchQueryBuilder(eventstore.ColumnsInstanceIDs). + OrderDesc(). + AddQuery(). + AggregateTypes("instance"). + EventTypes(instance.InstanceAddedEventType). + Builder(), + ) + if err != nil { + return err + } + for i, instance := range instances { + logging.WithFields("instance_id", instance, "migration", mig.String(), "progress", fmt.Sprintf("%d/%d", i+1, len(instances))).Info("execute delete query") + if _, err := mig.eventstore.Client().ExecContext(ctx, deleteStaleOrgFields, instance); err != nil { + return err + } + } + return nil } -func (mig *DeleteStaleOrgFields) String() string { - return "39_delete_stale_org_fields" +func (*DeleteStaleOrgFields) Check(map[string]any) bool { + return true +} + +func (*DeleteStaleOrgFields) String() string { + return "repeatable_delete_stale_org_fields" } diff --git a/cmd/setup/39.sql b/cmd/setup/39.sql index 2abb701c9f..6d3719fc5c 100644 --- a/cmd/setup/39.sql +++ b/cmd/setup/39.sql @@ -3,6 +3,7 @@ WHERE aggregate_type = 'org' AND aggregate_id IN ( SELECT aggregate_id FROM eventstore.events2 - WHERE aggregate_type = 'org' + WHERE instance_id = $1 + AND aggregate_type = 'org' AND event_type = 'org.removed' ); diff --git a/cmd/setup/config.go b/cmd/setup/config.go index 34bc80d4a2..966b5777a7 100644 --- a/cmd/setup/config.go +++ b/cmd/setup/config.go @@ -127,7 +127,6 @@ type Steps struct { s37Apps7OIDConfigsBackChannelLogoutURI *Apps7OIDConfigsBackChannelLogoutURI s38BackChannelLogoutNotificationStart *BackChannelLogoutNotificationStart s40InitPushFunc *InitPushFunc - s39DeleteStaleOrgFields *DeleteStaleOrgFields } func MustNewSteps(v *viper.Viper) *Steps { diff --git a/cmd/setup/setup.go b/cmd/setup/setup.go index ddf969fa66..7c789c399a 100644 --- a/cmd/setup/setup.go +++ b/cmd/setup/setup.go @@ -169,7 +169,6 @@ func Setup(ctx context.Context, config *Config, steps *Steps, masterKey string) steps.s36FillV2Milestones = &FillV3Milestones{dbClient: queryDBClient, eventstore: eventstoreClient} steps.s37Apps7OIDConfigsBackChannelLogoutURI = &Apps7OIDConfigsBackChannelLogoutURI{dbClient: esPusherDBClient} steps.s38BackChannelLogoutNotificationStart = &BackChannelLogoutNotificationStart{dbClient: esPusherDBClient, esClient: eventstoreClient} - steps.s39DeleteStaleOrgFields = &DeleteStaleOrgFields{dbClient: esPusherDBClient} steps.s40InitPushFunc = &InitPushFunc{dbClient: esPusherDBClient} err = projection.Create(ctx, projectionDBClient, eventstoreClient, config.Projections, nil, nil, nil) @@ -187,6 +186,9 @@ func Setup(ctx context.Context, config *Config, steps *Steps, masterKey string) es: eventstoreClient, Version: build.Version(), }, + &DeleteStaleOrgFields{ + eventstore: eventstoreClient, + }, &FillFieldsForInstanceDomains{ eventstore: eventstoreClient, }, @@ -238,7 +240,6 @@ func Setup(ctx context.Context, config *Config, steps *Steps, masterKey string) steps.s32AddAuthSessionID, steps.s33SMSConfigs3TwilioAddVerifyServiceSid, steps.s37Apps7OIDConfigsBackChannelLogoutURI, - steps.s39DeleteStaleOrgFields, } { mustExecuteMigration(ctx, eventstoreClient, step, "migration failed") }