Skip to content

Commit 827ca97

Browse files
armenzgpriscilawebdev
authored andcommitted
chore(group_history): Remove db_constraint for prev_history (#102672)
This is part of the required steps to remove a column from Sentry: https://develop.sentry.dev/backend/application-domains/database-migrations/#deleting-columns. This is a follow-up to #102474. This fixes [SENTRY-5BX8](https://sentry.sentry.io/issues/6994851757/) and [SENTRY-5BXW](https://sentry.sentry.io/issues/6995699817/) which are happening since we switched to the bulk deletion model (#102580).
1 parent 068ef2e commit 827ca97

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

migrations_lockfile.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ releases: 0002_delete_dual_written_commit_tables
3131

3232
replays: 0006_add_bulk_delete_job
3333

34-
sentry: 1001_prevent_grouphistory_infinte_recursion
34+
sentry: 1002_group_history_prev_history_remove_db_constraint
3535

3636
social_auth: 0003_social_auth_json_field
3737

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Generated by Django 5.2.1 on 2025-11-04 15:02
2+
3+
import django.db.models.deletion
4+
from django.db import migrations
5+
6+
import sentry.db.models.fields.foreignkey
7+
from sentry.new_migrations.migrations import CheckedMigration
8+
9+
10+
class Migration(CheckedMigration):
11+
# This flag is used to mark that a migration shouldn't be automatically run in production.
12+
# This should only be used for operations where it's safe to run the migration after your
13+
# code has deployed. So this should not be used for most operations that alter the schema
14+
# of a table.
15+
# Here are some things that make sense to mark as post deployment:
16+
# - Large data migrations. Typically we want these to be run manually so that they can be
17+
# monitored and not block the deploy for a long period of time while they run.
18+
# - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
19+
# run this outside deployments so that we don't block them. Note that while adding an index
20+
# is a schema change, it's completely safe to run the operation after the code has deployed.
21+
# Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment
22+
23+
is_post_deployment = False
24+
25+
dependencies = [
26+
("sentry", "1001_prevent_grouphistory_infinte_recursion"),
27+
]
28+
29+
operations = [
30+
migrations.AlterField(
31+
model_name="grouphistory",
32+
name="prev_history",
33+
field=sentry.db.models.fields.foreignkey.FlexibleForeignKey(
34+
db_constraint=False,
35+
null=True,
36+
on_delete=django.db.models.deletion.SET_NULL,
37+
to="sentry.grouphistory",
38+
),
39+
),
40+
]

src/sentry/models/grouphistory.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,18 @@ class GroupHistory(Model):
213213
),
214214
)
215215
prev_history = FlexibleForeignKey(
216-
"sentry.GroupHistory", null=True, on_delete=models.SET_NULL
217-
) # This field has no immediate use, but might be useful.
216+
"sentry.GroupHistory",
217+
null=True,
218+
on_delete=models.SET_NULL,
219+
# By default, Django creates a database-level foreign key constraint when you define a ForeignKey field.
220+
# This constraint enforces referential integrity at the database level, preventing you from:
221+
# * Deleting a referenced record without handling the reference
222+
# * Creating a reference to a non-existent record
223+
# When you set db_constraint=False, Django skips creating this database constraint.
224+
# The field still works as a ForeignKey in Python/Django (you can traverse relationships,
225+
# use .select_related(), etc.), but the database won't enforce the relationship.
226+
db_constraint=False,
227+
)
218228
prev_history_date = models.DateTimeField(
219229
null=True
220230
) # This field is used to simplify query calculations.

0 commit comments

Comments
 (0)