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

chore(actor) Remove Actor from django state (#70503)

Remove the Actor model from django state. This required reworking a few
migration operations in the squash to avoid creating constraints on a
table that doesn't exist anymore. It seems that when a model is removed
from state django doesn't create the table and any constraints that were
defined earlier in migration history need to be updated.
Mark Story 10 месяцев назад
Родитель
Сommit
f327610f59

+ 0 - 9
fixtures/backup/fresh-install.json

@@ -39,15 +39,6 @@
     "value": "\"23.6.1\""
   }
 },
-{
-  "model": "sentry.actor",
-  "pk": 1,
-  "fields": {
-    "type": 0,
-    "user_id": null,
-    "team": 1
-  }
-},
 {
   "model": "sentry.email",
   "pk": 1,

+ 0 - 18
fixtures/backup/model_dependencies/detailed.json

@@ -323,24 +323,6 @@
     "table_name": "sentry_activity",
     "uniques": []
   },
-  "sentry.actor": {
-    "dangling": false,
-    "foreign_keys": {
-      "team": {
-        "kind": "FlexibleForeignKey",
-        "model": "sentry.team",
-        "nullable": true
-      }
-    },
-    "model": "sentry.actor",
-    "relocation_dependencies": [],
-    "relocation_scope": "Excluded",
-    "silos": [
-      "Region"
-    ],
-    "table_name": "sentry_actor",
-    "uniques": []
-  },
   "sentry.alertrule": {
     "dangling": false,
     "foreign_keys": {

+ 0 - 3
fixtures/backup/model_dependencies/flat.json

@@ -44,9 +44,6 @@
     "sentry.project",
     "sentry.user"
   ],
-  "sentry.actor": [
-    "sentry.team"
-  ],
   "sentry.alertrule": [
     "sentry.organization",
     "sentry.snubaquery",

+ 0 - 1
fixtures/backup/model_dependencies/sorted.json

@@ -115,7 +115,6 @@
   "sentry.appconnectbuild",
   "sentry.apikey",
   "sentry.apiapplication",
-  "sentry.actor",
   "sentry.activity",
   "replays.replayrecordingsegment",
   "hybridcloud.orgauthtokenreplica",

+ 0 - 1
fixtures/backup/model_dependencies/truncate.json

@@ -115,7 +115,6 @@
   "sentry_appconnectbuild",
   "sentry_apikey",
   "sentry_apiapplication",
-  "sentry_actor",
   "sentry_activity",
   "replays_replayrecordingsegment",
   "hybridcloud_orgauthtokenreplica",

+ 1 - 1
migrations_lockfile.txt

@@ -9,5 +9,5 @@ feedback: 0004_index_together
 hybridcloud: 0016_add_control_cacheversion
 nodestore: 0002_nodestore_no_dictfield
 replays: 0004_index_together
-sentry: 0715_remove_actormodel_constraints
+sentry: 0716_remove_actormodel
 social_auth: 0002_default_auto_field

+ 12 - 4
src/sentry/backup/imports.py

@@ -74,6 +74,10 @@ DELETED_FIELDS: dict[str, set[str]] = {
     "sentry.grouphistory": {"actor"},
 }
 
+# When models are removed from the application, they will continue to be in exports
+# from previous releases. Models in this list are elided from data as imports are processed.
+DELETED_MODELS = {"sentry.actor"}
+
 # The maximum number of models that may be sent at a time.
 MAX_BATCH_SIZE = 20
 
@@ -169,17 +173,21 @@ def _import(
         else src.read().decode("utf-8")
     )
 
-    if len(DELETED_FIELDS) > 0:
-        # Parse the content JSON and remove and fields that we have marked for deletion in the
+    if len(DELETED_MODELS) > 0 or len(DELETED_FIELDS) > 0:
+        # Parse the content JSON and remove fields and models that we have marked for deletion in the
         # function.
-        shimmed_models = set(DELETED_FIELDS.keys())
         content_as_json = json.loads_experimental("backup.enable-orjson", content)  # type: ignore[arg-type]
-        for json_model in content_as_json:
+
+        shimmed_models = set(DELETED_FIELDS.keys())
+        for i, json_model in enumerate(content_as_json):
             if json_model["model"] in shimmed_models:
                 fields_to_remove = DELETED_FIELDS[json_model["model"]]
                 for field in fields_to_remove:
                     json_model["fields"].pop(field, None)
 
+            if json_model["model"] in DELETED_MODELS:
+                del content_as_json[i]
+
         # Return the content to byte form, as that is what the Django deserializer expects.
         content = json.dumps_experimental("backup.enable-orjson", content_as_json)
 

+ 1 - 0
src/sentry/db/router.py

@@ -68,6 +68,7 @@ class SiloRouter:
     """
 
     historical_silo_assignments = {
+        "sentry_actor": SiloMode.REGION,
         "sentry_teamavatar": SiloMode.REGION,
         "sentry_projectavatar": SiloMode.REGION,
         "sentry_pagerdutyservice": SiloMode.REGION,

+ 28 - 12
src/sentry/migrations/0001_squashed_0484_break_org_member_user_fk.py

@@ -6576,11 +6576,6 @@ class Migration(CheckedMigration):
                     reverse_sql="DROP INDEX CONCURRENTLY IF EXISTS sentry_team_actor_idx;",
                     hints={"tables": ["sentry_team"]},
                 ),
-                migrations.RunSQL(
-                    sql='ALTER TABLE sentry_team ADD CONSTRAINT "sentry_team_actor_idx_fk_sentry_actor_id" FOREIGN KEY ("actor_id") REFERENCES "sentry_actor" ("id") DEFERRABLE INITIALLY DEFERRED;',
-                    reverse_sql="ALTER TABLE sentry_team DROP CONSTRAINT IF EXISTS sentry_team_actor_idx_fk_sentry_actor_id;",
-                    hints={"tables": ["sentry_team"]},
-                ),
             ],
             state_operations=[
                 migrations.AddField(
@@ -6599,14 +6594,20 @@ class Migration(CheckedMigration):
             model_name="alertrule",
             name="owner",
             field=sentry.db.models.fields.foreignkey.FlexibleForeignKey(
-                null=True, on_delete=django.db.models.deletion.CASCADE, to="sentry.Actor"
+                null=True,
+                on_delete=django.db.models.deletion.CASCADE,
+                to="sentry.Actor",
+                db_constraint=False,
             ),
         ),
         migrations.AddField(
             model_name="rule",
             name="owner",
             field=sentry.db.models.fields.foreignkey.FlexibleForeignKey(
-                null=True, on_delete=django.db.models.deletion.CASCADE, to="sentry.Actor"
+                null=True,
+                on_delete=django.db.models.deletion.CASCADE,
+                to="sentry.Actor",
+                db_constraint=False,
             ),
         ),
         migrations.AlterField(
@@ -6687,7 +6688,9 @@ class Migration(CheckedMigration):
                 (
                     "actor",
                     sentry.db.models.fields.foreignkey.FlexibleForeignKey(
-                        on_delete=django.db.models.deletion.CASCADE, to="sentry.Actor"
+                        on_delete=django.db.models.deletion.CASCADE,
+                        to="sentry.Actor",
+                        db_constraint=False,
                     ),
                 ),
                 (
@@ -7708,7 +7711,10 @@ class Migration(CheckedMigration):
                 (
                     "actor",
                     sentry.db.models.fields.foreignkey.FlexibleForeignKey(
-                        null=True, on_delete=django.db.models.deletion.CASCADE, to="sentry.Actor"
+                        null=True,
+                        on_delete=django.db.models.deletion.CASCADE,
+                        to="sentry.Actor",
+                        db_constraint=False,
                     ),
                 ),
                 (
@@ -7810,7 +7816,10 @@ class Migration(CheckedMigration):
             model_name="grouphistory",
             name="actor",
             field=sentry.db.models.fields.foreignkey.FlexibleForeignKey(
-                null=True, on_delete=django.db.models.deletion.SET_NULL, to="sentry.Actor"
+                null=True,
+                on_delete=django.db.models.deletion.SET_NULL,
+                to="sentry.Actor",
+                db_constraint=False,
             ),
         ),
         migrations.SeparateDatabaseAndState(
@@ -9531,13 +9540,17 @@ class Migration(CheckedMigration):
                         related_name="team_from_actor",
                         to="sentry.Actor",
                         unique=True,
+                        db_constraint=False,
                     ),
                 ),
                 migrations.AlterField(
                     model_name="alertrule",
                     name="owner",
                     field=sentry.db.models.fields.foreignkey.FlexibleForeignKey(
-                        null=True, on_delete=django.db.models.deletion.SET_NULL, to="sentry.Actor"
+                        null=True,
+                        on_delete=django.db.models.deletion.SET_NULL,
+                        to="sentry.Actor",
+                        db_constraint=False,
                     ),
                 ),
             ],
@@ -9877,7 +9890,10 @@ class Migration(CheckedMigration):
             model_name="rule",
             name="owner",
             field=sentry.db.models.fields.foreignkey.FlexibleForeignKey(
-                null=True, on_delete=django.db.models.deletion.SET_NULL, to="sentry.Actor"
+                null=True,
+                on_delete=django.db.models.deletion.SET_NULL,
+                to="sentry.Actor",
+                db_constraint=False,
             ),
         ),
         migrations.AddField(

+ 36 - 0
src/sentry/migrations/0716_remove_actormodel.py

@@ -0,0 +1,36 @@
+# Generated by Django 5.0.4 on 2024-05-08 14:30
+
+from django.db import migrations
+
+from sentry.new_migrations.migrations import CheckedMigration
+
+
+class Migration(CheckedMigration):
+    # This flag is used to mark that a migration shouldn't be automatically run in production.
+    # 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 post deployment:
+    # - Large data migrations. Typically we want these to be run manually 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
+    #   run this outside deployments so that we don't block them. Note that while adding an index
+    #   is a schema change, it's completely safe to run the operation after the code has deployed.
+    # Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment
+
+    is_post_deployment = False
+
+    dependencies = [
+        ("sentry", "0715_remove_actormodel_constraints"),
+    ]
+
+    operations = [
+        migrations.SeparateDatabaseAndState(
+            database_operations=[],
+            state_operations=[
+                migrations.DeleteModel(
+                    name="Actor",
+                ),
+            ],
+        )
+    ]

Некоторые файлы не были показаны из-за большого количества измененных файлов