Last active
May 11, 2020 08:28
-
-
Save filipeximenes/e4f4681474efdee696aa2a4542101d82 to your computer and use it in GitHub Desktop.
Multitenancy post code examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spinners = ( | |
Spinner.objects | |
.using(request.customer.name) | |
.annotate( | |
avg_duration=Avg('owned_spinners__spins__duration')) | |
.order_by('-avg_duration')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DATABASES = { | |
'default': { | |
'ENGINE': ..., | |
'NAME': ..., | |
}, | |
'ibm': { | |
'ENGINE': ..., | |
'NAME': ..., | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def multidb_middleware(get_response): | |
def middleware(request): | |
subdomain = get_subdomain(request) | |
customer = get_customer(subdomain) | |
request.customer = customer | |
@thread_local(using_db=customer.name) | |
def execute_request(request): | |
return get_response(request) | |
response = execute_request(request) | |
return response | |
return middleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spinners = ( | |
Spinner.objects | |
.annotate( | |
avg_duration=Avg('owned_spinners__spins__duration')) | |
.order_by('-avg_duration')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TenantRouter(object): | |
def db_for_read(self, model, **hints): | |
return get_thread_local('using_db', 'default') | |
def db_for_write(self, model, **hints): | |
return get_thread_local('using_db', 'default') | |
# … |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DATABASE_ROUTERS = ['multitenancy.routers.TenantRouter'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE SCHEMA ibm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
try: | |
cursor_for_search_path.execute( | |
'SET search_path = {0}'.format(','.join(search_paths))) | |
except (django.db.utils.DatabaseError, psycopg2.InternalError): | |
self.search_path_set = False | |
else: | |
self.search_path_set = True | |
if name: | |
cursor_for_search_path.close() | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
connection.set_schema_to_public() | |
hostname = self.hostname_from_request(request) | |
TenantModel = get_tenant_model() | |
try: | |
tenant = self.get_tenant(TenantModel, hostname, request) | |
assert isinstance(tenant, TenantModel) | |
except TenantModel.DoesNotExist: | |
# ... | |
request.tenant = tenant | |
connection.set_tenant(request.tenant) | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET search_path TO ibm; | |
SELECT id, name FROM user | |
WHERE user.name LIKE 'F%'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT id, name FROM ibm.user | |
WHERE ibm.user.name LIKE 'F%' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT id, duration FROM ibm.spinner_spin | |
WHERE duration > 120 | |
UNION | |
SELECT id, duration FROM vinta.spinner_spin | |
WHERE duration > 120; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT uuid, duration FROM ibm.spinner_spin | |
WHERE duration > 120 | |
UNION | |
SELECT uuid, duration FROM vinta.spinner_spin | |
WHERE duration > 120; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment