Skip to content

chore(alerts): Remove metric alert columns on NotificationMessage#115578

Open
ceorourke wants to merge 1 commit into
masterfrom
ceorourke/rm-metric-alert-columns-notificationmessag
Open

chore(alerts): Remove metric alert columns on NotificationMessage#115578
ceorourke wants to merge 1 commit into
masterfrom
ceorourke/rm-metric-alert-columns-notificationmessag

Conversation

@ceorourke
Copy link
Copy Markdown
Member

Follow up to #115529 to start removing the incident and trigger_action column on NotifcationMessage. Since only action and group will be used now, I'm trying to make those not nullable.

@ceorourke ceorourke requested review from a team as code owners May 14, 2026 17:52
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label May 14, 2026
action = FlexibleForeignKey("workflow_engine.Action", null=True)
group = FlexibleForeignKey("sentry.Group", null=True)
action = FlexibleForeignKey("workflow_engine.Action", db_default=0)
group = FlexibleForeignKey("sentry.Group", db_default=0)
Copy link
Copy Markdown
Member Author

@ceorourke ceorourke May 14, 2026

Choose a reason for hiding this comment

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

this is probably not what I should do, but I am unsure of how to handle this situation. I have to provide a default value if I want to make these not nullable. I guess another option is to leave them as nullable but add a constraint saying they both have to be set, but that seems like a weird way to go about it.

Comment on lines +39 to +49
),
),
migrations.AlterField(
model_name="notificationmessage",
name="group",
field=sentry.db.models.fields.foreignkey.FlexibleForeignKey(
db_default=0,
on_delete=django.db.models.deletion.CASCADE,
to="sentry.group",
),
),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: This migration will fail because it changes action and group to non-nullable, but existing rows have NULL values that are not handled by a data migration.
Severity: CRITICAL

Suggested Fix

Add a RunPython data migration before this schema change to either delete the NotificationMessage rows with NULL action and group columns or backfill them with valid data.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location:
src/sentry/notifications/migrations/0009_remove_metric_alert_columns_notificationmessage.py#L32-L49

Potential issue: The migration attempts to change the `action` and `group` ForeignKey
fields on the `NotificationMessage` model to be non-nullable, setting `db_default=0`.
However, existing `NotificationMessage` rows for metric alerts have `NULL` values for
these columns. When the migration is applied, it will fail. The database will either
refuse to apply the `NOT NULL` constraint on a column containing `NULL`s, or the attempt
to update `NULL`s to the default value of `0` will violate foreign key constraints, as
`0` is not a valid ID in the referenced `workflow_engine_action` and `sentry_group`
tables. No preceding data migration cleans up or backfills these `NULL` values.

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link
Copy Markdown
Member

@wedamija wedamija left a comment

Choose a reason for hiding this comment

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

If these columns already have values, you can do the same thing that I did here:
https://github.com/getsentry/sentry/pull/115524/changes

However, it looks like there are many rows with null values: https://redash.de.getsentry.net/queries/367/source

So you either need to provide a default, or clean up these rows

Copy link
Copy Markdown
Contributor

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit eea68d7. Configure here.

on_delete=django.db.models.deletion.CASCADE,
to="sentry.group",
),
),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Invalid FK default breaks migration with existing NULLs

High Severity

The action and group fields use db_default=0 while retaining db_constraint=True (the default). No Action or Group with id=0 exists. When Django applies the AlterField from null=True to non-nullable, it generates UPDATE ... SET action_id = 0 WHERE action_id IS NULL before SET NOT NULL. If any rows have NULL action_id or group_id (likely, since the old metric alert path used incident/trigger_action and left these NULL), the UPDATE violates the FK constraint, causing an IntegrityError that blocks deployment.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit eea68d7. Configure here.

@github-actions
Copy link
Copy Markdown
Contributor

This PR has a migration; here is the generated SQL for src/sentry/notifications/migrations/0009_remove_metric_alert_columns_notificationmessage.py

for 0009_remove_metric_alert_columns_notificationmessage in notifications

--
-- Alter field action on notificationmessage
--
SET CONSTRAINTS "sentry_notificationm_action_id_e224a327_fk_workflow_" IMMEDIATE; ALTER TABLE "sentry_notificationmessage" DROP CONSTRAINT "sentry_notificationm_action_id_e224a327_fk_workflow_";
ALTER TABLE "sentry_notificationmessage" ALTER COLUMN "action_id" SET DEFAULT 0;
UPDATE "sentry_notificationmessage" SET "action_id" = 0 WHERE "action_id" IS NULL; SET CONSTRAINTS ALL IMMEDIATE;
ALTER TABLE "sentry_notificationmessage" ADD CONSTRAINT "sentry_notificationm_action_id_e224a327_fk_workflow_" FOREIGN KEY ("action_id") REFERENCES "workflow_engine_action" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
ALTER TABLE "sentry_notificationmessage" VALIDATE CONSTRAINT "sentry_notificationm_action_id_e224a327_fk_workflow_";
--
-- Alter field group on notificationmessage
--
SET CONSTRAINTS "sentry_notificationm_group_id_6e588d2d_fk_sentry_gr" IMMEDIATE; ALTER TABLE "sentry_notificationmessage" DROP CONSTRAINT "sentry_notificationm_group_id_6e588d2d_fk_sentry_gr";
ALTER TABLE "sentry_notificationmessage" ALTER COLUMN "group_id" SET DEFAULT 0;
UPDATE "sentry_notificationmessage" SET "group_id" = 0 WHERE "group_id" IS NULL; SET CONSTRAINTS ALL IMMEDIATE;
ALTER TABLE "sentry_notificationmessage" ADD CONSTRAINT "sentry_notificationm_group_id_6e588d2d_fk_sentry_gr" FOREIGN KEY ("group_id") REFERENCES "sentry_groupedmessage" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
ALTER TABLE "sentry_notificationmessage" VALIDATE CONSTRAINT "sentry_notificationm_group_id_6e588d2d_fk_sentry_gr";
--
-- Alter field incident on notificationmessage
--
SET CONSTRAINTS "sentry_notificationm_incident_id_536f94b5_fk_sentry_in" IMMEDIATE; ALTER TABLE "sentry_notificationmessage" DROP CONSTRAINT "sentry_notificationm_incident_id_536f94b5_fk_sentry_in";
--
-- Alter field trigger_action on notificationmessage
--
SET CONSTRAINTS "sentry_notificationm_trigger_action_id_2f8dabba_fk_sentry_al" IMMEDIATE; ALTER TABLE "sentry_notificationmessage" DROP CONSTRAINT "sentry_notificationm_trigger_action_id_2f8dabba_fk_sentry_al";
ALTER TABLE "sentry_notificationmessage" ADD CONSTRAINT "sentry_notificationmessage_action_id_e224a327_notnull" CHECK ("action_id" IS NOT NULL) NOT VALID;
ALTER TABLE "sentry_notificationmessage" VALIDATE CONSTRAINT "sentry_notificationmessage_action_id_e224a327_notnull";
ALTER TABLE "sentry_notificationmessage" ALTER COLUMN "action_id" SET NOT NULL;
ALTER TABLE "sentry_notificationmessage" DROP CONSTRAINT "sentry_notificationmessage_action_id_e224a327_notnull";
ALTER TABLE "sentry_notificationmessage" ADD CONSTRAINT "sentry_notificationmessage_group_id_6e588d2d_notnull" CHECK ("group_id" IS NOT NULL) NOT VALID;
ALTER TABLE "sentry_notificationmessage" VALIDATE CONSTRAINT "sentry_notificationmessage_group_id_6e588d2d_notnull";
ALTER TABLE "sentry_notificationmessage" ALTER COLUMN "group_id" SET NOT NULL;
ALTER TABLE "sentry_notificationmessage" DROP CONSTRAINT "sentry_notificationmessage_group_id_6e588d2d_notnull";

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