chore(alerts): Remove metric alert columns on NotificationMessage#115578
chore(alerts): Remove metric alert columns on NotificationMessage#115578ceorourke wants to merge 1 commit into
Conversation
| 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) |
There was a problem hiding this comment.
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.
| ), | ||
| ), | ||
| 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", | ||
| ), | ||
| ), |
There was a problem hiding this comment.
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.
wedamija
left a comment
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ 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", | ||
| ), | ||
| ), |
There was a problem hiding this comment.
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)
Reviewed by Cursor Bugbot for commit eea68d7. Configure here.
|
This PR has a migration; here is the generated SQL for for --
-- 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"; |


Follow up to #115529 to start removing the
incidentandtrigger_actioncolumn onNotifcationMessage. Since onlyactionandgroupwill be used now, I'm trying to make those not nullable.