Browse Source

ref(sourcemaps): Update deletion logic (#46308)

Riccardo Busetti 1 year ago
parent
commit
6a4a32c97d

+ 7 - 14
src/sentry/api/endpoints/artifact_bundles.py

@@ -1,6 +1,6 @@
 from typing import Optional
 
-from django.db import router
+from django.db import transaction
 from django.db.models import Q
 from rest_framework import status
 from rest_framework.request import Request
@@ -12,7 +12,6 @@ from sentry.api.exceptions import ResourceDoesNotExist, SentryAPIException
 from sentry.api.paginator import OffsetPaginator
 from sentry.api.serializers import serialize
 from sentry.models import ArtifactBundle, ProjectArtifactBundle, ReleaseArtifactBundle
-from sentry.utils.db import atomic_transaction
 
 
 class InvalidSortByParameter(SentryAPIException):
@@ -125,21 +124,15 @@ class ArtifactBundlesEndpoint(ProjectEndpoint, ArtifactBundlesMixin):
 
         if bundle_id:
             try:
-                artifact_bundle = ArtifactBundle.objects.get(
-                    organization_id=project.organization_id, bundle_id=bundle_id
-                )
+                with transaction.atomic():
+                    ArtifactBundle.objects.get(
+                        organization_id=project.organization_id, bundle_id=bundle_id
+                    ).delete()
+
+                return Response(status=204)
             except ArtifactBundle.DoesNotExist:
                 return Response(
                     {"error": f"Couldn't find a bundle with bundle_id {bundle_id}"}, status=404
                 )
 
-            with atomic_transaction(using=router.db_for_write(ArtifactBundle)):
-                # We want to delete all the connections to a project.
-                ProjectArtifactBundle.objects.filter(artifact_bundle_id=artifact_bundle.id).delete()
-
-                # We also delete the bundle itself.
-                artifact_bundle.delete()
-
-                return Response(status=204)
-
         return Response({"error": f"Supplied an invalid bundle_id {bundle_id}"}, status=404)

+ 27 - 1
tests/sentry/api/endpoints/test_artifact_bundles.py

@@ -3,7 +3,14 @@ from datetime import datetime, timedelta
 from django.urls import reverse
 from freezegun import freeze_time
 
-from sentry.models import ArtifactBundle, ProjectArtifactBundle, ReleaseArtifactBundle
+from sentry.models import (
+    ArtifactBundle,
+    DebugIdArtifactBundle,
+    File,
+    ProjectArtifactBundle,
+    ReleaseArtifactBundle,
+    SourceFileType,
+)
 from sentry.testutils import APITestCase
 from sentry.testutils.silo import region_silo_test
 
@@ -192,6 +199,18 @@ class ArtifactBundlesEndpointTest(APITestCase):
             project_id=project.id,
             artifact_bundle=artifact_bundle,
         )
+        ReleaseArtifactBundle.objects.create(
+            organization_id=self.organization.id,
+            release_name="1.0",
+            dist_name="android",
+            artifact_bundle=artifact_bundle,
+        )
+        DebugIdArtifactBundle.objects.create(
+            organization_id=self.organization.id,
+            debug_id="eb6e60f1-65ff-4f6f-adff-f1bbeded627b",
+            source_file_type=SourceFileType.MINIFIED_SOURCE.value,
+            artifact_bundle=artifact_bundle,
+        )
 
         url = reverse(
             "sentry-api-0-artifact-bundles",
@@ -206,3 +225,10 @@ class ArtifactBundlesEndpointTest(APITestCase):
         assert not ProjectArtifactBundle.objects.filter(
             artifact_bundle_id=artifact_bundle.id
         ).exists()
+        assert not ReleaseArtifactBundle.objects.filter(
+            artifact_bundle_id=artifact_bundle.id
+        ).exists()
+        assert not DebugIdArtifactBundle.objects.filter(
+            artifact_bundle_id=artifact_bundle.id
+        ).exists()
+        assert not File.objects.filter(id=artifact_bundle.file.id).exists()