Files
zitadel/cmd/setup/79/09_entity_ids.sql
T
765027e0d7 feat(eventstore): remove application constraints by app owner (#12798)
<!-- CURSOR_AGENT_PR_BODY_BEGIN -->
# Which Problems Are Solved

Deleting an application loads the SAML config only to learn the entity
id, then deletes the `entity_ids` unique constraint by that field. The
app name constraint is deleted by `name:projectID`. Neither row is
tagged with the app id, so the delete cannot use the owner index.

# How the Problems Are Solved

`appname` and `entity_ids` rows are tagged `app:<appID>`, in addition to
the existing org and project tags.

- Live writes stamp the tag on application add/rename and on SAML
entity-id add/change.
- Setup 79 appends `app:<appID>` on the next version run, including rows
that already have `org` and `project` owners.
- When the owners backfill is finalized, `RemoveApplication` emits
`RemoveByOwner(app, appID)` and does not read the SAML config.
- Before that backfill, removal still deletes by the app name and, for
SAML apps, by the entity id.

Renames stay field deletes. Project names and roles stay tagged with org
and project only.

# Additional Changes

- `UniqueConstraintOwnerApp` is added next to the other owner kinds.

# Additional Context

- Base branch: `v4.x`
- Follow-up for the unique-constraint owners work (setup 78/79)

Verified locally:

- `go test ./cmd/setup -run TestBackfillUniqueConstraintOwnersStmts`
- `go test ./internal/command -run
'TestCommandSide_RemoveApplication|TestCommandSide_RemoveDynamicOIDCClient'`
- `go test ./internal/eventstore ./internal/v2/eventstore`

<!-- 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>&nbsp;<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>&nbsp;</div>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Silvan <adlerhurst@users.noreply.github.com>
2026-09-23 07:17:09 +02:00

15 lines
547 B
SQL

-- unique_field: entityID (project.NewAddSAMLConfigEntityIDUniqueConstraint)
UPDATE eventstore.unique_constraints uc
SET owners = (
CASE WHEN uc.owners = '{}'
THEN ARRAY['org:' || a.resource_owner, 'project:' || a.project_id]
ELSE uc.owners
END
) || ARRAY['app:' || a.id]
FROM {{.apps}} a
JOIN {{.apps_saml}} s ON s.app_id = a.id AND s.instance_id = a.instance_id
WHERE uc.unique_type = 'entity_ids'
AND uc.instance_id = a.instance_id
AND NOT (uc.owners @> ARRAY['app:' || a.id])
AND uc.unique_field IN (s.entity_id, lower(s.entity_id));