From 953040943dd1ddd8a2203e37c57f4ca257ddde7a Mon Sep 17 00:00:00 2001 From: viatrak Date: Thu, 7 Sep 2017 12:34:11 -0500 Subject: [PATCH 1/3] Redundant code to set_schema(). Make it DRY --- tenant_schemas/postgresql_backend/base.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tenant_schemas/postgresql_backend/base.py b/tenant_schemas/postgresql_backend/base.py index 6e940fa..a9c52a0 100644 --- a/tenant_schemas/postgresql_backend/base.py +++ b/tenant_schemas/postgresql_backend/base.py @@ -88,10 +88,7 @@ class DatabaseWrapper(original_backend.DatabaseWrapper): """ Instructs to stay in the common 'public' schema. """ - self.tenant = FakeTenant(schema_name=get_public_schema_name()) - self.schema_name = get_public_schema_name() - self.set_settings_schema(self.schema_name) - self.search_path_set = False + self.set_schema(get_public_schema_name()) def set_settings_schema(self, schema_name): self.settings_dict['SCHEMA'] = schema_name From 86c4050856774fd36e7759336b5fa89109d155ee Mon Sep 17 00:00:00 2001 From: viatrak Date: Thu, 7 Sep 2017 12:41:12 -0500 Subject: [PATCH 2/3] Redundant code in set_tenant as set_schema(). Make it DRY --- tenant_schemas/postgresql_backend/base.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tenant_schemas/postgresql_backend/base.py b/tenant_schemas/postgresql_backend/base.py index a9c52a0..ad61f5b 100644 --- a/tenant_schemas/postgresql_backend/base.py +++ b/tenant_schemas/postgresql_backend/base.py @@ -67,11 +67,8 @@ class DatabaseWrapper(original_backend.DatabaseWrapper): Main API method to current database schema, but it does not actually modify the db connection. """ + self.set_schema(tenant.schema_name, include_public) self.tenant = tenant - self.schema_name = tenant.schema_name - self.include_public_schema = include_public - self.set_settings_schema(self.schema_name) - self.search_path_set = False def set_schema(self, schema_name, include_public=True): """ From 7e104037c9522a408138c79c1fa8be3f98fc5894 Mon Sep 17 00:00:00 2001 From: viatrak Date: Thu, 7 Sep 2017 12:43:40 -0500 Subject: [PATCH 3/3] Content type cache should be cleared not just at the beginning of a request, but *anytime* the schema is changed (which happens in middle of requests). set_tenant() will also now result in a set_schema() call so it will claer_cache() at the beginning of a request, and on subsequent set_schema calls --- tenant_schemas/middleware.py | 9 --------- tenant_schemas/postgresql_backend/base.py | 9 +++++++++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tenant_schemas/middleware.py b/tenant_schemas/middleware.py index 639e686..c70c053 100644 --- a/tenant_schemas/middleware.py +++ b/tenant_schemas/middleware.py @@ -62,15 +62,6 @@ class BaseTenantMiddleware(MIDDLEWARE_MIXIN): request.tenant = tenant connection.set_tenant(request.tenant) - # Content type can no longer be cached as public and tenant schemas - # have different models. If someone wants to change this, the cache - # needs to be separated between public and shared schemas. If this - # cache isn't cleared, this can cause permission problems. For example, - # on public, a particular model has id 14, but on the tenants it has - # the id 15. if 14 is cached instead of 15, the permissions for the - # wrong model will be fetched. - ContentType.objects.clear_cache() - # Do we have a public-specific urlconf? if hasattr(settings, 'PUBLIC_SCHEMA_URLCONF') and request.tenant.schema_name == get_public_schema_name(): request.urlconf = settings.PUBLIC_SCHEMA_URLCONF diff --git a/tenant_schemas/postgresql_backend/base.py b/tenant_schemas/postgresql_backend/base.py index ad61f5b..212d3ea 100644 --- a/tenant_schemas/postgresql_backend/base.py +++ b/tenant_schemas/postgresql_backend/base.py @@ -3,6 +3,7 @@ import warnings import psycopg2 from django.conf import settings +from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ImproperlyConfigured, ValidationError import django.db.utils @@ -80,6 +81,14 @@ class DatabaseWrapper(original_backend.DatabaseWrapper): self.include_public_schema = include_public self.set_settings_schema(schema_name) self.search_path_set = False + # Content type can no longer be cached as public and tenant schemas + # have different models. If someone wants to change this, the cache + # needs to be separated between public and shared schemas. If this + # cache isn't cleared, this can cause permission problems. For example, + # on public, a particular model has id 14, but on the tenants it has + # the id 15. if 14 is cached instead of 15, the permissions for the + # wrong model will be fetched. + ContentType.objects.clear_cache() def set_schema_to_public(self): """