From 87aeab557c78f8ebe40817b3f66397efa7d6666d Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 2 Jun 2023 11:00:14 -0400 Subject: [PATCH] fix(hybridcloud) Clear lookup state during initialization (#50166) This avoids leaking state between instances of the SiloRouter which was impacting getsentry tests. --- src/sentry/db/router.py | 5 ++++- tests/sentry/db/test_router.py | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/sentry/db/router.py b/src/sentry/db/router.py index c0b9729c7bb50..9353a5d99f2fb 100644 --- a/src/sentry/db/router.py +++ b/src/sentry/db/router.py @@ -39,6 +39,7 @@ class SiloRouter: """Whether or not we're operating in a simulated silo environment""" def __init__(self): + self.__table_to_silo = {} try: # By accessing the connections Django will raise # Use `assert` to appease linters @@ -89,7 +90,9 @@ def _db_for_table(self, table, app_label): # have to scan through models more than once. self.__table_to_silo[table] = self._db_for_model(model) - return self.__table_to_silo[table] + # All actively used tables should be in this map, but we also + # need to handle tables in migrations that no longer exist. + return self.__table_to_silo.get(table, "default") def db_for_read(self, model, **hints): return self._db_for_model(model) diff --git a/tests/sentry/db/test_router.py b/tests/sentry/db/test_router.py index 43a91bf23418f..6156d1170a422 100644 --- a/tests/sentry/db/test_router.py +++ b/tests/sentry/db/test_router.py @@ -24,6 +24,11 @@ def test_for_control(self): assert router.allow_migrate("control", "sentry", User) assert not router.allow_migrate("region", "sentry", User) + # Ensure tables that no longer exist don't fail + assert router.allow_migrate( + "default", "sentry", model=None, hints={"tables": ["jira_ac_tenant"]} + ) + @override_settings(SILO_MODE="REGION") def test_for_region(self): router = SiloRouter()