mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 03:24:26 +00:00
fix(postgres <=15): delete unique constraints (#6971)
fix(postgres): delete unique constraints
This commit is contained in:
parent
72bc3ffe14
commit
8982e1aae3
@ -18,6 +18,8 @@ import (
|
|||||||
var (
|
var (
|
||||||
//go:embed unique_constraints_delete.sql
|
//go:embed unique_constraints_delete.sql
|
||||||
deleteConstraintStmt string
|
deleteConstraintStmt string
|
||||||
|
//go:embed unique_constraints_delete_placeholders.sql
|
||||||
|
deleteConstraintPlaceholdersStmt string
|
||||||
//go:embed unique_constraints_add.sql
|
//go:embed unique_constraints_add.sql
|
||||||
addConstraintStmt string
|
addConstraintStmt string
|
||||||
)
|
)
|
||||||
@ -40,9 +42,7 @@ func handleUniqueConstraints(ctx context.Context, tx *sql.Tx, commands []eventst
|
|||||||
addArgs = append(addArgs, command.Aggregate().InstanceID, constraint.UniqueType, constraint.UniqueField)
|
addArgs = append(addArgs, command.Aggregate().InstanceID, constraint.UniqueType, constraint.UniqueField)
|
||||||
addConstraints[fmt.Sprintf(uniqueConstraintPlaceholderFmt, command.Aggregate().InstanceID, constraint.UniqueType, constraint.UniqueField)] = constraint
|
addConstraints[fmt.Sprintf(uniqueConstraintPlaceholderFmt, command.Aggregate().InstanceID, constraint.UniqueType, constraint.UniqueField)] = constraint
|
||||||
case eventstore.UniqueConstraintRemove:
|
case eventstore.UniqueConstraintRemove:
|
||||||
// the query is so complex because we accidentally stored unique constraint case sensitive
|
deletePlaceholders = append(deletePlaceholders, fmt.Sprintf(deleteConstraintPlaceholdersStmt, len(deleteArgs)+1, len(deleteArgs)+2, len(deleteArgs)+3))
|
||||||
// the query checks first if there is a case sensitive match and afterwards if there is a case insensitive match
|
|
||||||
deletePlaceholders = append(deletePlaceholders, fmt.Sprintf("(instance_id = $%[1]d AND unique_type = $%[2]d AND unique_field = (SELECT unique_field from (SELECT instance_id, unique_type, unique_field FROM eventstore.unique_constraints WHERE instance_id = $%[1]d AND unique_type = $%[2]d AND unique_field = $%[3]d UNION ALL SELECT instance_id, unique_type, unique_field FROM eventstore.unique_constraints WHERE instance_id = $%[1]d AND unique_type = $%[2]d AND unique_field = LOWER($%[3]d)) LIMIT 1))", len(deleteArgs)+1, len(deleteArgs)+2, len(deleteArgs)+3))
|
|
||||||
deleteArgs = append(deleteArgs, command.Aggregate().InstanceID, constraint.UniqueType, constraint.UniqueField)
|
deleteArgs = append(deleteArgs, command.Aggregate().InstanceID, constraint.UniqueType, constraint.UniqueField)
|
||||||
deleteConstraints[fmt.Sprintf(uniqueConstraintPlaceholderFmt, command.Aggregate().InstanceID, constraint.UniqueType, constraint.UniqueField)] = constraint
|
deleteConstraints[fmt.Sprintf(uniqueConstraintPlaceholderFmt, command.Aggregate().InstanceID, constraint.UniqueType, constraint.UniqueField)] = constraint
|
||||||
case eventstore.UniqueConstraintInstanceRemove:
|
case eventstore.UniqueConstraintInstanceRemove:
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
-- the query is so complex because we accidentally stored unique constraint case sensitive
|
||||||
|
-- the query checks first if there is a case sensitive match and afterwards if there is a case insensitive match
|
||||||
|
(instance_id = $%[1]d AND unique_type = $%[2]d AND unique_field = (
|
||||||
|
SELECT unique_field from (
|
||||||
|
SELECT instance_id, unique_type, unique_field
|
||||||
|
FROM eventstore.unique_constraints
|
||||||
|
WHERE instance_id = $%[1]d AND unique_type = $%[2]d AND unique_field = $%[3]d
|
||||||
|
UNION ALL
|
||||||
|
SELECT instance_id, unique_type, unique_field
|
||||||
|
FROM eventstore.unique_constraints
|
||||||
|
WHERE instance_id = $%[1]d AND unique_type = $%[2]d AND unique_field = LOWER($%[3]d)
|
||||||
|
) AS case_insensitive_constraints LIMIT 1)
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user