mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-14 20:08:02 +00:00
fix: ensure uniqueness (#6956)
* fix: ensure uniqueness
* only update wrong ones
* Update cmd/setup/16.go
Co-authored-by: Silvan <silvan.reusser@gmail.com>
---------
Co-authored-by: Silvan <silvan.reusser@gmail.com>
(cherry picked from commit b563041103
)
This commit is contained in:
parent
7786b09444
commit
3a0f7ef193
33
cmd/setup/16.go
Normal file
33
cmd/setup/16.go
Normal file
@ -0,0 +1,33 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/database"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed 16.sql
|
||||
uniqueConstraintLower string
|
||||
)
|
||||
|
||||
type UniqueConstraintToLower struct {
|
||||
dbClient *database.DB
|
||||
}
|
||||
|
||||
func (mig *UniqueConstraintToLower) Execute(ctx context.Context) error {
|
||||
res, err := mig.dbClient.ExecContext(ctx, uniqueConstraintLower)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
count, err := res.RowsAffected()
|
||||
logging.WithFields("count", count).Info("unique constraints updated")
|
||||
return err
|
||||
}
|
||||
|
||||
func (mig *UniqueConstraintToLower) String() string {
|
||||
return "16_unique_constraint_lower"
|
||||
}
|
13
cmd/setup/16.sql
Normal file
13
cmd/setup/16.sql
Normal file
@ -0,0 +1,13 @@
|
||||
WITH casesensitive as (
|
||||
SELECT instance_id, unique_type, lower(unique_field)
|
||||
FROM eventstore.unique_constraints
|
||||
GROUP BY instance_id, unique_type, lower(unique_field)
|
||||
HAVING count(unique_field) < 2
|
||||
)
|
||||
UPDATE eventstore.unique_constraints c
|
||||
SET unique_field = casesensitive.lower
|
||||
FROM casesensitive
|
||||
WHERE c.instance_id = casesensitive.instance_id
|
||||
AND c.unique_type = casesensitive.unique_type
|
||||
AND lower(c.unique_field) = casesensitive.lower
|
||||
AND c.unique_field <> casesensitive.lower;
|
@ -61,18 +61,19 @@ func MustNewConfig(v *viper.Viper) *Config {
|
||||
}
|
||||
|
||||
type Steps struct {
|
||||
s1ProjectionTable *ProjectionTable
|
||||
s2AssetsTable *AssetTable
|
||||
FirstInstance *FirstInstance
|
||||
s5LastFailed *LastFailed
|
||||
s6OwnerRemoveColumns *OwnerRemoveColumns
|
||||
s7LogstoreTables *LogstoreTables
|
||||
s8AuthTokens *AuthTokenIndexes
|
||||
CorrectCreationDate *CorrectCreationDate
|
||||
s12AddOTPColumns *AddOTPColumns
|
||||
s13FixQuotaProjection *FixQuotaConstraints
|
||||
s14NewEventsTable *NewEventsTable
|
||||
s15CurrentStates *CurrentProjectionState
|
||||
s1ProjectionTable *ProjectionTable
|
||||
s2AssetsTable *AssetTable
|
||||
FirstInstance *FirstInstance
|
||||
s5LastFailed *LastFailed
|
||||
s6OwnerRemoveColumns *OwnerRemoveColumns
|
||||
s7LogstoreTables *LogstoreTables
|
||||
s8AuthTokens *AuthTokenIndexes
|
||||
CorrectCreationDate *CorrectCreationDate
|
||||
s12AddOTPColumns *AddOTPColumns
|
||||
s13FixQuotaProjection *FixQuotaConstraints
|
||||
s14NewEventsTable *NewEventsTable
|
||||
s15CurrentStates *CurrentProjectionState
|
||||
s16UniqueConstraintsLower *UniqueConstraintToLower
|
||||
}
|
||||
|
||||
type encryptionKeyConfig struct {
|
||||
|
@ -100,6 +100,7 @@ func Setup(config *Config, steps *Steps, masterKey string) {
|
||||
steps.s13FixQuotaProjection = &FixQuotaConstraints{dbClient: zitadelDBClient}
|
||||
steps.s14NewEventsTable = &NewEventsTable{dbClient: esPusherDBClient}
|
||||
steps.s15CurrentStates = &CurrentProjectionState{dbClient: zitadelDBClient}
|
||||
steps.s16UniqueConstraintsLower = &UniqueConstraintToLower{dbClient: zitadelDBClient}
|
||||
|
||||
err = projection.Create(ctx, zitadelDBClient, eventstoreClient, config.Projections, nil, nil, nil)
|
||||
logging.OnError(err).Fatal("unable to start projections")
|
||||
@ -140,6 +141,8 @@ func Setup(config *Config, steps *Steps, masterKey string) {
|
||||
logging.WithFields("name", steps.s13FixQuotaProjection.String()).OnError(err).Fatal("migration failed")
|
||||
err = migration.Migrate(ctx, eventstoreClient, steps.s15CurrentStates)
|
||||
logging.WithFields("name", steps.s15CurrentStates.String()).OnError(err).Fatal("migration failed")
|
||||
err = migration.Migrate(ctx, eventstoreClient, steps.s16UniqueConstraintsLower)
|
||||
logging.WithFields("name", steps.s16UniqueConstraintsLower.String()).OnError(err).Fatal("migration failed")
|
||||
|
||||
for _, repeatableStep := range repeatableSteps {
|
||||
err = migration.Migrate(ctx, eventstoreClient, repeatableStep)
|
||||
|
@ -33,6 +33,7 @@ func handleUniqueConstraints(ctx context.Context, tx *sql.Tx, commands []eventst
|
||||
|
||||
for _, command := range commands {
|
||||
for _, constraint := range command.UniqueConstraints() {
|
||||
constraint.UniqueField = strings.ToLower(constraint.UniqueField)
|
||||
switch constraint.Action {
|
||||
case eventstore.UniqueConstraintAdd:
|
||||
addPlaceholders = append(addPlaceholders, fmt.Sprintf("($%d, $%d, $%d)", len(addArgs)+1, len(addArgs)+2, len(addArgs)+3))
|
||||
|
Loading…
Reference in New Issue
Block a user