We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Upgrade from 21.6.1 to 21.6.2 migration error, relation already exists
### Environment

self-hosted (`onpremise` deployment)

### Version

21.6.1 upgrade to 21.6.1

### Steps to Reproduce

1. git fetch tags/21.6.2
2. ./install.sh

### Expected Result

Migration to succeed.

### Actual Result

```
django.db.utils.ProgrammingError: ProgrammingError('relation "sentry_groupedmessage_project_id_id_515aaa7e_uniq" already exists\n',)
SQL: ALTER TABLE "sentry_groupedmessage" ADD CONSTRAINT "sentry_groupedmessage_project_id_id_515aaa7e_uniq" UNIQUE ("project_id", "id")
```

--- 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/0216_cdc_setup_replication_index.py`
Content:
```
1 # Generated by Django 1.11.29 on 2021-06-30 18:51
2 
3 from django.db import migrations
4 
5 
6 class Migration(migrations.Migration):
7     # This flag is used to mark that a migration shouldn't be automatically run in
8     # production. We set this to True for operations that we think are risky and want
9     # someone from ops to run manually and monitor.
10     # General advice is that if in doubt, mark your migration as `is_dangerous`.
11     # Some things you should always mark as dangerous:
12     # - Large data migrations. Typically we want these to be run manually by ops so that
13     #   they can be monitored. Since data migrations will now hold a transaction open
14     #   this is even more important.
15     # - Adding columns to highly active tables, even ones that are NULL.
16     is_dangerous = True
17 
18     # This flag is used to decide whether to run this migration in a transaction or not.
19     # By default we prefer to run in a transaction, but for migrations where you want
20     # to `CREATE INDEX CONCURRENTLY` this needs to be set to False. Typically you'll
21     # want to create an index concurrently when adding one to an existing table.
22     # You'll also usually want to set this to `False` if you're writing a data
23     # migration, since we don't want the entire migration to run in one long-running
24     # transaction.
25     atomic = False
26 
27     dependencies = [
28         ("sentry", "0215_fix_state"),
29     ]
30 
31     operations = [
32         migrations.AlterUniqueTogether(
33             name="group",
34             unique_together={("project", "id"), ("project", "short_id")},
35         ),
36         migrations.RunSQL(
37             sql="""
38             ALTER TABLE sentry_groupasignee REPLICA IDENTITY USING INDEX
39             sentry_groupasignee_project_id_group_id_fbf4364e_uniq
40             """,
41             reverse_sql="""
42             ALTER TABLE sentry_groupasignee REPLICA IDENTITY DEFAULT
43             """,
44             hints={"tables": ["sentry_groupasignee"]},
45         ),
46         migrations.RunSQL(
47             sql="""
48             ALTER TABLE sentry_groupedmessage REPLICA IDENTITY USING INDEX
49             sentry_groupedmessage_project_id_id_515aaa7e_uniq
50             """,
51             reverse_sql="""
52             ALTER TABLE sentry_groupedmessage REPLICA IDENTITY DEFAULT
53             """,
54             hints={"tables": ["sentry_groupedmessage"]},
55         ),
56     ]
57
```
--- 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/0216_cdc_setup_replication_index.py b/src/sentry/migrations/0216_cdc_setup_replication_index.py
--- a/src/sentry/migrations/0216_cdc_setup_replication_index.py
+++ b/src/sentry/migrations/0216_cdc_setup_replication_index.py
@@ -29,9 +29,33 @@
     ]
 
     operations = [
-        migrations.AlterUniqueTogether(
-            name="group",
-            unique_together={("project", "id"), ("project", "short_id")},
+        migrations.SeparateDatabaseAndState(
+            database_operations=[
+                migrations.RunSQL(
+                    """
+                    CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS "sentry_groupedmessage_project_id_id_515aaa7e_uniq" ON "sentry_groupedmessage" ("project_id", "id");
+                    """,
+                    reverse_sql="""
+                    DROP INDEX CONCURRENTLY IF EXISTS sentry_groupedmessage_project_id_id_515aaa7e_uniq;
+                    """,
+                    hints={"tables": ["sentry_groupedmessage"]},
+                ),
+                migrations.RunSQL(
+                    """
+                    ALTER TABLE "sentry_groupedmessage" ADD CONSTRAINT "sentry_groupedmessage_project_id_id_515aaa7e_uniq" UNIQUE USING INDEX "sentry_groupedmessage_project_id_id_515aaa7e_uniq";
+                    """,
+                    reverse_sql="""
+                    ALTER TABLE "sentry_groupedmessage" DROP CONSTRAINT IF EXISTS "sentry_groupedmessage_project_id_id_515aaa7e_uniq";
+                    """,
+                    hints={"tables": ["sentry_groupedmessage"]},
+                ),
+            ],
+            state_operations=[
+                migrations.AlterUniqueTogether(
+                    name="group",
+                    unique_together={("project", "id"), ("project", "short_id")},
+                ),
+            ],
         ),
         migrations.RunSQL(
             sql="""

{"golden_diff": "diff --git a/src/sentry/migrations/0216_cdc_setup_replication_index.py b/src/sentry/migrations/0216_cdc_setup_replication_index.py\n--- a/src/sentry/migrations/0216_cdc_setup_replication_index.py\n+++ b/src/sentry/migrations/0216_cdc_setup_replication_index.py\n@@ -29,9 +29,33 @@\n     ]\n \n     operations = [\n-        migrations.AlterUniqueTogether(\n-            name=\"group\",\n-            unique_together={(\"project\", \"id\"), (\"project\", \"short_id\")},\n+        migrations.SeparateDatabaseAndState(\n+            database_operations=[\n+                migrations.RunSQL(\n+                    \"\"\"\n+                    CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS \"sentry_groupedmessage_project_id_id_515aaa7e_uniq\" ON \"sentry_groupedmessage\" (\"project_id\", \"id\");\n+                    \"\"\",\n+                    reverse_sql=\"\"\"\n+                    DROP INDEX CONCURRENTLY IF EXISTS sentry_groupedmessage_project_id_id_515aaa7e_uniq;\n+                    \"\"\",\n+                    hints={\"tables\": [\"sentry_groupedmessage\"]},\n+                ),\n+                migrations.RunSQL(\n+                    \"\"\"\n+                    ALTER TABLE \"sentry_groupedmessage\" ADD CONSTRAINT \"sentry_groupedmessage_project_id_id_515aaa7e_uniq\" UNIQUE USING INDEX \"sentry_groupedmessage_project_id_id_515aaa7e_uniq\";\n+                    \"\"\",\n+                    reverse_sql=\"\"\"\n+                    ALTER TABLE \"sentry_groupedmessage\" DROP CONSTRAINT IF EXISTS \"sentry_groupedmessage_project_id_id_515aaa7e_uniq\";\n+                    \"\"\",\n+                    hints={\"tables\": [\"sentry_groupedmessage\"]},\n+                ),\n+            ],\n+            state_operations=[\n+                migrations.AlterUniqueTogether(\n+                    name=\"group\",\n+                    unique_together={(\"project\", \"id\"), (\"project\", \"short_id\")},\n+                ),\n+            ],\n         ),\n         migrations.RunSQL(\n             sql=\"\"\"\n", "issue": "Upgrade from 21.6.1 to 21.6.2 migration error, relation already exists\n### Environment\n\nself-hosted (`onpremise` deployment)\n\n### Version\n\n21.6.1 upgrade to 21.6.1\n\n### Steps to Reproduce\n\n1. git fetch tags/21.6.2\r\n2. ./install.sh\n\n### Expected Result\n\nMigration to succeed.\n\n### Actual Result\n\n```\r\ndjango.db.utils.ProgrammingError: ProgrammingError('relation \"sentry_groupedmessage_project_id_id_515aaa7e_uniq\" already exists\\n',)\r\nSQL: ALTER TABLE \"sentry_groupedmessage\" ADD CONSTRAINT \"sentry_groupedmessage_project_id_id_515aaa7e_uniq\" UNIQUE (\"project_id\", \"id\")\r\n```\n", "before_files": [{"content": "# Generated by Django 1.11.29 on 2021-06-30 18:51\n\nfrom django.db import migrations\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    # - 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    # This flag is used to decide whether to run this migration in a transaction or not.\n    # By default we prefer to run in a transaction, but for migrations where you want\n    # to `CREATE INDEX CONCURRENTLY` this needs to be set to False. Typically you'll\n    # want to create an index concurrently when adding one to an existing table.\n    # You'll also usually want to set this to `False` if you're writing a data\n    # migration, since we don't want the entire migration to run in one long-running\n    # transaction.\n    atomic = False\n\n    dependencies = [\n        (\"sentry\", \"0215_fix_state\"),\n    ]\n\n    operations = [\n        migrations.AlterUniqueTogether(\n            name=\"group\",\n            unique_together={(\"project\", \"id\"), (\"project\", \"short_id\")},\n        ),\n        migrations.RunSQL(\n            sql=\"\"\"\n            ALTER TABLE sentry_groupasignee REPLICA IDENTITY USING INDEX\n            sentry_groupasignee_project_id_group_id_fbf4364e_uniq\n            \"\"\",\n            reverse_sql=\"\"\"\n            ALTER TABLE sentry_groupasignee REPLICA IDENTITY DEFAULT\n            \"\"\",\n            hints={\"tables\": [\"sentry_groupasignee\"]},\n        ),\n        migrations.RunSQL(\n            sql=\"\"\"\n            ALTER TABLE sentry_groupedmessage REPLICA IDENTITY USING INDEX\n            sentry_groupedmessage_project_id_id_515aaa7e_uniq\n            \"\"\",\n            reverse_sql=\"\"\"\n            ALTER TABLE sentry_groupedmessage REPLICA IDENTITY DEFAULT\n            \"\"\",\n            hints={\"tables\": [\"sentry_groupedmessage\"]},\n        ),\n    ]\n", "path": "src/sentry/migrations/0216_cdc_setup_replication_index.py"}], "after_files": [{"content": "# Generated by Django 1.11.29 on 2021-06-30 18:51\n\nfrom django.db import migrations\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    # - 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    # This flag is used to decide whether to run this migration in a transaction or not.\n    # By default we prefer to run in a transaction, but for migrations where you want\n    # to `CREATE INDEX CONCURRENTLY` this needs to be set to False. Typically you'll\n    # want to create an index concurrently when adding one to an existing table.\n    # You'll also usually want to set this to `False` if you're writing a data\n    # migration, since we don't want the entire migration to run in one long-running\n    # transaction.\n    atomic = False\n\n    dependencies = [\n        (\"sentry\", \"0215_fix_state\"),\n    ]\n\n    operations = [\n        migrations.SeparateDatabaseAndState(\n            database_operations=[\n                migrations.RunSQL(\n                    \"\"\"\n                    CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS \"sentry_groupedmessage_project_id_id_515aaa7e_uniq\" ON \"sentry_groupedmessage\" (\"project_id\", \"id\");\n                    \"\"\",\n                    reverse_sql=\"\"\"\n                    DROP INDEX CONCURRENTLY IF EXISTS sentry_groupedmessage_project_id_id_515aaa7e_uniq;\n                    \"\"\",\n                    hints={\"tables\": [\"sentry_groupedmessage\"]},\n                ),\n                migrations.RunSQL(\n                    \"\"\"\n                    ALTER TABLE \"sentry_groupedmessage\" ADD CONSTRAINT \"sentry_groupedmessage_project_id_id_515aaa7e_uniq\" UNIQUE USING INDEX \"sentry_groupedmessage_project_id_id_515aaa7e_uniq\";\n                    \"\"\",\n                    reverse_sql=\"\"\"\n                    ALTER TABLE \"sentry_groupedmessage\" DROP CONSTRAINT IF EXISTS \"sentry_groupedmessage_project_id_id_515aaa7e_uniq\";\n                    \"\"\",\n                    hints={\"tables\": [\"sentry_groupedmessage\"]},\n                ),\n            ],\n            state_operations=[\n                migrations.AlterUniqueTogether(\n                    name=\"group\",\n                    unique_together={(\"project\", \"id\"), (\"project\", \"short_id\")},\n                ),\n            ],\n        ),\n        migrations.RunSQL(\n            sql=\"\"\"\n            ALTER TABLE sentry_groupasignee REPLICA IDENTITY USING INDEX\n            sentry_groupasignee_project_id_group_id_fbf4364e_uniq\n            \"\"\",\n            reverse_sql=\"\"\"\n            ALTER TABLE sentry_groupasignee REPLICA IDENTITY DEFAULT\n            \"\"\",\n            hints={\"tables\": [\"sentry_groupasignee\"]},\n        ),\n        migrations.RunSQL(\n            sql=\"\"\"\n            ALTER TABLE sentry_groupedmessage REPLICA IDENTITY USING INDEX\n            sentry_groupedmessage_project_id_id_515aaa7e_uniq\n            \"\"\",\n            reverse_sql=\"\"\"\n            ALTER TABLE sentry_groupedmessage REPLICA IDENTITY DEFAULT\n            \"\"\",\n            hints={\"tables\": [\"sentry_groupedmessage\"]},\n        ),\n    ]\n", "path": "src/sentry/migrations/0216_cdc_setup_replication_index.py"}]}