fix: use hash to compare user metadata value (#10749)

# Which Problems Are Solved

Depending on the metadata values (already existing), the newly created
index (#10415) cannot be created or error in the future.

# How the Problems Are Solved

- Create the index using `sha256` and change the query to use sha256 as
well when comparing bytes values such as user_metadata.
- Added a setup step to cleanup potentially created index on
`projections.user_metadata5`

# Additional Changes

None

# Additional Context

- relates to #10415
- requires backport to v4.x

(cherry picked from commit 57e8033b6e)
This commit is contained in:
Livio Spring
2025-09-18 11:50:56 +02:00
parent eb18bf3ae6
commit 3667e0dac9
7 changed files with 40 additions and 8 deletions

27
cmd/setup/65.go Normal file
View File

@@ -0,0 +1,27 @@
package setup
import (
"context"
_ "embed"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/eventstore"
)
var (
//go:embed 65.sql
userMetadata5Index string
)
type FixUserMetadata5Index struct {
dbClient *database.DB
}
func (mig *FixUserMetadata5Index) Execute(ctx context.Context, _ eventstore.Event) error {
_, err := mig.dbClient.ExecContext(ctx, userMetadata5Index)
return err
}
func (mig *FixUserMetadata5Index) String() string {
return "65_fix_user_metadata5_index"
}

3
cmd/setup/65.sql Normal file
View File

@@ -0,0 +1,3 @@
ALTER INDEX IF EXISTS projections.user_metadata5_metadata_key_idx
RENAME TO user_metadata5_key_idx;
DROP INDEX IF EXISTS projections.user_metadata5_metadata_value_idx;

View File

@@ -161,6 +161,7 @@ type Steps struct {
s62HTTPProviderAddSigningKey *HTTPProviderAddSigningKey
s63AlterResourceCounts *AlterResourceCounts
s64ChangePushPosition *ChangePushPosition
s65FixUserMetadata5Index *FixUserMetadata5Index
}
func MustNewSteps(v *viper.Viper) *Steps {

View File

@@ -222,6 +222,7 @@ func Setup(ctx context.Context, config *Config, steps *Steps, masterKey string)
steps.s62HTTPProviderAddSigningKey = &HTTPProviderAddSigningKey{dbClient: dbClient}
steps.s63AlterResourceCounts = &AlterResourceCounts{dbClient: dbClient}
steps.s64ChangePushPosition = &ChangePushPosition{dbClient: dbClient}
steps.s65FixUserMetadata5Index = &FixUserMetadata5Index{dbClient: dbClient}
err = projection.Create(ctx, dbClient, eventstoreClient, config.Projections, nil, nil, nil)
logging.OnError(err).Fatal("unable to start projections")
@@ -274,6 +275,7 @@ func Setup(ctx context.Context, config *Config, steps *Steps, masterKey string)
steps.s62HTTPProviderAddSigningKey,
steps.s63AlterResourceCounts,
steps.s64ChangePushPosition,
steps.s65FixUserMetadata5Index,
} {
setupErr = executeMigration(ctx, eventstoreClient, step, "migration failed")
if setupErr != nil {