Skip to content

feat(service-accounts): Only enforce unique names among active accounts - #129914

Merged
skaasten merged 1 commit into
masterfrom
feat/service-accounts-partial-name-unique
Nov 4, 2026
Merged

skaasten merged 1 commit into
masterfrom
feat/service-accounts-partial-name-unique

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

First PR of the service accounts soft-delete plan. Deleting a service account will soon keep the row and set is_active=False, the same way deleting a user works. Seer provisions accounts by name, so a deleted account's name has to stay reusable.

This replaces the (organization_id, name) unique constraint with a partial one on is_active = true. The new constraint is looser than the old one, so it's safe to deploy before the soft-delete code change. The IntegrityError handling for duplicate names in the endpoint and RPC doesn't change.

The migration adds the new index before it drops the old constraint. Sentry's migration runner turns AddConstraint into CREATE UNIQUE INDEX CONCURRENTLY, which runs outside the transaction, so this order means names are never left unconstrained. The table is tiny, because there are no service accounts in production yet.

Until the soft-delete PR lands, an account disabled with isActive: false also frees its name. Turning it back on while a live account holds the same name returns the existing duplicate-name 400 from PUT.

via shaun.kaasten.

--

View Session [Sentry]

Replace the (organization_id, name) unique constraint with a partial one on is_active=True. Deleted service accounts will be soft-deleted with is_active=False, and their names need to stay reusable. The new constraint is looser than the old one, so it ships before the soft-delete code change.

Co-Authored-By: Shaun Kaasten <[email protected]>
@sentry-junior
sentry-junior Bot requested a review from a team as a code owner November 4, 2026 18:29
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 4, 2026
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

This PR has a migration; here is the generated SQL for src/sentry/service_accounts/migrations/0004_partial_name_unique_constraint.py

for 0004_partial_name_unique_constraint in service_accounts

--
-- Create constraint service_accounts_serviceaccount_org_name_unique_active on model serviceaccount
--
CREATE UNIQUE INDEX CONCURRENTLY "service_accounts_serviceaccount_org_name_unique_active" ON "service_accounts_serviceaccount" ("organization_id", "name") WHERE "is_active";
--
-- Remove constraint service_accounts_serviceaccount_org_name_unique from model serviceaccount
--
ALTER TABLE "service_accounts_serviceaccount" DROP CONSTRAINT "service_accounts_serviceaccount_org_name_unique";

@markstory markstory left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. The table is empty so constraints will build quickly.

@skaasten
skaasten merged commit 2ae54d5 into master Nov 4, 2026
97 of 99 checks passed
@skaasten
skaasten deleted the feat/service-accounts-partial-name-unique branch November 4, 2026 19:55
skaasten added a commit that referenced this pull request Nov 5, 2026
PR 2 of the service account soft-delete plan. PR 1 (#129914) made names
unique only among active accounts, so a deleted account's name can be
reused.

Deleting a service account now keeps the row and sets `is_active=False`,
like `User`, instead of hard-deleting it. That keeps references such as
`created_by_service_account_id` on dashboards pointing at a real
account, and it makes the token work later in the plan simpler. The cell
replica stays too, with `is_active=False`.

`ServiceAccount.deactivate()` does the delete. It locks the active row,
removes its project links, clears `all_projects` and turns the account
off. Everything that reads an account through the API or RPC now treats
a deleted account as missing (404 / `None`). The cell-side readers
already reject inactive accounts.

Deleted is now the only way to be inactive, so `isActive` is gone from
the request and the response. In a response it was always `true`, and in
a request only `true` was useful. Like other unknown fields, `isActive`
in a request is now ignored. Seer's generated client still lists the
field, but no Seer code reads or sends it, so it can drop out the next
time the client is regenerated. Audit entries no longer record
`is_active`.

If two DELETE requests race, the second one gets a 404 and writes no
audit entry.

Left as is:
- Seer's "Service account is inactive" error message. Seer tests match
on it, and the plan marks the change as optional.
- No new org-deletion test. Rows still go with the org through the
existing cascade, and a test would only exercise that.

<!-- junior-request-attribution:start -->
via **shaun.kaasten**.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC0CA89MBDQB%3A1793816514.925669 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0CA89MBDQB%3A1793816514.925669)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0CA89MBDQB%3A1793816514.925669/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Shaun Kaasten <[email protected]>
Co-authored-by: Shaun Kaasten <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants