Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion migrations_lockfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ releases: 0002_delete_dual_written_commit_tables

replays: 0006_add_bulk_delete_job

sentry: 1001_prevent_grouphistory_infinte_recursion
sentry: 1002_group_history_prev_history_remove_db_constraint_and_safe_removal

social_auth: 0003_social_auth_json_field

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Generated by Django 5.2.1 on 2025-11-04 13:30

import django.db.models.deletion
from django.db import migrations

from sentry.db.models.fields.foreignkey import FlexibleForeignKey
from sentry.new_migrations.migrations import CheckedMigration
from sentry.new_migrations.monkey.fields import SafeRemoveField
from sentry.new_migrations.monkey.state import DeletionAction


class Migration(CheckedMigration):
# This flag is used to mark that a migration shouldn't be automatically run in production.
# This should only be used for operations where it's safe to run the migration after your
# code has deployed. So this should not be used for most operations that alter the schema
# of a table.
# Here are some things that make sense to mark as post deployment:
# - Large data migrations. Typically we want these to be run manually so that they can be
# monitored and not block the deploy for a long period of time while they run.
# - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
# run this outside deployments so that we don't block them. Note that while adding an index
# is a schema change, it's completely safe to run the operation after the code has deployed.
# Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment

is_post_deployment = False

dependencies = [
("sentry", "1001_prevent_grouphistory_infinte_recursion"),
]

operations = [
migrations.AlterField(
model_name="grouphistory",
name="prev_history",
field=FlexibleForeignKey(
# We want to remove the database-level foreign key constraint
db_constraint=False,
Copy link
Member Author

@armenzg armenzg Nov 4, 2025

Choose a reason for hiding this comment

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

This causes integrity errors like this: https://sentry.sentry.io/issues/6994851757/

null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="sentry.grouphistory",
),
),
# This marks the field as pending deletion
SafeRemoveField(
model_name="grouphistory",
name="prev_history",
deletion_action=DeletionAction.MOVE_TO_PENDING,
Copy link
Member Author

Choose a reason for hiding this comment

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

This simply marks the column as not useable.

),
]
14 changes: 12 additions & 2 deletions src/sentry/models/grouphistory.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,18 @@ class GroupHistory(Model):
),
)
prev_history = FlexibleForeignKey(
"sentry.GroupHistory", null=True, on_delete=models.SET_NULL
) # This field has no immediate use, but might be useful.
"sentry.GroupHistory",
null=True,
on_delete=models.SET_NULL,
# By default, Django creates a database-level foreign key constraint when you define a ForeignKey field.
# This constraint enforces referential integrity at the database level, preventing you from:
# * Deleting a referenced record without handling the reference
# * Creating a reference to a non-existent record
# When you set db_constraint=False, Django skips creating this database constraint.
# The field still works as a ForeignKey in Python/Django (you can traverse relationships,
# use .select_related(), etc.), but the database won't enforce the relationship.
db_constraint=False,
)
prev_history_date = models.DateTimeField(
null=True
) # This field is used to simplify query calculations.
Comment on lines 228 to 230
Copy link

Choose a reason for hiding this comment

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

Bug: Removing prev_history from GroupHistory model before its SafeRemoveField migration runs causes a FieldDoesNotExist error during migration execution.
Severity: CRITICAL | Confidence: 1.00

🔍 Detailed Analysis

The SafeRemoveField.state_forwards() method, used in the migration, expects the prev_history field to exist in the GroupHistory model definition when the migration runs. Removing prev_history from src/sentry/models/grouphistory.py at lines 215-217 in the same change as the migration causes state.apps.get_model(...)._meta.get_field('prev_history') to raise a FieldDoesNotExist error, leading to a migration crash.

💡 Suggested Fix

Retain the prev_history field definition in src/sentry/models/grouphistory.py in this PR. Remove the field definition in a separate, subsequent PR after the migration has successfully deployed and executed in production.

🤖 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/models/grouphistory.py#L215-L217

Potential issue: The `SafeRemoveField.state_forwards()` method, used in the migration,
expects the `prev_history` field to exist in the `GroupHistory` model definition when
the migration runs. Removing `prev_history` from `src/sentry/models/grouphistory.py` at
lines 215-217 in the same change as the migration causes
`state.apps.get_model(...)._meta.get_field('prev_history')` to raise a
`FieldDoesNotExist` error, leading to a migration crash.

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

Expand Down
Loading