Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds optional cursor override to support django schema tenants #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions django_redshift_backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
import json

import django
from django.utils.asyncio import async_unsafe
from django.utils import timezone
from django.conf import settings
from django.core.exceptions import FieldDoesNotExist
from django.db.models import Index
from django.db.models.expressions import Col
from django.db.utils import NotSupportedError, ProgrammingError
from django.db import connection


from ._vendor.django40.db.backends.base.introspection import FieldInfo, TableInfo
from ._vendor.django40.db.backends.base.schema import (
Expand Down Expand Up @@ -1395,3 +1398,13 @@ def check_constraints(self, table_names=None):
No constraints to check in Redshift.
"""
pass

@async_unsafe
def create_cursor(self, name=None):
cursor = super().create_cursor()
DJANGO_REDSHIFT_ENABLE_TENANT_SCHEMA = getattr(settings, 'REDSHIFT_SET_TENANT_SCHEMA', False)

if DJANGO_REDSHIFT_ENABLE_TENANT_SCHEMA:
cursor.execute(f"SET SEARCH_PATH TO '{connection.schema_name}', public")
Comment on lines +1407 to +1408
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a feature of the Redshift Backend.
I understand that you can't use ‘django_tenants.postgresql_backend’ provided by django-tenants, but I think you should implement a DatabaseWrapper in your application, as django-tenants does.
https://github.com/django-tenants/django-tenants/blob/c02b5a46ae8abc089c3f5f355b4d116e25572a6a/django_tenants/postgresql_backend/base.py#L60


return cursor