We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Clean up TaskReservedResources/task-table at migration to new-tasking-system
See https://bugzilla.redhat.com/show_bug.cgi?id=2031154 for details.

Migration that needs to be updated to purge taskreservedresource entries: 0064_add_new_style_task_columns.py

This wants to be cherrypicked into 3.14/15/16 (after which the offending table no longer exists)

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pulpcore/app/migrations/0064_add_new_style_task_columns.py`
Content:
```
1 # Generated by Django 2.2.20 on 2021-04-27 07:51
2 
3 import django.contrib.postgres.fields
4 import django.contrib.postgres.fields.jsonb
5 from django.db import migrations, models
6 
7 
8 def copy_reserved_resources_record(apps, schema_editor):
9     Task = apps.get_model('core', 'Task')
10     for task in Task.objects.iterator():
11         task._reserved_resources_record = list(task.reserved_resources_record.values_list('resource', flat=True))
12         task.save()
13 
14 
15 def noop(apps, schema_editor):
16     pass
17 
18 
19 class Migration(migrations.Migration):
20 
21     dependencies = [
22         ('core', '0063_repository_retained_versions'),
23     ]
24 
25     operations = [
26         migrations.AddField(
27             model_name='task',
28             name='args',
29             field=django.contrib.postgres.fields.jsonb.JSONField(null=True),
30         ),
31         migrations.AddField(
32             model_name='task',
33             name='kwargs',
34             field=django.contrib.postgres.fields.jsonb.JSONField(null=True),
35         ),
36         migrations.AddField(
37             model_name='task',
38             name='_reserved_resources_record',
39             field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=256), null=True, size=None),
40         ),
41         migrations.AlterField(
42             model_name='task',
43             name='_resource_job_id',
44             field=models.UUIDField(null=True),
45         ),
46         migrations.AlterField(
47             model_name='progressreport',
48             name='state',
49             field=models.TextField(choices=[('waiting', 'Waiting'), ('skipped', 'Skipped'), ('running', 'Running'), ('completed', 'Completed'), ('failed', 'Failed'), ('canceled', 'Canceled'), ('canceling', 'Canceling')], default='waiting'),
50         ),
51         migrations.AlterField(
52             model_name='task',
53             name='state',
54             field=models.TextField(choices=[('waiting', 'Waiting'), ('skipped', 'Skipped'), ('running', 'Running'), ('completed', 'Completed'), ('failed', 'Failed'), ('canceled', 'Canceled'), ('canceling', 'Canceling')]),
55         ),
56         migrations.AddIndex(
57             model_name='task',
58             index=models.Index(fields=['pulp_created'], name='core_task_pulp_cr_10223f_idx'),
59         ),
60         migrations.RunPython(
61             code=copy_reserved_resources_record,
62             reverse_code=noop,
63         ),
64         migrations.RemoveField(
65             model_name='taskreservedresourcerecord',
66             name='resource',
67         ),
68         migrations.RemoveField(
69             model_name='taskreservedresourcerecord',
70             name='task',
71         ),
72         migrations.DeleteModel(
73             name='ReservedResourceRecord',
74         ),
75         migrations.DeleteModel(
76             name='TaskReservedResourceRecord',
77         ),
78         migrations.RenameField(
79             model_name='task',
80             old_name='_reserved_resources_record',
81             new_name='reserved_resources_record',
82         ),
83     ]
84
```
--- 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/pulpcore/app/migrations/0064_add_new_style_task_columns.py b/pulpcore/app/migrations/0064_add_new_style_task_columns.py
--- a/pulpcore/app/migrations/0064_add_new_style_task_columns.py
+++ b/pulpcore/app/migrations/0064_add_new_style_task_columns.py
@@ -4,16 +4,34 @@
 import django.contrib.postgres.fields.jsonb
 from django.db import migrations, models
 
+TASK_BATCH_SIZE = 1000
+
 
 def copy_reserved_resources_record(apps, schema_editor):
     Task = apps.get_model('core', 'Task')
-    for task in Task.objects.iterator():
+
+    # Update _reserved_resource_record for all tasks, 1000 tasks at a time.
+    # When we hit 1K tasks, go to the db for the batch.
+    # Make sure to update the final batch!
+    tasks = []
+    for task in Task.objects.iterator(chunk_size=TASK_BATCH_SIZE):
         task._reserved_resources_record = list(task.reserved_resources_record.values_list('resource', flat=True))
-        task.save()
+        tasks.append(task)
+        if len(tasks) == TASK_BATCH_SIZE:
+            Task.objects.bulk_update(tasks, ["_reserved_resources_record"])
+            tasks.clear()
+
+    # Update last set of tasks
+    if len(tasks) > 0:
+        Task.objects.bulk_update(tasks, ["_reserved_resources_record"])
+
 
+def purge_reservedresources(apps, schema_editor):
+    TaskReservedResource = apps.get_model('core', 'TaskReservedResource')
+    TaskReservedResource.objects.all().delete()
 
-def noop(apps, schema_editor):
-    pass
+    ReservedResource = apps.get_model('core', 'ReservedResource')
+    ReservedResource.objects.all().delete()
 
 
 class Migration(migrations.Migration):
@@ -23,6 +41,12 @@
     ]
 
     operations = [
+        # Purge any ReservedResource entries - if there are any, they're orphans
+        migrations.RunPython(
+            code=purge_reservedresources,
+            reverse_code=migrations.RunPython.noop,
+        ),
+        # Update entities for the new task-system
         migrations.AddField(
             model_name='task',
             name='args',
@@ -59,7 +83,7 @@
         ),
         migrations.RunPython(
             code=copy_reserved_resources_record,
-            reverse_code=noop,
+            reverse_code=migrations.RunPython.noop,
         ),
         migrations.RemoveField(
             model_name='taskreservedresourcerecord',
@@ -80,4 +104,5 @@
             old_name='_reserved_resources_record',
             new_name='reserved_resources_record',
         ),
+
     ]

{"golden_diff": "diff --git a/pulpcore/app/migrations/0064_add_new_style_task_columns.py b/pulpcore/app/migrations/0064_add_new_style_task_columns.py\n--- a/pulpcore/app/migrations/0064_add_new_style_task_columns.py\n+++ b/pulpcore/app/migrations/0064_add_new_style_task_columns.py\n@@ -4,16 +4,34 @@\n import django.contrib.postgres.fields.jsonb\n from django.db import migrations, models\n \n+TASK_BATCH_SIZE = 1000\n+\n \n def copy_reserved_resources_record(apps, schema_editor):\n     Task = apps.get_model('core', 'Task')\n-    for task in Task.objects.iterator():\n+\n+    # Update _reserved_resource_record for all tasks, 1000 tasks at a time.\n+    # When we hit 1K tasks, go to the db for the batch.\n+    # Make sure to update the final batch!\n+    tasks = []\n+    for task in Task.objects.iterator(chunk_size=TASK_BATCH_SIZE):\n         task._reserved_resources_record = list(task.reserved_resources_record.values_list('resource', flat=True))\n-        task.save()\n+        tasks.append(task)\n+        if len(tasks) == TASK_BATCH_SIZE:\n+            Task.objects.bulk_update(tasks, [\"_reserved_resources_record\"])\n+            tasks.clear()\n+\n+    # Update last set of tasks\n+    if len(tasks) > 0:\n+        Task.objects.bulk_update(tasks, [\"_reserved_resources_record\"])\n+\n \n+def purge_reservedresources(apps, schema_editor):\n+    TaskReservedResource = apps.get_model('core', 'TaskReservedResource')\n+    TaskReservedResource.objects.all().delete()\n \n-def noop(apps, schema_editor):\n-    pass\n+    ReservedResource = apps.get_model('core', 'ReservedResource')\n+    ReservedResource.objects.all().delete()\n \n \n class Migration(migrations.Migration):\n@@ -23,6 +41,12 @@\n     ]\n \n     operations = [\n+        # Purge any ReservedResource entries - if there are any, they're orphans\n+        migrations.RunPython(\n+            code=purge_reservedresources,\n+            reverse_code=migrations.RunPython.noop,\n+        ),\n+        # Update entities for the new task-system\n         migrations.AddField(\n             model_name='task',\n             name='args',\n@@ -59,7 +83,7 @@\n         ),\n         migrations.RunPython(\n             code=copy_reserved_resources_record,\n-            reverse_code=noop,\n+            reverse_code=migrations.RunPython.noop,\n         ),\n         migrations.RemoveField(\n             model_name='taskreservedresourcerecord',\n@@ -80,4 +104,5 @@\n             old_name='_reserved_resources_record',\n             new_name='reserved_resources_record',\n         ),\n+\n     ]\n", "issue": "Clean up TaskReservedResources/task-table at migration to new-tasking-system\nSee https://bugzilla.redhat.com/show_bug.cgi?id=2031154 for details.\r\n\r\nMigration that needs to be updated to purge taskreservedresource entries: 0064_add_new_style_task_columns.py\r\n\r\nThis wants to be cherrypicked into 3.14/15/16 (after which the offending table no longer exists)\n", "before_files": [{"content": "# Generated by Django 2.2.20 on 2021-04-27 07:51\n\nimport django.contrib.postgres.fields\nimport django.contrib.postgres.fields.jsonb\nfrom django.db import migrations, models\n\n\ndef copy_reserved_resources_record(apps, schema_editor):\n    Task = apps.get_model('core', 'Task')\n    for task in Task.objects.iterator():\n        task._reserved_resources_record = list(task.reserved_resources_record.values_list('resource', flat=True))\n        task.save()\n\n\ndef noop(apps, schema_editor):\n    pass\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        ('core', '0063_repository_retained_versions'),\n    ]\n\n    operations = [\n        migrations.AddField(\n            model_name='task',\n            name='args',\n            field=django.contrib.postgres.fields.jsonb.JSONField(null=True),\n        ),\n        migrations.AddField(\n            model_name='task',\n            name='kwargs',\n            field=django.contrib.postgres.fields.jsonb.JSONField(null=True),\n        ),\n        migrations.AddField(\n            model_name='task',\n            name='_reserved_resources_record',\n            field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=256), null=True, size=None),\n        ),\n        migrations.AlterField(\n            model_name='task',\n            name='_resource_job_id',\n            field=models.UUIDField(null=True),\n        ),\n        migrations.AlterField(\n            model_name='progressreport',\n            name='state',\n            field=models.TextField(choices=[('waiting', 'Waiting'), ('skipped', 'Skipped'), ('running', 'Running'), ('completed', 'Completed'), ('failed', 'Failed'), ('canceled', 'Canceled'), ('canceling', 'Canceling')], default='waiting'),\n        ),\n        migrations.AlterField(\n            model_name='task',\n            name='state',\n            field=models.TextField(choices=[('waiting', 'Waiting'), ('skipped', 'Skipped'), ('running', 'Running'), ('completed', 'Completed'), ('failed', 'Failed'), ('canceled', 'Canceled'), ('canceling', 'Canceling')]),\n        ),\n        migrations.AddIndex(\n            model_name='task',\n            index=models.Index(fields=['pulp_created'], name='core_task_pulp_cr_10223f_idx'),\n        ),\n        migrations.RunPython(\n            code=copy_reserved_resources_record,\n            reverse_code=noop,\n        ),\n        migrations.RemoveField(\n            model_name='taskreservedresourcerecord',\n            name='resource',\n        ),\n        migrations.RemoveField(\n            model_name='taskreservedresourcerecord',\n            name='task',\n        ),\n        migrations.DeleteModel(\n            name='ReservedResourceRecord',\n        ),\n        migrations.DeleteModel(\n            name='TaskReservedResourceRecord',\n        ),\n        migrations.RenameField(\n            model_name='task',\n            old_name='_reserved_resources_record',\n            new_name='reserved_resources_record',\n        ),\n    ]\n", "path": "pulpcore/app/migrations/0064_add_new_style_task_columns.py"}], "after_files": [{"content": "# Generated by Django 2.2.20 on 2021-04-27 07:51\n\nimport django.contrib.postgres.fields\nimport django.contrib.postgres.fields.jsonb\nfrom django.db import migrations, models\n\nTASK_BATCH_SIZE = 1000\n\n\ndef copy_reserved_resources_record(apps, schema_editor):\n    Task = apps.get_model('core', 'Task')\n\n    # Update _reserved_resource_record for all tasks, 1000 tasks at a time.\n    # When we hit 1K tasks, go to the db for the batch.\n    # Make sure to update the final batch!\n    tasks = []\n    for task in Task.objects.iterator(chunk_size=TASK_BATCH_SIZE):\n        task._reserved_resources_record = list(task.reserved_resources_record.values_list('resource', flat=True))\n        tasks.append(task)\n        if len(tasks) == TASK_BATCH_SIZE:\n            Task.objects.bulk_update(tasks, [\"_reserved_resources_record\"])\n            tasks.clear()\n\n    # Update last set of tasks\n    if len(tasks) > 0:\n        Task.objects.bulk_update(tasks, [\"_reserved_resources_record\"])\n\n\ndef purge_reservedresources(apps, schema_editor):\n    TaskReservedResource = apps.get_model('core', 'TaskReservedResource')\n    TaskReservedResource.objects.all().delete()\n\n    ReservedResource = apps.get_model('core', 'ReservedResource')\n    ReservedResource.objects.all().delete()\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        ('core', '0063_repository_retained_versions'),\n    ]\n\n    operations = [\n        # Purge any ReservedResource entries - if there are any, they're orphans\n        migrations.RunPython(\n            code=purge_reservedresources,\n            reverse_code=migrations.RunPython.noop,\n        ),\n        # Update entities for the new task-system\n        migrations.AddField(\n            model_name='task',\n            name='args',\n            field=django.contrib.postgres.fields.jsonb.JSONField(null=True),\n        ),\n        migrations.AddField(\n            model_name='task',\n            name='kwargs',\n            field=django.contrib.postgres.fields.jsonb.JSONField(null=True),\n        ),\n        migrations.AddField(\n            model_name='task',\n            name='_reserved_resources_record',\n            field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=256), null=True, size=None),\n        ),\n        migrations.AlterField(\n            model_name='task',\n            name='_resource_job_id',\n            field=models.UUIDField(null=True),\n        ),\n        migrations.AlterField(\n            model_name='progressreport',\n            name='state',\n            field=models.TextField(choices=[('waiting', 'Waiting'), ('skipped', 'Skipped'), ('running', 'Running'), ('completed', 'Completed'), ('failed', 'Failed'), ('canceled', 'Canceled'), ('canceling', 'Canceling')], default='waiting'),\n        ),\n        migrations.AlterField(\n            model_name='task',\n            name='state',\n            field=models.TextField(choices=[('waiting', 'Waiting'), ('skipped', 'Skipped'), ('running', 'Running'), ('completed', 'Completed'), ('failed', 'Failed'), ('canceled', 'Canceled'), ('canceling', 'Canceling')]),\n        ),\n        migrations.AddIndex(\n            model_name='task',\n            index=models.Index(fields=['pulp_created'], name='core_task_pulp_cr_10223f_idx'),\n        ),\n        migrations.RunPython(\n            code=copy_reserved_resources_record,\n            reverse_code=migrations.RunPython.noop,\n        ),\n        migrations.RemoveField(\n            model_name='taskreservedresourcerecord',\n            name='resource',\n        ),\n        migrations.RemoveField(\n            model_name='taskreservedresourcerecord',\n            name='task',\n        ),\n        migrations.DeleteModel(\n            name='ReservedResourceRecord',\n        ),\n        migrations.DeleteModel(\n            name='TaskReservedResourceRecord',\n        ),\n        migrations.RenameField(\n            model_name='task',\n            old_name='_reserved_resources_record',\n            new_name='reserved_resources_record',\n        ),\n\n    ]\n", "path": "pulpcore/app/migrations/0064_add_new_style_task_columns.py"}]}