fix(postgres <=15): delete unique constraints (#6971)

fix(postgres): delete unique constraints
This commit is contained in:
Elio Bischof 2023-11-24 07:23:23 +01:00 committed by GitHub
parent 72bc3ffe14
commit 8982e1aae3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -18,6 +18,8 @@ import (
var (
//go:embed unique_constraints_delete.sql
deleteConstraintStmt string
//go:embed unique_constraints_delete_placeholders.sql
deleteConstraintPlaceholdersStmt string
//go:embed unique_constraints_add.sql
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)
addConstraints[fmt.Sprintf(uniqueConstraintPlaceholderFmt, command.Aggregate().InstanceID, constraint.UniqueType, constraint.UniqueField)] = constraint
case eventstore.UniqueConstraintRemove:
// 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
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))
deletePlaceholders = append(deletePlaceholders, fmt.Sprintf(deleteConstraintPlaceholdersStmt, len(deleteArgs)+1, len(deleteArgs)+2, len(deleteArgs)+3))
deleteArgs = append(deleteArgs, command.Aggregate().InstanceID, constraint.UniqueType, constraint.UniqueField)
deleteConstraints[fmt.Sprintf(uniqueConstraintPlaceholderFmt, command.Aggregate().InstanceID, constraint.UniqueType, constraint.UniqueField)] = constraint
case eventstore.UniqueConstraintInstanceRemove:

View File

@ -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)
)