Просмотр исходного кода

chore(hybrid-cloud): Backfill organizationmember_id column in OrganizationMemberMapping (#48025)

Alberto Leal 1 год назад
Родитель
Сommit
9e7d47cc2c

+ 1 - 1
migrations_lockfile.txt

@@ -6,5 +6,5 @@ To resolve this, rebase against latest master and regenerate your migration. Thi
 will then be regenerated, and you should be able to merge without conflicts.
 
 nodestore: 0002_nodestore_no_dictfield
-sentry: 0431_breaking_orgintegration_pieces_and_default_auth_team_fks
+sentry: 0432_backfill_org_member_id_organizationmembermapping
 social_auth: 0001_initial

+ 53 - 0
src/sentry/migrations/0432_backfill_org_member_id_organizationmembermapping.py

@@ -0,0 +1,53 @@
+# Generated by Django 2.2.28 on 2023-04-26 19:54
+
+from django.db import migrations
+
+from sentry.new_migrations.migrations import CheckedMigration
+from sentry.utils.query import RangeQuerySetWrapperWithProgressBar
+
+
+def backfill_org_member_id_organizationmembermapping(apps, schema_editor):
+    OrganizationMemberMapping = apps.get_model("sentry", "OrganizationMemberMapping")
+    OrganizationMember = apps.get_model("sentry", "OrganizationMember")
+    for org_member_mapping in RangeQuerySetWrapperWithProgressBar(
+        OrganizationMemberMapping.objects.filter(organizationmember_id__isnull=True)
+    ):
+        org_member = None
+        try:
+            org_member = OrganizationMember.objects.filter(
+                organization_id=org_member_mapping.organization_id,
+                user_id=org_member_mapping.user_id,
+                email=org_member_mapping.email,
+            ).get()
+        except OrganizationMember.DoesNotExist:
+            org_member = None
+
+        if org_member is not None:
+            org_member_mapping.organizationmember_id = org_member.id
+            org_member_mapping.save(update_fields=["organizationmember_id"])
+
+
+class Migration(CheckedMigration):
+    # This flag is used to mark that a migration shouldn't be automatically run in production. For
+    # the most part, this should only be used for operations where it's safe to run the migration
+    # after your code has deployed. So this should not be used for most operations that alter the
+    # schema of a table.
+    # Here are some things that make sense to mark as dangerous:
+    # - Large data migrations. Typically we want these to be run manually by ops so that they can
+    #   be monitored and not block the deploy for a long period of time while they run.
+    # - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
+    #   have ops run this and not block the deploy. Note that while adding an index is a schema
+    #   change, it's completely safe to run the operation after the code has deployed.
+    is_dangerous = False
+
+    dependencies = [
+        ("sentry", "0431_breaking_orgintegration_pieces_and_default_auth_team_fks"),
+    ]
+
+    operations = [
+        migrations.RunPython(
+            backfill_org_member_id_organizationmembermapping,
+            reverse_code=migrations.RunPython.noop,
+            hints={"tables": ["sentry_organizationmembermapping", "sentry_organizationmember"]},
+        ),
+    ]