Skip to content

feat(explore): Track service account and org token creators on saved queries - #129549

Merged
DominikB2014 merged 4 commits into
masterfrom
feat/saved-query-non-user-creator
Nov 3, 2026
Merged

DominikB2014 merged 4 commits into
masterfrom
feat/saved-query-non-user-creator

Conversation

@DominikB2014

Copy link
Copy Markdown
Contributor

Saved queries created by service accounts or org auth tokens currently get no author. Same approach as #129219 for dashboards.

  • Add nullable created_by_service_account_id / created_by_org_auth_token_id plus a single-creator check constraint to DiscoverSavedQuery, ExploreSavedQuery and ExploreSavedFormula
  • Create endpoints set the creator via get_creator_reference(request); existing created_by_id is already SET_NULL on all three
  • Exhaustive relocation fixture now attributes the explore query and formula to a service account

Fixes ENG-8935

…queries

Add created_by_service_account_id and created_by_org_auth_token_id to
DiscoverSavedQuery, ExploreSavedQuery and ExploreSavedFormula, with an
at-most-one-creator check constraint, and record the creator via
get_creator_reference() on create.

Fixes ENG-8935
@linear-code

linear-code Bot commented Nov 2, 2026

Copy link
Copy Markdown

ENG-8935

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

This PR has a migration; here is the generated SQL for src/sentry/discover/migrations/0005_add_saved_query_non_user_creator.py src/sentry/explore/migrations/0020_add_saved_query_non_user_creator.py

for 0005_add_saved_query_non_user_creator in discover

--
-- Add field created_by_org_auth_token_id to discoversavedquery
--
ALTER TABLE "sentry_discoversavedquery" ADD COLUMN "created_by_org_auth_token_id" bigint NULL;
--
-- Add field created_by_service_account_id to discoversavedquery
--
ALTER TABLE "sentry_discoversavedquery" ADD COLUMN "created_by_service_account_id" bigint NULL;
--
-- Create constraint sentry_discoversavedquery_single_creator on model discoversavedquery
--
ALTER TABLE "sentry_discoversavedquery" ADD CONSTRAINT "sentry_discoversavedquery_single_creator" CHECK ((("created_by_service_account_id" IS NULL AND "created_by_org_auth_token_id" IS NULL) OR ("created_by_id" IS NULL AND "created_by_org_auth_token_id" IS NULL) OR ("created_by_id" IS NULL AND "created_by_service_account_id" IS NULL))) NOT VALID;
ALTER TABLE "sentry_discoversavedquery" VALIDATE CONSTRAINT "sentry_discoversavedquery_single_creator";
CREATE INDEX CONCURRENTLY "sentry_discoversavedquery_created_by_org_auth_token_id_0ef25d16" ON "sentry_discoversavedquery" ("created_by_org_auth_token_id");
CREATE INDEX CONCURRENTLY "sentry_discoversavedquery_created_by_service_account_c8f8bc44" ON "sentry_discoversavedquery" ("created_by_service_account_id");

for 0020_add_saved_query_non_user_creator in explore

--
-- Add field created_by_org_auth_token_id to exploresavedformula
--
ALTER TABLE "explore_exploresavedformula" ADD COLUMN "created_by_org_auth_token_id" bigint NULL;
--
-- Add field created_by_service_account_id to exploresavedformula
--
ALTER TABLE "explore_exploresavedformula" ADD COLUMN "created_by_service_account_id" bigint NULL;
--
-- Add field created_by_org_auth_token_id to exploresavedquery
--
ALTER TABLE "explore_exploresavedquery" ADD COLUMN "created_by_org_auth_token_id" bigint NULL;
--
-- Add field created_by_service_account_id to exploresavedquery
--
ALTER TABLE "explore_exploresavedquery" ADD COLUMN "created_by_service_account_id" bigint NULL;
--
-- Create constraint explore_exploresavedformula_single_creator on model exploresavedformula
--
ALTER TABLE "explore_exploresavedformula" ADD CONSTRAINT "explore_exploresavedformula_single_creator" CHECK ((("created_by_service_account_id" IS NULL AND "created_by_org_auth_token_id" IS NULL) OR ("created_by_id" IS NULL AND "created_by_org_auth_token_id" IS NULL) OR ("created_by_id" IS NULL AND "created_by_service_account_id" IS NULL))) NOT VALID;
ALTER TABLE "explore_exploresavedformula" VALIDATE CONSTRAINT "explore_exploresavedformula_single_creator";
--
-- Create constraint explore_exploresavedquery_single_creator on model exploresavedquery
--
ALTER TABLE "explore_exploresavedquery" ADD CONSTRAINT "explore_exploresavedquery_single_creator" CHECK ((("created_by_service_account_id" IS NULL AND "created_by_org_auth_token_id" IS NULL) OR ("created_by_id" IS NULL AND "created_by_org_auth_token_id" IS NULL) OR ("created_by_id" IS NULL AND "created_by_service_account_id" IS NULL))) NOT VALID;
ALTER TABLE "explore_exploresavedquery" VALIDATE CONSTRAINT "explore_exploresavedquery_single_creator";
CREATE INDEX CONCURRENTLY "explore_exploresavedformul_created_by_org_auth_token__bc7a4fa8" ON "explore_exploresavedformula" ("created_by_org_auth_token_id");
CREATE INDEX CONCURRENTLY "explore_exploresavedformul_created_by_service_account_b4cd99a6" ON "explore_exploresavedformula" ("created_by_service_account_id");
CREATE INDEX CONCURRENTLY "explore_exploresavedquery_created_by_org_auth_token_id_6ebbce7d" ON "explore_exploresavedquery" ("created_by_org_auth_token_id");
CREATE INDEX CONCURRENTLY "explore_exploresavedquery_created_by_service_account_c069a8a1" ON "explore_exploresavedquery" ("created_by_service_account_id");

@DominikB2014
DominikB2014 marked this pull request as ready for review November 2, 2026 21:12
@DominikB2014
DominikB2014 requested review from a team as code owners November 2, 2026 21:12

@wmak wmak 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.

lgtm

…n-user-creator

# Conflicts:
#	src/sentry/explore/models.py
@DominikB2014
DominikB2014 merged commit 0cfcc3b into master Nov 3, 2026
98 of 99 checks passed
@DominikB2014
DominikB2014 deleted the feat/saved-query-non-user-creator branch November 3, 2026 16:34

This branch was successfully deployed

1 active deployment
Preview — 328d7d94 Deployed Nov 3, 2026 by vercel[bot]
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.

2 participants