Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions src/sentry/models/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import ClassVar, Self
from urllib.parse import unquote

from django.db import IntegrityError, models, router, transaction
from django.db import models
from django.utils import timezone

from sentry.backup.scopes import RelocationScope
Expand All @@ -15,11 +15,9 @@
sane_repr,
)
from sentry.db.models.manager.base import BaseManager
from sentry.options.rollout import in_random_rollout
from sentry.utils import metrics
from sentry.utils.cache import cache
from sentry.utils.hashlib import md5_text
from sentry.utils.rollback_metrics import incr_rollback_metrics

OK_NAME_PATTERN = re.compile(ENVIRONMENT_NAME_PATTERN)

Expand Down Expand Up @@ -112,23 +110,11 @@ def add_project(self, project, is_hidden=None):
cache_key = f"envproj:c:{self.id}:{project.id}"

if cache.get(cache_key) is None:
if in_random_rollout("environmentproject.new_add_project.rollout"):
EnvironmentProject.objects.get_or_create(
project=project, environment=self, defaults={"is_hidden": is_hidden}
)
# The object already exists, we cache the action to reduce the load on the database.
cache.set(cache_key, 1, 3600)
else:
try:
with transaction.atomic(router.db_for_write(EnvironmentProject)):
EnvironmentProject.objects.create(
project=project, environment=self, is_hidden=is_hidden
)
cache.set(cache_key, 1, 3600)
except IntegrityError:
incr_rollback_metrics(EnvironmentProject)
# We've already created the object, should still cache the action.
cache.set(cache_key, 1, 3600)
EnvironmentProject.objects.get_or_create(
project=project, environment=self, defaults={"is_hidden": is_hidden}
)
# The object already exists, we cache the action to reduce the load on the database.
cache.set(cache_key, 1, 3600)

@staticmethod
def get_name_from_path_segment(segment):
Expand Down
25 changes: 0 additions & 25 deletions tests/sentry/models/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from sentry.models.environment import Environment
from sentry.testutils.cases import TestCase
from sentry.testutils.helpers import override_options


class GetOrCreateTest(TestCase):
Expand All @@ -24,30 +23,6 @@ def test_simple(self):
with self.assertNumQueries(0):
assert Environment.get_for_organization_id(project.organization_id, "prod").id == env.id

@override_options({"environmentproject.new_add_project.rollout": 1.0})
def test_simple_with_new_option(self):
"""
Same test as test_simple, but with the new option enabled which has refactored
the get_or_create method.
Note (vgrozdanic): this test will be removed after the rollout is complete.
"""
project = self.create_project()

with pytest.raises(Environment.DoesNotExist):
Environment.get_for_organization_id(project.organization_id, "prod")

env = Environment.get_or_create(project=project, name="prod")

assert env.name == "prod"
assert env.projects.first().id == project.id

env2 = Environment.get_or_create(project=project, name="prod")

assert env2.id == env.id

with self.assertNumQueries(0):
assert Environment.get_for_organization_id(project.organization_id, "prod").id == env.id


@pytest.mark.parametrize(
"val,expected",
Expand Down
Loading