mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-20 20:08:46 +00:00
fix(eventstore): omit owners on empty unique constraint inserts (#12785)
<!-- CURSOR_AGENT_PR_BODY_BEGIN --> # Which Problems Are Solved After #12740, setup on existing deployments (v4.17.x → v4.18.0) fails before migration 78 can add `eventstore.unique_constraints.owners`. Starting any setup step pushes `system.migration.started`, which inserts a unique constraint that names the `owners` column. On upgraded databases that column does not exist yet, so setup dies on `76_users14_login_equality_indexes` with `SQLSTATE 42703`. # How the Problems Are Solved Empty-owner unique-constraint inserts no longer name the `owners` column: - `len(Owners) == 0` → `INSERT (instance_id, unique_type, unique_field)` - tagged `WithOwners(...)` → unchanged `INSERT (..., owners)` Setup started/done events never stamp owners, so 76–78 can start. Migration 78 still adds the column; after that, omitted `owners` uses `DEFAULT '{}'`. Both v2 and v3 pushers apply the same rule. Mixed pushes split into two INSERT batches so tagged and empty rows do not share a column list. # Additional Changes - v2 SQL mocks now expect the three-column INSERT for untagged adds, plus tagged and mixed cases - Pusher tests cover mixed tagged+empty adds and adds against a table that does not have `owners` yet - The missing-column pusher test drops `owners` and pushes in one transaction (`PushWithClient`), then rolls back so the shared test schema is not mutated. Other tests may stall on `AccessExclusiveLock` until rollback. # Additional Context - Closes #12780 - Follow-up for PR #12740 - Operators do not need to re-run init or apply DDL outside setup Verified locally: - `go test ./internal/eventstore ./internal/v2/eventstore/postgres ./internal/eventstore/v3` (including `-race` on those packages) - Empty-owner insert succeeds when the `owners` column is missing; tagged insert still requires the column <!-- CURSOR_AGENT_PR_BODY_END --> <div><a href="https://cursor.com/agents/bc-31bfb5c4-5888-43f0-85cf-af1af097ca30?cursor_ref=pr_footer&cursor_cta=open_in_web"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-web-light.png"><img alt="Open in Web" width="114" height="28" src="https://cursor.com/assets/images/open-in-web-dark.png"></picture></a> <a href="https://cursor.com/background-agent?bcId=bc-31bfb5c4-5888-43f0-85cf-af1af097ca30&cursor_ref=pr_footer&cursor_cta=open_in_cursor"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-cursor-light.png"><img alt="Open in Cursor" width="131" height="28" src="https://cursor.com/assets/images/open-in-cursor-dark.png"></picture></a> </div> --------- Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: Silvan <[email protected]>
This commit is contained in:
co-authored by
Cursor Agent
Silvan
parent
4bc484dac9
commit
0a08e7269a
@@ -216,15 +216,25 @@ func uniqueConstraints(ctx context.Context, tx *sql.Tx, commands []*command) (er
|
||||
}
|
||||
switch constraint.Action {
|
||||
case eventstore.UniqueConstraintAdd:
|
||||
stmt.WriteString(`INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field, owners) VALUES (`)
|
||||
stmt.WriteArg(instance)
|
||||
stmt.WriteString(`, `)
|
||||
stmt.WriteArg(constraint.UniqueType)
|
||||
stmt.WriteString(`, `)
|
||||
stmt.WriteArg(constraint.UniqueField)
|
||||
stmt.WriteString(`, `)
|
||||
stmt.WriteArg(constraint.Owners)
|
||||
stmt.WriteString(`)`)
|
||||
if len(constraint.Owners) == 0 {
|
||||
stmt.WriteString(`INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field) VALUES (`)
|
||||
stmt.WriteArg(instance)
|
||||
stmt.WriteString(`, `)
|
||||
stmt.WriteArg(constraint.UniqueType)
|
||||
stmt.WriteString(`, `)
|
||||
stmt.WriteArg(constraint.UniqueField)
|
||||
stmt.WriteString(`)`)
|
||||
} else {
|
||||
stmt.WriteString(`INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field, owners) VALUES (`)
|
||||
stmt.WriteArg(instance)
|
||||
stmt.WriteString(`, `)
|
||||
stmt.WriteArg(constraint.UniqueType)
|
||||
stmt.WriteString(`, `)
|
||||
stmt.WriteArg(constraint.UniqueField)
|
||||
stmt.WriteString(`, `)
|
||||
stmt.WriteArg(constraint.Owners)
|
||||
stmt.WriteString(`)`)
|
||||
}
|
||||
case eventstore.UniqueConstraintInstanceRemove:
|
||||
stmt.WriteString(`DELETE FROM eventstore.unique_constraints WHERE instance_id = `)
|
||||
stmt.WriteArgs(instance)
|
||||
|
||||
@@ -64,10 +64,38 @@ func Test_uniqueConstraints(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
expectations: []mock.Expectation{
|
||||
mock.ExpectExec(
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field) VALUES ($1, $2, $3)",
|
||||
mock.WithExecArgs("instance", "test", "id"),
|
||||
mock.WithExecRowsAffected(1),
|
||||
),
|
||||
},
|
||||
},
|
||||
assertErr: expectNoErr,
|
||||
},
|
||||
{
|
||||
name: "add 1 constraint with owners",
|
||||
args: args{
|
||||
commands: []*command{
|
||||
{
|
||||
intent: &intent{
|
||||
PushAggregate: eventstore.NewPushIntent(
|
||||
"instance",
|
||||
eventstore.AppendAggregate("", "", ""),
|
||||
).Aggregates()[0],
|
||||
},
|
||||
Command: &eventstore.Command{
|
||||
UniqueConstraints: []*eventstore.UniqueConstraint{
|
||||
eventstore.NewAddEventUniqueConstraint("test", "id", "error").WithOwners("org:org-1", "user:u1"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
expectations: []mock.Expectation{
|
||||
mock.ExpectExec(
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field, owners) VALUES ($1, $2, $3, $4)",
|
||||
mock.WithExecArgs("instance", "test", "id", []string{}),
|
||||
mock.WithExecArgs("instance", "test", "id", []string{"org:org-1", "user:u1"}),
|
||||
mock.WithExecRowsAffected(1),
|
||||
),
|
||||
},
|
||||
@@ -94,8 +122,8 @@ func Test_uniqueConstraints(t *testing.T) {
|
||||
},
|
||||
expectations: []mock.Expectation{
|
||||
mock.ExpectExec(
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field, owners) VALUES ($1, $2, $3, $4)",
|
||||
mock.WithExecArgs("", "test", "id", []string{}),
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field) VALUES ($1, $2, $3)",
|
||||
mock.WithExecArgs("", "test", "id"),
|
||||
mock.WithExecRowsAffected(1),
|
||||
),
|
||||
},
|
||||
@@ -123,13 +151,47 @@ func Test_uniqueConstraints(t *testing.T) {
|
||||
},
|
||||
expectations: []mock.Expectation{
|
||||
mock.ExpectExec(
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field, owners) VALUES ($1, $2, $3, $4)",
|
||||
mock.WithExecArgs("instance", "test", "id", []string{}),
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field) VALUES ($1, $2, $3)",
|
||||
mock.WithExecArgs("instance", "test", "id"),
|
||||
mock.WithExecRowsAffected(1),
|
||||
),
|
||||
mock.ExpectExec(
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field) VALUES ($1, $2, $3)",
|
||||
mock.WithExecArgs("instance", "test", "id2"),
|
||||
mock.WithExecRowsAffected(1),
|
||||
),
|
||||
},
|
||||
},
|
||||
assertErr: expectNoErr,
|
||||
},
|
||||
{
|
||||
name: "add tagged and empty owners 1 command",
|
||||
args: args{
|
||||
commands: []*command{
|
||||
{
|
||||
intent: &intent{
|
||||
PushAggregate: eventstore.NewPushIntent(
|
||||
"instance",
|
||||
eventstore.AppendAggregate("", "", ""),
|
||||
).Aggregates()[0],
|
||||
},
|
||||
Command: &eventstore.Command{
|
||||
UniqueConstraints: []*eventstore.UniqueConstraint{
|
||||
eventstore.NewAddEventUniqueConstraint("test", "id", "error").WithOwners("org:org-1"),
|
||||
eventstore.NewAddEventUniqueConstraint("test", "id2", "error"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
expectations: []mock.Expectation{
|
||||
mock.ExpectExec(
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field, owners) VALUES ($1, $2, $3, $4)",
|
||||
mock.WithExecArgs("instance", "test", "id2", []string{}),
|
||||
mock.WithExecArgs("instance", "test", "id", []string{"org:org-1"}),
|
||||
mock.WithExecRowsAffected(1),
|
||||
),
|
||||
mock.ExpectExec(
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field) VALUES ($1, $2, $3)",
|
||||
mock.WithExecArgs("instance", "test", "id2"),
|
||||
mock.WithExecRowsAffected(1),
|
||||
),
|
||||
},
|
||||
@@ -169,13 +231,13 @@ func Test_uniqueConstraints(t *testing.T) {
|
||||
},
|
||||
expectations: []mock.Expectation{
|
||||
mock.ExpectExec(
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field, owners) VALUES ($1, $2, $3, $4)",
|
||||
mock.WithExecArgs("instance", "test", "id", []string{}),
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field) VALUES ($1, $2, $3)",
|
||||
mock.WithExecArgs("instance", "test", "id"),
|
||||
mock.WithExecRowsAffected(1),
|
||||
),
|
||||
mock.ExpectExec(
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field, owners) VALUES ($1, $2, $3, $4)",
|
||||
mock.WithExecArgs("instance", "test", "id2", []string{}),
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field) VALUES ($1, $2, $3)",
|
||||
mock.WithExecArgs("instance", "test", "id2"),
|
||||
mock.WithExecRowsAffected(1),
|
||||
),
|
||||
},
|
||||
@@ -440,8 +502,8 @@ func Test_uniqueConstraints(t *testing.T) {
|
||||
},
|
||||
expectations: []mock.Expectation{
|
||||
mock.ExpectExec(
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field, owners) VALUES ($1, $2, $3, $4)",
|
||||
mock.WithExecArgs("instance", "test", "id", []string{}),
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field) VALUES ($1, $2, $3)",
|
||||
mock.WithExecArgs("instance", "test", "id"),
|
||||
mock.WithExecErr(execErr),
|
||||
),
|
||||
},
|
||||
@@ -474,8 +536,8 @@ func Test_uniqueConstraints(t *testing.T) {
|
||||
},
|
||||
expectations: []mock.Expectation{
|
||||
mock.ExpectExec(
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field, owners) VALUES ($1, $2, $3, $4)",
|
||||
mock.WithExecArgs("instance", "test", "id", []string{}),
|
||||
"INSERT INTO eventstore.unique_constraints (instance_id, unique_type, unique_field) VALUES ($1, $2, $3)",
|
||||
mock.WithExecArgs("instance", "test", "id"),
|
||||
mock.WithExecErr(execErr),
|
||||
),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user