We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Event migration 9.1.2 -> 10
<!--

Do you want to ask a question? Are you looking for support? The Sentry message
board is the best place for getting support: https://forum.sentry.io
-->

## Important Details

How are you running Sentry?

* [X] On-Premise docker [Version 9.1.2]
* [ ] Saas (sentry.io)
* [ ] Other [briefly describe your environment]

## Description

I followed the migration guide, alongside all fixes and workaround and managed to get to the actual migration routine. Sentry tries to process all existing postgres events but fails to (for every event):

```
An error occured while trying to instert the following event: <sentry.eventstore.models.Event object at 0x7f2f08e552d0>
.----
insert() takes at least 8 arguments (8 given)
[...]
Event migration done. Migrated 0 of 197988 events.
```

## Steps to Reproduce

1. Have a 9.1.2 onpremise setup and have event data
2. Upgrade to 10 (dev-master), run `install.sh` etc.

### What you expected to happen

Migration scripts succeeds and I have all event data in the new version.

### Possible Solution

Error message suggests a syntax error?


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/sentry/migrations/0024_auto_20191230_2052.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 # Generated by Django 1.9.13 on 2019-12-30 20:52
3 from __future__ import unicode_literals, print_function
4 
5 import os
6 import types
7 from datetime import timedelta, datetime
8 
9 from django.db import migrations
10 from django.utils import timezone
11 
12 from sentry import options
13 from sentry.eventstore.models import Event as NewEvent
14 
15 
16 def backfill_eventstream(apps, schema_editor):
17     """
18     Inserts Postgres events into the eventstream if there are recent events in Postgres.
19 
20     This is for open source users migrating from 9.x who want to keep their events.
21     If there are no recent events in Postgres, skip the backfill.
22     """
23     from sentry import eventstore, eventstream
24     from sentry.utils.query import RangeQuerySetWrapper
25 
26     Event = apps.get_model("sentry", "Event")
27     Group = apps.get_model("sentry", "Group")
28     Project = apps.get_model("sentry", "Project")
29 
30     # Kill switch to skip this migration
31     skip_backfill = os.environ.get("SENTRY_SKIP_EVENTS_BACKFILL_FOR_10", False)
32 
33     # Use 90 day retention if the option has not been set or set to 0
34     DEFAULT_RETENTION = 90
35     retention_days = options.get("system.event-retention-days") or DEFAULT_RETENTION
36 
37     def get_events(last_days):
38         to_date = timezone.now()
39         from_date = to_date - timedelta(days=last_days)
40         return Event.objects.filter(
41             datetime__gte=from_date, datetime__lte=to_date, group_id__isnull=False
42         )
43 
44     def _attach_related(_events):
45         project_ids = set()
46         group_ids = set()
47         for event in _events:
48             project_ids.add(event.project_id)
49             group_ids.add(event.group_id)
50         projects = {p.id: p for p in Project.objects.filter(id__in=project_ids)}
51         groups = {g.id: g for g in Group.objects.filter(id__in=group_ids)}
52 
53         for event in _events:
54             event.project = projects.get(event.project_id)
55             event.group = groups.get(event.group_id)
56         eventstore.bind_nodes(_events, "data")
57 
58     if skip_backfill:
59         print("Skipping backfill.\n")
60         return
61 
62     events = get_events(retention_days)
63     count = events.count()
64 
65     if count == 0:
66         print("Nothing to do, skipping migration.\n")
67         return
68 
69     print("Events to process: {}\n".format(count))
70 
71     processed = 0
72     for e in RangeQuerySetWrapper(events, step=100, callbacks=(_attach_related,)):
73         event = NewEvent(
74             project_id=e.project_id, event_id=e.event_id, group_id=e.group_id, data=e.data.data
75         )
76         primary_hash = event.get_primary_hash()
77         if event.project is None or event.group is None:
78             print("Skipped {} as group or project information is invalid.\n".format(event))
79             continue
80 
81         try:
82             eventstream.insert(
83                 group=event.group,
84                 event=event,
85                 is_new=False,
86                 is_regression=False,
87                 is_new_group_environment=False,
88                 primary_hash=primary_hash,
89                 skip_consume=True,
90             )
91             processed += 1
92         except Exception as error:
93             print(
94                 "An error occured while trying to instert the following event: {}\n.----\n{}".format(
95                     event, error
96                 )
97             )
98 
99     print("Event migration done. Migrated {} of {} events.\n".format(processed, count))
100 
101 
102 class Migration(migrations.Migration):
103     # This flag is used to mark that a migration shouldn't be automatically run in
104     # production. We set this to True for operations that we think are risky and want
105     # someone from ops to run manually and monitor.
106     # General advice is that if in doubt, mark your migration as `is_dangerous`.
107     # Some things you should always mark as dangerous:
108     # - Adding indexes to large tables. These indexes should be created concurrently,
109     #   unfortunately we can't run migrations outside of a transaction until Django
110     #   1.10. So until then these should be run manually.
111     # - Large data migrations. Typically we want these to be run manually by ops so that
112     #   they can be monitored. Since data migrations will now hold a transaction open
113     #   this is even more important.
114     # - Adding columns to highly active tables, even ones that are NULL.
115     is_dangerous = True
116 
117     dependencies = [
118         ("sentry", "0023_hide_environment_none_20191126"),
119     ]
120 
121     operations = [
122         migrations.RunPython(backfill_eventstream, reverse_code=migrations.RunPython.noop),
123     ]
124
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/sentry/migrations/0024_auto_20191230_2052.py b/src/sentry/migrations/0024_auto_20191230_2052.py
--- a/src/sentry/migrations/0024_auto_20191230_2052.py
+++ b/src/sentry/migrations/0024_auto_20191230_2052.py
@@ -86,6 +86,8 @@
                 is_regression=False,
                 is_new_group_environment=False,
                 primary_hash=primary_hash,
+                received_timestamp=event.data.get("received")
+                or float(event.datetime.strftime("%s")),
                 skip_consume=True,
             )
             processed += 1

{"golden_diff": "diff --git a/src/sentry/migrations/0024_auto_20191230_2052.py b/src/sentry/migrations/0024_auto_20191230_2052.py\n--- a/src/sentry/migrations/0024_auto_20191230_2052.py\n+++ b/src/sentry/migrations/0024_auto_20191230_2052.py\n@@ -86,6 +86,8 @@\n                 is_regression=False,\n                 is_new_group_environment=False,\n                 primary_hash=primary_hash,\n+                received_timestamp=event.data.get(\"received\")\n+                or float(event.datetime.strftime(\"%s\")),\n                 skip_consume=True,\n             )\n             processed += 1\n", "issue": "Event migration 9.1.2 -> 10\n<!--\r\n\r\nDo you want to ask a question? Are you looking for support? The Sentry message\r\nboard is the best place for getting support: https://forum.sentry.io\r\n-->\r\n\r\n## Important Details\r\n\r\nHow are you running Sentry?\r\n\r\n* [X] On-Premise docker [Version 9.1.2]\r\n* [ ] Saas (sentry.io)\r\n* [ ] Other [briefly describe your environment]\r\n\r\n## Description\r\n\r\nI followed the migration guide, alongside all fixes and workaround and managed to get to the actual migration routine. Sentry tries to process all existing postgres events but fails to (for every event):\r\n\r\n```\r\nAn error occured while trying to instert the following event: <sentry.eventstore.models.Event object at 0x7f2f08e552d0>\r\n.----\r\ninsert() takes at least 8 arguments (8 given)\r\n[...]\r\nEvent migration done. Migrated 0 of 197988 events.\r\n```\r\n\r\n## Steps to Reproduce\r\n\r\n1. Have a 9.1.2 onpremise setup and have event data\r\n2. Upgrade to 10 (dev-master), run `install.sh` etc.\r\n\r\n### What you expected to happen\r\n\r\nMigration scripts succeeds and I have all event data in the new version.\r\n\r\n### Possible Solution\r\n\r\nError message suggests a syntax error?\r\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n# Generated by Django 1.9.13 on 2019-12-30 20:52\nfrom __future__ import unicode_literals, print_function\n\nimport os\nimport types\nfrom datetime import timedelta, datetime\n\nfrom django.db import migrations\nfrom django.utils import timezone\n\nfrom sentry import options\nfrom sentry.eventstore.models import Event as NewEvent\n\n\ndef backfill_eventstream(apps, schema_editor):\n    \"\"\"\n    Inserts Postgres events into the eventstream if there are recent events in Postgres.\n\n    This is for open source users migrating from 9.x who want to keep their events.\n    If there are no recent events in Postgres, skip the backfill.\n    \"\"\"\n    from sentry import eventstore, eventstream\n    from sentry.utils.query import RangeQuerySetWrapper\n\n    Event = apps.get_model(\"sentry\", \"Event\")\n    Group = apps.get_model(\"sentry\", \"Group\")\n    Project = apps.get_model(\"sentry\", \"Project\")\n\n    # Kill switch to skip this migration\n    skip_backfill = os.environ.get(\"SENTRY_SKIP_EVENTS_BACKFILL_FOR_10\", False)\n\n    # Use 90 day retention if the option has not been set or set to 0\n    DEFAULT_RETENTION = 90\n    retention_days = options.get(\"system.event-retention-days\") or DEFAULT_RETENTION\n\n    def get_events(last_days):\n        to_date = timezone.now()\n        from_date = to_date - timedelta(days=last_days)\n        return Event.objects.filter(\n            datetime__gte=from_date, datetime__lte=to_date, group_id__isnull=False\n        )\n\n    def _attach_related(_events):\n        project_ids = set()\n        group_ids = set()\n        for event in _events:\n            project_ids.add(event.project_id)\n            group_ids.add(event.group_id)\n        projects = {p.id: p for p in Project.objects.filter(id__in=project_ids)}\n        groups = {g.id: g for g in Group.objects.filter(id__in=group_ids)}\n\n        for event in _events:\n            event.project = projects.get(event.project_id)\n            event.group = groups.get(event.group_id)\n        eventstore.bind_nodes(_events, \"data\")\n\n    if skip_backfill:\n        print(\"Skipping backfill.\\n\")\n        return\n\n    events = get_events(retention_days)\n    count = events.count()\n\n    if count == 0:\n        print(\"Nothing to do, skipping migration.\\n\")\n        return\n\n    print(\"Events to process: {}\\n\".format(count))\n\n    processed = 0\n    for e in RangeQuerySetWrapper(events, step=100, callbacks=(_attach_related,)):\n        event = NewEvent(\n            project_id=e.project_id, event_id=e.event_id, group_id=e.group_id, data=e.data.data\n        )\n        primary_hash = event.get_primary_hash()\n        if event.project is None or event.group is None:\n            print(\"Skipped {} as group or project information is invalid.\\n\".format(event))\n            continue\n\n        try:\n            eventstream.insert(\n                group=event.group,\n                event=event,\n                is_new=False,\n                is_regression=False,\n                is_new_group_environment=False,\n                primary_hash=primary_hash,\n                skip_consume=True,\n            )\n            processed += 1\n        except Exception as error:\n            print(\n                \"An error occured while trying to instert the following event: {}\\n.----\\n{}\".format(\n                    event, error\n                )\n            )\n\n    print(\"Event migration done. Migrated {} of {} events.\\n\".format(processed, count))\n\n\nclass Migration(migrations.Migration):\n    # This flag is used to mark that a migration shouldn't be automatically run in\n    # production. We set this to True for operations that we think are risky and want\n    # someone from ops to run manually and monitor.\n    # General advice is that if in doubt, mark your migration as `is_dangerous`.\n    # Some things you should always mark as dangerous:\n    # - Adding indexes to large tables. These indexes should be created concurrently,\n    #   unfortunately we can't run migrations outside of a transaction until Django\n    #   1.10. So until then these should be run manually.\n    # - Large data migrations. Typically we want these to be run manually by ops so that\n    #   they can be monitored. Since data migrations will now hold a transaction open\n    #   this is even more important.\n    # - Adding columns to highly active tables, even ones that are NULL.\n    is_dangerous = True\n\n    dependencies = [\n        (\"sentry\", \"0023_hide_environment_none_20191126\"),\n    ]\n\n    operations = [\n        migrations.RunPython(backfill_eventstream, reverse_code=migrations.RunPython.noop),\n    ]\n", "path": "src/sentry/migrations/0024_auto_20191230_2052.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n# Generated by Django 1.9.13 on 2019-12-30 20:52\nfrom __future__ import unicode_literals, print_function\n\nimport os\nimport types\nfrom datetime import timedelta, datetime\n\nfrom django.db import migrations\nfrom django.utils import timezone\n\nfrom sentry import options\nfrom sentry.eventstore.models import Event as NewEvent\n\n\ndef backfill_eventstream(apps, schema_editor):\n    \"\"\"\n    Inserts Postgres events into the eventstream if there are recent events in Postgres.\n\n    This is for open source users migrating from 9.x who want to keep their events.\n    If there are no recent events in Postgres, skip the backfill.\n    \"\"\"\n    from sentry import eventstore, eventstream\n    from sentry.utils.query import RangeQuerySetWrapper\n\n    Event = apps.get_model(\"sentry\", \"Event\")\n    Group = apps.get_model(\"sentry\", \"Group\")\n    Project = apps.get_model(\"sentry\", \"Project\")\n\n    # Kill switch to skip this migration\n    skip_backfill = os.environ.get(\"SENTRY_SKIP_EVENTS_BACKFILL_FOR_10\", False)\n\n    # Use 90 day retention if the option has not been set or set to 0\n    DEFAULT_RETENTION = 90\n    retention_days = options.get(\"system.event-retention-days\") or DEFAULT_RETENTION\n\n    def get_events(last_days):\n        to_date = timezone.now()\n        from_date = to_date - timedelta(days=last_days)\n        return Event.objects.filter(\n            datetime__gte=from_date, datetime__lte=to_date, group_id__isnull=False\n        )\n\n    def _attach_related(_events):\n        project_ids = set()\n        group_ids = set()\n        for event in _events:\n            project_ids.add(event.project_id)\n            group_ids.add(event.group_id)\n        projects = {p.id: p for p in Project.objects.filter(id__in=project_ids)}\n        groups = {g.id: g for g in Group.objects.filter(id__in=group_ids)}\n\n        for event in _events:\n            event.project = projects.get(event.project_id)\n            event.group = groups.get(event.group_id)\n        eventstore.bind_nodes(_events, \"data\")\n\n    if skip_backfill:\n        print(\"Skipping backfill.\\n\")\n        return\n\n    events = get_events(retention_days)\n    count = events.count()\n\n    if count == 0:\n        print(\"Nothing to do, skipping migration.\\n\")\n        return\n\n    print(\"Events to process: {}\\n\".format(count))\n\n    processed = 0\n    for e in RangeQuerySetWrapper(events, step=100, callbacks=(_attach_related,)):\n        event = NewEvent(\n            project_id=e.project_id, event_id=e.event_id, group_id=e.group_id, data=e.data.data\n        )\n        primary_hash = event.get_primary_hash()\n        if event.project is None or event.group is None:\n            print(\"Skipped {} as group or project information is invalid.\\n\".format(event))\n            continue\n\n        try:\n            eventstream.insert(\n                group=event.group,\n                event=event,\n                is_new=False,\n                is_regression=False,\n                is_new_group_environment=False,\n                primary_hash=primary_hash,\n                received_timestamp=event.data.get(\"received\")\n                or float(event.datetime.strftime(\"%s\")),\n                skip_consume=True,\n            )\n            processed += 1\n        except Exception as error:\n            print(\n                \"An error occured while trying to instert the following event: {}\\n.----\\n{}\".format(\n                    event, error\n                )\n            )\n\n    print(\"Event migration done. Migrated {} of {} events.\\n\".format(processed, count))\n\n\nclass Migration(migrations.Migration):\n    # This flag is used to mark that a migration shouldn't be automatically run in\n    # production. We set this to True for operations that we think are risky and want\n    # someone from ops to run manually and monitor.\n    # General advice is that if in doubt, mark your migration as `is_dangerous`.\n    # Some things you should always mark as dangerous:\n    # - Adding indexes to large tables. These indexes should be created concurrently,\n    #   unfortunately we can't run migrations outside of a transaction until Django\n    #   1.10. So until then these should be run manually.\n    # - Large data migrations. Typically we want these to be run manually by ops so that\n    #   they can be monitored. Since data migrations will now hold a transaction open\n    #   this is even more important.\n    # - Adding columns to highly active tables, even ones that are NULL.\n    is_dangerous = True\n\n    dependencies = [\n        (\"sentry\", \"0023_hide_environment_none_20191126\"),\n    ]\n\n    operations = [\n        migrations.RunPython(backfill_eventstream, reverse_code=migrations.RunPython.noop),\n    ]\n", "path": "src/sentry/migrations/0024_auto_20191230_2052.py"}]}