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

Migration fails in a with Custom DB Router #199

Closed
VaZark opened this issue Nov 16, 2022 · 3 comments
Closed

Migration fails in a with Custom DB Router #199

VaZark opened this issue Nov 16, 2022 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@VaZark
Copy link
Contributor

VaZark commented Nov 16, 2022

Python version
3.8

Django version
4.1.3

Package version
0.22.1

Current behavior (bug description)
The get_active_theme when invoked in the post_migrate_handler throws an error in a multi-db setup. It does respect the current db defined by the signal migrate.

Expected behavior
Pass the using parameter set in kwargs of post_migrate_handler to get_active_theme to be used with the queryset

Related error : https://code.djangoproject.com/ticket/34165

Minimal example

from threading import local
from django.conf import settings

local_state = local()


class InvalidTenantException(Exception):
    pass


class TenantSubdomainMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        ## Get Subdomain
        host = request.get_host().split(":")[0]
        local_state.subdomain = (
            # We assume single level of subdomain : app.service.com 
            # HOST_IP : used to for local dev. 
            host if host in settings.HOST_IP else host.split(".")[0]
        )
        response = self.get_response(request)
        return response


class TenantDatabaseRouter:
    def _default_db(self):
        subdomain = getattr(local_state, "subdomain", None)
        if subdomain is not None and subdomain in settings.TENANT_MAP:
            db_name = settings.TENANT_MAP[local_state.subdomain]
            return db_name
        else:
            raise InvalidTenantException()

    def db_for_read(self, model, **hints):
        print("read", hints)
        return self._default_db()

    def db_for_write(self, model, **hints):
        print("write", hints)
        return self._default_db()

    def allow_relation(self, obj1, obj2, **hints):
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        return None

## settings.py

INSTALLED_APPS = [
    "admin_interface",
    ...
]

MIDDLEWARE = [
    "utils.tenant_db_router.TenantSubdomainMiddleware",
    "django.middleware.security.SecurityMiddleware",
    ...
]

TENANT_MAP = {"localhost":"default", "tenant_1":"default"}
DATABASE_ROUTERS = ["utils.tenant_db_router.TenantDatabaseRouter"]
@VaZark VaZark added the bug Something isn't working label Nov 16, 2022
@fabiocaccamo
Copy link
Owner

@VaZark thank you for reporting this, could you submit a PR containing a failing test and the relative fix?

@fabiocaccamo
Copy link
Owner

fabiocaccamo commented Nov 16, 2022

@cristianosavoia probably this issue is related to #170.

@VaZark VaZark closed this as completed Nov 18, 2022
@fabiocaccamo
Copy link
Owner

Fix available by upgrading to 0.22.2 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants