Skip to content

Commit 54d389b

Browse files
ref: fix types for sentry.db.router (#80456)
<!-- Describe your PR here. -->
1 parent 09f75b6 commit 54d389b

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ module = [
188188
"sentry.auth.system",
189189
"sentry.db.mixin",
190190
"sentry.db.postgres.base",
191-
"sentry.db.router",
192191
"sentry.discover.endpoints.discover_key_transactions",
193192
"sentry.eventstore.models",
194193
"sentry.grouping.strategies.legacy",

src/sentry/db/router.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
from django.apps import apps
77
from django.db import connections
8+
from django.db.models.base import Model
89
from django.utils.connection import ConnectionDoesNotExist
910

10-
from sentry.db.models.base import Model, ModelSiloLimit
11+
from sentry.db.models.base import ModelSiloLimit
1112
from sentry.silo.base import SiloLimit, SiloMode
1213
from sentry.utils.env import in_test_environment
1314

@@ -44,9 +45,6 @@ class SiloRouter:
4445
SiloMode.CONTROL: "control",
4546
}
4647

47-
__table_to_silo = {}
48-
"""Memoized results of table : silo pairings"""
49-
5048
__is_simulated = False
5149
"""Whether or not we're operating in a simulated silo environment"""
5250

@@ -82,7 +80,8 @@ class SiloRouter:
8280
"""
8381

8482
def __init__(self):
85-
self.__table_to_silo = {}
83+
# Memoized results of table : silo pairings
84+
self.__table_to_silo: dict[str, str | None] = {}
8685
try:
8786
# By accessing the connections Django will raise
8887
# Use `assert` to appease linters
@@ -122,14 +121,14 @@ def _resolve_silo_connection(self, silo_modes: Iterable[SiloMode], table: str) -
122121
else:
123122
return None
124123

125-
def _find_model(self, table: str, app_label: str) -> Model | None:
124+
def _find_model(self, table: str) -> type[Model] | None:
126125
# Use django's model inventory to find our table and what silo it is on.
127-
for model in apps.get_models(app_label):
126+
for model in apps.get_models(include_auto_created=True):
128127
if model._meta.db_table == table:
129128
return model
130129
return None
131130

132-
def _silo_limit(self, model: Model) -> SiloLimit | None:
131+
def _silo_limit(self, model: type[Model]) -> SiloLimit | None:
133132
silo_limit = getattr(model._meta, "silo_limit", None)
134133
if silo_limit:
135134
return silo_limit
@@ -140,24 +139,24 @@ def _silo_limit(self, model: Model) -> SiloLimit | None:
140139

141140
# If we didn't find a silo_limit we could be working with __fake__ model
142141
# from django, so we need to locate the real class by table.
143-
real_model = self._find_model(db_table, model._meta.app_label)
142+
real_model = self._find_model(db_table)
144143
if real_model:
145144
return getattr(real_model._meta, "silo_limit", None)
146145

147146
return None
148147

149-
def _db_for_model(self, model: Model):
148+
def _db_for_model(self, model: type[Model]) -> str | None:
150149
silo_limit = self._silo_limit(model)
151150
if not silo_limit:
152151
return "default"
153152

154153
return self._resolve_silo_connection(silo_limit.modes, table=model._meta.db_table)
155154

156-
def _db_for_table(self, table, app_label):
155+
def _db_for_table(self, table: str, app_label: str) -> str | None:
157156
if table in self.__table_to_silo:
158157
return self.__table_to_silo[table]
159158

160-
model = self._find_model(table, app_label)
159+
model = self._find_model(table)
161160
if model:
162161
# Incrementally build up our result cache so we don't
163162
# have to scan through models more than once.
@@ -171,7 +170,7 @@ def _db_for_table(self, table, app_label):
171170
# Default to None for sentry/getsentry app_label as models
172171
# in those apps must have silo assignments, and 'default'
173172
# for other app_labels that can't have silo assignments.
174-
fallback = "default"
173+
fallback: str | None = "default"
175174
if app_label in {"sentry", "getsentry"}:
176175
fallback = None
177176
self.__table_to_silo[table] = fallback

0 commit comments

Comments
 (0)