We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Need a way to directly migrate pulp_labels from 3.21 to 3.28
**Is your feature request related to a problem? Please describe.**
As RHUI developers, we are heavy users of pulp_labels on repository objects.  As the current 4.5 release is on pulpcore 3.21, we would like our next release (4.6) to be based on pulpcore 3.28, and we need to ensure a smooth upgrade path for our customers.  As it is currently laid out, the 3.21 to 3.28 migration path requires an intermediate step (<3.25) where `pulpcore-manager datarepair-labels` command is available.

**Describe the solution you'd like**
The ideal solution would be to make Django migrations take care of that automatically, without requiring an intermediate step/release.  Since we live in non-ideal world, a solution where an additional command like `datarepair-labels` is available would also suffice.

**Describe alternatives you've considered**
We have considered an intermediate release of RHUI 4.6 with pulpcore 3.22 followed by RHUI 4.7 built on 3.28, but it would create a lot of migration issues for our customers, with many of them still running RHUI 4.0.



--- 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/0104_delete_label.py`
Content:
```
1 # Generated by Django 3.2.18 on 2023-05-04 07:06
2 
3 from django.db import migrations
4 
5 
6 def check_no_existing_labels(apps, schema_editor):
7     Label = apps.get_model("core", "Label")
8     if Label.objects.exists():
9         raise RuntimeError(
10             "There are remaining labels. Please revert to pulpcore<3.25 and make sure all labels are properly mirgated or deleted."
11         )
12 
13 
14 class Migration(migrations.Migration):
15 
16     dependencies = [
17         ('core', '0103_alter_export_task'),
18     ]
19 
20     operations = [
21         migrations.RunPython(
22             code=check_no_existing_labels,
23             reverse_code=migrations.RunPython.noop,
24             elidable=True,
25         ),
26         migrations.DeleteModel(
27             name='Label',
28         ),
29     ]
30
```
--- 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/0104_delete_label.py b/pulpcore/app/migrations/0104_delete_label.py
--- a/pulpcore/app/migrations/0104_delete_label.py
+++ b/pulpcore/app/migrations/0104_delete_label.py
@@ -1,6 +1,45 @@
 # Generated by Django 3.2.18 on 2023-05-04 07:06
 
 from django.db import migrations
+from django.db.models.expressions import OuterRef, RawSQL
+
+
+def migrate_remaining_labels(apps, schema_editor):
+    """
+    This data migration handles the "but what about plugins" problem noted in the issue [0], with only two caveats:
+
+    Case 1: If there were to exist a plugin containing a Model whose model-name ended in (for example) "Repository",
+    that was NOT a detail-model of a Repository master-model, AND that plugin allowed Labels for such a model - then,
+    upon running this migration, those Labels would be lost.
+
+    Case 2: If there were to exist a plugin containing a Model that was a Detail of (for example) Repository,
+    but named something like "PluginRepositoryButWhy", and that plugin allowed Labels,
+    and instances of such a Model had Labels associated with them - then this migration would fail,
+    because the Labels would not be found, migrated, and deleted, and the old-Label table would not be able to be dropped.
+
+    And the plugins described above would have to have existed and been in use with pulpcore/3.21,only -
+    if they appeared with core/3.22, they'd be using new-Labels and all would be (already) well.
+
+    No such plugins/Models exist, to the best of our knowledge.
+
+    [0] https://github.com/pulp/pulpcore/issues/4319
+    """
+    Label = apps.get_model("core", "Label")
+    Repository = apps.get_model("core", "Repository")
+    Remote = apps.get_model("core", "Remote")
+    Distribution = apps.get_model("core", "Distribution")
+    ContentType = apps.get_model("contenttypes", "ContentType")
+
+    for master_model, model_name in [(Repository, "repository"), (Remote, "remote"), (Distribution, "distribution")]:
+        detail_ctypes = ContentType.objects.filter(app_label__ne="core", model__endswith=model_name)
+        affected_ids = Label.objects.filter(content_type__in=detail_ctypes).values("object_id").distinct()
+        label_subq = Label.objects.filter(
+            content_type__in=detail_ctypes, object_id=OuterRef("pulp_id")
+        ).annotate(
+            label_data=RawSQL("hstore(array_agg(key), array_agg(value))", [])
+        ).values("label_data")
+        master_model.objects.filter(pulp_id__in=affected_ids).update(pulp_labels=label_subq)
+        Label.objects.filter(content_type__in=detail_ctypes).delete()
 
 
 def check_no_existing_labels(apps, schema_editor):
@@ -18,11 +57,20 @@
     ]
 
     operations = [
+        migrations.RunPython(
+            code=migrate_remaining_labels,
+            reverse_code=migrations.RunPython.noop,
+            elidable=True,
+        ),
         migrations.RunPython(
             code=check_no_existing_labels,
             reverse_code=migrations.RunPython.noop,
             elidable=True,
         ),
+        migrations.RunSQL(
+            sql="SET CONSTRAINTS ALL IMMEDIATE;",
+            reverse_sql="",
+        ),
         migrations.DeleteModel(
             name='Label',
         ),

{"golden_diff": "diff --git a/pulpcore/app/migrations/0104_delete_label.py b/pulpcore/app/migrations/0104_delete_label.py\n--- a/pulpcore/app/migrations/0104_delete_label.py\n+++ b/pulpcore/app/migrations/0104_delete_label.py\n@@ -1,6 +1,45 @@\n # Generated by Django 3.2.18 on 2023-05-04 07:06\n \n from django.db import migrations\n+from django.db.models.expressions import OuterRef, RawSQL\n+\n+\n+def migrate_remaining_labels(apps, schema_editor):\n+    \"\"\"\n+    This data migration handles the \"but what about plugins\" problem noted in the issue [0], with only two caveats:\n+\n+    Case 1: If there were to exist a plugin containing a Model whose model-name ended in (for example) \"Repository\",\n+    that was NOT a detail-model of a Repository master-model, AND that plugin allowed Labels for such a model - then,\n+    upon running this migration, those Labels would be lost.\n+\n+    Case 2: If there were to exist a plugin containing a Model that was a Detail of (for example) Repository,\n+    but named something like \"PluginRepositoryButWhy\", and that plugin allowed Labels,\n+    and instances of such a Model had Labels associated with them - then this migration would fail,\n+    because the Labels would not be found, migrated, and deleted, and the old-Label table would not be able to be dropped.\n+\n+    And the plugins described above would have to have existed and been in use with pulpcore/3.21,only -\n+    if they appeared with core/3.22, they'd be using new-Labels and all would be (already) well.\n+\n+    No such plugins/Models exist, to the best of our knowledge.\n+\n+    [0] https://github.com/pulp/pulpcore/issues/4319\n+    \"\"\"\n+    Label = apps.get_model(\"core\", \"Label\")\n+    Repository = apps.get_model(\"core\", \"Repository\")\n+    Remote = apps.get_model(\"core\", \"Remote\")\n+    Distribution = apps.get_model(\"core\", \"Distribution\")\n+    ContentType = apps.get_model(\"contenttypes\", \"ContentType\")\n+\n+    for master_model, model_name in [(Repository, \"repository\"), (Remote, \"remote\"), (Distribution, \"distribution\")]:\n+        detail_ctypes = ContentType.objects.filter(app_label__ne=\"core\", model__endswith=model_name)\n+        affected_ids = Label.objects.filter(content_type__in=detail_ctypes).values(\"object_id\").distinct()\n+        label_subq = Label.objects.filter(\n+            content_type__in=detail_ctypes, object_id=OuterRef(\"pulp_id\")\n+        ).annotate(\n+            label_data=RawSQL(\"hstore(array_agg(key), array_agg(value))\", [])\n+        ).values(\"label_data\")\n+        master_model.objects.filter(pulp_id__in=affected_ids).update(pulp_labels=label_subq)\n+        Label.objects.filter(content_type__in=detail_ctypes).delete()\n \n \n def check_no_existing_labels(apps, schema_editor):\n@@ -18,11 +57,20 @@\n     ]\n \n     operations = [\n+        migrations.RunPython(\n+            code=migrate_remaining_labels,\n+            reverse_code=migrations.RunPython.noop,\n+            elidable=True,\n+        ),\n         migrations.RunPython(\n             code=check_no_existing_labels,\n             reverse_code=migrations.RunPython.noop,\n             elidable=True,\n         ),\n+        migrations.RunSQL(\n+            sql=\"SET CONSTRAINTS ALL IMMEDIATE;\",\n+            reverse_sql=\"\",\n+        ),\n         migrations.DeleteModel(\n             name='Label',\n         ),\n", "issue": "Need a way to directly migrate pulp_labels from 3.21 to 3.28\n**Is your feature request related to a problem? Please describe.**\r\nAs RHUI developers, we are heavy users of pulp_labels on repository objects.  As the current 4.5 release is on pulpcore 3.21, we would like our next release (4.6) to be based on pulpcore 3.28, and we need to ensure a smooth upgrade path for our customers.  As it is currently laid out, the 3.21 to 3.28 migration path requires an intermediate step (<3.25) where `pulpcore-manager datarepair-labels` command is available.\r\n\r\n**Describe the solution you'd like**\r\nThe ideal solution would be to make Django migrations take care of that automatically, without requiring an intermediate step/release.  Since we live in non-ideal world, a solution where an additional command like `datarepair-labels` is available would also suffice.\r\n\r\n**Describe alternatives you've considered**\r\nWe have considered an intermediate release of RHUI 4.6 with pulpcore 3.22 followed by RHUI 4.7 built on 3.28, but it would create a lot of migration issues for our customers, with many of them still running RHUI 4.0.\r\n\r\n\n", "before_files": [{"content": "# Generated by Django 3.2.18 on 2023-05-04 07:06\n\nfrom django.db import migrations\n\n\ndef check_no_existing_labels(apps, schema_editor):\n    Label = apps.get_model(\"core\", \"Label\")\n    if Label.objects.exists():\n        raise RuntimeError(\n            \"There are remaining labels. Please revert to pulpcore<3.25 and make sure all labels are properly mirgated or deleted.\"\n        )\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        ('core', '0103_alter_export_task'),\n    ]\n\n    operations = [\n        migrations.RunPython(\n            code=check_no_existing_labels,\n            reverse_code=migrations.RunPython.noop,\n            elidable=True,\n        ),\n        migrations.DeleteModel(\n            name='Label',\n        ),\n    ]\n", "path": "pulpcore/app/migrations/0104_delete_label.py"}], "after_files": [{"content": "# Generated by Django 3.2.18 on 2023-05-04 07:06\n\nfrom django.db import migrations\nfrom django.db.models.expressions import OuterRef, RawSQL\n\n\ndef migrate_remaining_labels(apps, schema_editor):\n    \"\"\"\n    This data migration handles the \"but what about plugins\" problem noted in the issue [0], with only two caveats:\n\n    Case 1: If there were to exist a plugin containing a Model whose model-name ended in (for example) \"Repository\",\n    that was NOT a detail-model of a Repository master-model, AND that plugin allowed Labels for such a model - then,\n    upon running this migration, those Labels would be lost.\n\n    Case 2: If there were to exist a plugin containing a Model that was a Detail of (for example) Repository,\n    but named something like \"PluginRepositoryButWhy\", and that plugin allowed Labels,\n    and instances of such a Model had Labels associated with them - then this migration would fail,\n    because the Labels would not be found, migrated, and deleted, and the old-Label table would not be able to be dropped.\n\n    And the plugins described above would have to have existed and been in use with pulpcore/3.21,only -\n    if they appeared with core/3.22, they'd be using new-Labels and all would be (already) well.\n\n    No such plugins/Models exist, to the best of our knowledge.\n\n    [0] https://github.com/pulp/pulpcore/issues/4319\n    \"\"\"\n    Label = apps.get_model(\"core\", \"Label\")\n    Repository = apps.get_model(\"core\", \"Repository\")\n    Remote = apps.get_model(\"core\", \"Remote\")\n    Distribution = apps.get_model(\"core\", \"Distribution\")\n    ContentType = apps.get_model(\"contenttypes\", \"ContentType\")\n\n    for master_model, model_name in [(Repository, \"repository\"), (Remote, \"remote\"), (Distribution, \"distribution\")]:\n        detail_ctypes = ContentType.objects.filter(app_label__ne=\"core\", model__endswith=model_name)\n        affected_ids = Label.objects.filter(content_type__in=detail_ctypes).values(\"object_id\").distinct()\n        label_subq = Label.objects.filter(\n            content_type__in=detail_ctypes, object_id=OuterRef(\"pulp_id\")\n        ).annotate(\n            label_data=RawSQL(\"hstore(array_agg(key), array_agg(value))\", [])\n        ).values(\"label_data\")\n        master_model.objects.filter(pulp_id__in=affected_ids).update(pulp_labels=label_subq)\n        Label.objects.filter(content_type__in=detail_ctypes).delete()\n\n\ndef check_no_existing_labels(apps, schema_editor):\n    Label = apps.get_model(\"core\", \"Label\")\n    if Label.objects.exists():\n        raise RuntimeError(\n            \"There are remaining labels. Please revert to pulpcore<3.25 and make sure all labels are properly mirgated or deleted.\"\n        )\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        ('core', '0103_alter_export_task'),\n    ]\n\n    operations = [\n        migrations.RunPython(\n            code=migrate_remaining_labels,\n            reverse_code=migrations.RunPython.noop,\n            elidable=True,\n        ),\n        migrations.RunPython(\n            code=check_no_existing_labels,\n            reverse_code=migrations.RunPython.noop,\n            elidable=True,\n        ),\n        migrations.RunSQL(\n            sql=\"SET CONSTRAINTS ALL IMMEDIATE;\",\n            reverse_sql=\"\",\n        ),\n        migrations.DeleteModel(\n            name='Label',\n        ),\n    ]\n", "path": "pulpcore/app/migrations/0104_delete_label.py"}]}