Files
bugsink/issues/migrations/0020_remove_objects_with_null_issue.py
Klaas van Schelven e45c61d6f0 Various models: .issue and .grouping; SET_NULL => DO_NOTHING
I originally thought `SET_NULL` would be a good way to "do stuff later", but
that's only so the degree that [1] updates are cheaper than deletes and [2]
2nd-order effects (further deletes in the dep-tree) are avoided.

Now that we have explicit Issue-deletion (deps-first, delayed, properly batched)
the SET_NULL behavior is always a no-op (but with cost in queries).

As a result, in the test for issue deletion (which has deletes for many
of the altered models), the following 8 queries are no longer done:

```
SELECT "issues_grouping"."id", [..many fields..] FROM "issues_grouping" WHERE "issues_grouping"."id" IN (1)
UPDATE "events_event" SET "grouping_id" = NULL WHERE "events_event"."grouping_id" IN (1)

[.. a few moments later..]

SELECT "issues_issue"."id", [..many fields..] FROM "issues_issue" WHERE "issues_issue"."id" = 'uuid'
UPDATE "issues_grouping" SET "issue_id" = NULL WHERE "issues_grouping"."issue_id" IN ('uuid')
UPDATE "issues_turningpoint" SET "issue_id" = NULL WHERE "issues_turningpoint"."issue_id" IN ('uuid')
UPDATE "events_event" SET "issue_id" = NULL WHERE "events_event"."issue_id" IN ('uuid')
UPDATE "tags_eventtag" SET "issue_id" = NULL WHERE "tags_eventtag"."issue_id" IN ('uuid')
UPDATE "tags_issuetag" SET "issue_id" = NULL WHERE "tags_issuetag"."issue_id" IN ('uuid')
```

(breaks the tests b/c of constraints and not always using factories; will fix next)
2025-07-03 11:33:58 +02:00

27 lines
1.0 KiB
Python

from django.db import migrations
def remove_objects_with_null_issue(apps, schema_editor):
# Up until now, we have various models w/ .issue=FK(null=True, on_delete=models.SET_NULL)
# Although it is "not expected" in the interface, issue-deletion would have led to those
# objects with a null issue. We're about to change that to .issue=FK(null=False, ...) which
# would crash if we don't remove those objects first. Object-removal is "fine" though, because
# as per the meaning of the SET_NULL, these objects were "dangling" anyway.
Grouping = apps.get_model("issues", "Grouping")
TurningPoint = apps.get_model("issues", "TurningPoint")
Grouping.objects.filter(issue__isnull=True).delete()
TurningPoint.objects.filter(issue__isnull=True).delete()
class Migration(migrations.Migration):
dependencies = [
("issues", "0019_alter_grouping_grouping_key_hash"),
]
operations = [
migrations.RunPython(remove_objects_with_null_issue, reverse_code=migrations.RunPython.noop),
]