Skip to content

Commit

Permalink
feat(hc): use snowflake ids for Team (#47479)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbro authored Apr 20, 2023
1 parent 14f1d22 commit 9a5a6ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/sentry/models/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from sentry.locks import locks
from sentry.models.actor import Actor
from sentry.utils.retries import TimedRetryPolicy
from sentry.utils.snowflake import SnowflakeIdMixin

if TYPE_CHECKING:
from sentry.models import Organization, Project, User
Expand Down Expand Up @@ -131,7 +132,7 @@ class TeamStatus:


@region_silo_only_model
class Team(Model):
class Team(Model, SnowflakeIdMixin):
"""
A team represents a group of individuals which maintain ownership of projects.
"""
Expand Down Expand Up @@ -183,7 +184,13 @@ def save(self, *args, **kwargs):
lock = locks.get(f"slug:team:{self.organization_id}", duration=5, name="team_slug")
with TimedRetryPolicy(10)(lock.acquire):
slugify_instance(self, self.name, organization=self.organization)
super().save(*args, **kwargs)
if settings.SENTRY_USE_SNOWFLAKE:
snowflake_redis_key = "team_snowflake_key"
self.save_with_snowflake_id(
snowflake_redis_key, lambda: super(Team, self).save(*args, **kwargs)
)
else:
super().save(*args, **kwargs)

@property
def member_set(self):
Expand Down
10 changes: 10 additions & 0 deletions tests/sentry/models/test_team.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.test import override_settings

from sentry.models import (
OrganizationMember,
OrganizationMemberTeam,
Expand Down Expand Up @@ -57,6 +59,14 @@ def test_get_projects(self):
projects = team.get_projects()
assert {_.id for _ in projects} == {project.id}

@override_settings(SENTRY_USE_SNOWFLAKE=False)
def test_without_snowflake(self):
user = self.create_user()
org = self.create_organization(owner=user)
team = self.create_team(organization=org)
assert team.id < 1_000_000_000
assert Team.objects.filter(id=team.id).exists()


class TransferTest(TestCase):
def test_simple(self):
Expand Down

0 comments on commit 9a5a6ed

Please sign in to comment.