Swap FK event<->issue

This commit is contained in:
Klaas van Schelven
2024-01-05 22:38:59 +01:00
parent 164ed8dfd5
commit 94661b4bb8
7 changed files with 88 additions and 13 deletions
+17
View File
@@ -0,0 +1,17 @@
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('events', '0004_event_server_side_timestamp'),
]
operations = [
migrations.AddField(
model_name='event',
name='issue',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='issues.issue'),
),
]
@@ -0,0 +1,25 @@
from django.db import migrations
def set_event_issue(apps, schema_editor):
# this was never actually run successfully (the code that ran was a different version), but it's not needed anymore
# anyway :-) kept for laughs and giggles
Issue = apps.get_model('issues', 'Issue')
for issue in Issue.objects.all():
for event in issue.events.all():
event.issue = issue
event.save()
class Migration(migrations.Migration):
dependencies = [
('events', '0005_event_issue'),
('issues', '0006_issue_is_muted_and_more'),
]
operations = [
migrations.RunPython(set_event_issue),
]
@@ -0,0 +1,18 @@
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('issues', '0007_remove_issue_events'),
('events', '0006_auto_20240105_1954'),
]
operations = [
migrations.AlterField(
model_name='event',
name='issue',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='events', to='issues.issue'),
),
]
+4 -2
View File
@@ -49,6 +49,9 @@ class Event(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) # This ID is internal to bugsink
server_side_timestamp = models.DateTimeField(db_index=True, blank=False, null=False)
# not actually expected to be null, but we want to be able to delete issues without deleting events (cleanup later)
issue = models.ForeignKey("issues.Issue", blank=False, null=True, on_delete=models.SET_NULL)
# > Required. Hexadecimal string representing a uuid4 value. The length is exactly 32 characters. Dashes are not
# > allowed. Has to be lowercase.
# Not a primary key: events may be duplicated across projects
@@ -135,8 +138,7 @@ class Event(models.Model):
# index_together = (("group_id", "datetime"),) TODO seriously think about indexes
def get_absolute_url(self):
issue = self.issue_set.get()
return f"/issues/issue/{ issue.id }/event/{ self.id }/"
return f"/issues/issue/{ self.issue_id }/event/{ self.id }/"
def get_raw_link(self):
# for the admin
+9 -8
View File
@@ -72,19 +72,20 @@ class BaseIngestAPIView(APIView):
debug_info = request.META.get("HTTP_X_BUGSINK_DEBUGINFO", "")
event, event_created = Event.from_json(project, event_data, now, debug_info)
if not event_created:
return
create_release_if_needed(project, event.release)
hash_ = get_hash_for_data(event_data)
issue, issue_created = Issue.objects.get_or_create(
project=project,
hash=hash_,
)
issue.events.add(event)
event, event_created = Event.from_json(project, event_data, issue, now, debug_info)
if not event_created:
# note: previously we created the event before the issue, which allowed for one less query. I don't see
# straight away how we can reproduce that now that we create issue-before-event (since creating the issue
# first is needed to be able to set the FK in one go)
return
create_release_if_needed(project, event.release)
issue_pc = issue_period_counters[issue.id]
issue_pc.inc(now)
@@ -0,0 +1,15 @@
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('issues', '0006_issue_is_muted_and_more'),
]
operations = [
migrations.RemoveField(
model_name='issue',
name='events',
),
]
-3
View File
@@ -11,9 +11,6 @@ class Issue(models.Model):
"projects.Project", blank=False, null=True, on_delete=models.SET_NULL) # SET_NULL: cleanup 'later'
hash = models.CharField(max_length=32, blank=False, null=False)
# TODO: we should get rid of an M2M table by making this a FK from the other side
events = models.ManyToManyField("events.Event")
# fields related to resolution:
# what does this mean for the release-based use cases? it means what you filter on.
# it also simply means: it was "marked as resolved" after the last regression (if any)