通过 URL 参数进行数据库路由

Database Routing by URL parameter

我正在使用多个数据库开发 Django 项目。在应用程序中,我需要根据用户的请求将数据库连接从开发数据库切换到测试数据库或生产数据库。 (数据库架构已设置且不可更改!)

我也用这个旧指南试试运气 here 不起作用。在 DB Router 中,我无法访问 threading.locals.

我也试过设置自定义数据库路由器。通过会话变量,我尝试设置连接字符串。要读取 dbRouter 中的用户会话,我需要确切的会话密钥,否则我必须循环抛出所有会话。

object.using('DB_CONNECTION) 的方法是不可接受的解决方案……对于许多依赖项。我想为登录用户全局设置一个连接,而不为每个模型函数提供数据库连接……

请告诉我如何解决这个问题。

我应该能够 return db Router 中的 dbConnection 基于 会话值...

def db_for_read|write|*():
   from django.contrib.sessions.backends.db import SessionStore
   session = SessionStore(session_key='WhyINeedHereAKey_SessionKeyCouldBeUserId')
   return session['app.dbConnection']

更新1: 谢谢@victorT 的输入。我只是用给定的例子试了一下。 还没有达到目标...

这是我试过的。也许您会看到配置错误。

Django Version:     2.1.4
Python Version:     3.6.3
Exception Value:    (1146, "Table 'app.myModel' doesn't exist")

.app/views/myView.py

from ..models import myModel
from ..thread_local import thread_local

class myView:
    @thread_local(DB_FOR_READ_OVERRIDE='MY_DATABASE')
    def get_queryset(self, *args, **kwargs):
        return myModel.objects.get_queryset()

.app/myRouter.py

from .thread_local import get_thread_local

class myRouter:
    def db_for_read(self, model, **hints):
        myDbCon = get_thread_local('DB_FOR_READ_OVERRIDE', 'default')
        print('Returning myDbCon:', myDbCon)
        return myDbCon

.app/thread_local.py

import threading
from functools import wraps


threadlocal = threading.local()


class thread_local(object):
    def __init__(self, **kwargs):
        self.options = kwargs

    def __enter__(self):
        for attr, value in self.options.items():
            print(attr, value)
            setattr(threadlocal, attr, value)

    def __exit__(self, exc_type, exc_value, traceback):
        for attr in self.options.keys():
            setattr(threadlocal, attr, None)

    def __call__(self, test_func):

        @wraps(test_func)
        def inner(*args, **kwargs):
            # the thread_local class is also a context manager
            # which means it will call __enter__ and __exit__
            with self:
                return test_func(*args, **kwargs)

        return inner

def get_thread_local(attr, default=None):
    """ use this method from lower in the stack to get the value """
    return getattr(threadlocal, attr, default)

这是输出:

Returning myDbCon: default              
DEBUG (0.000) None; args=None
DEBUG (0.000) None; args=None
DEBUG (0.000) None; args=('2019-05-14 06:13:39.477467', '4agimu6ctbwgykvu31tmdvuzr5u94tgk')
DEBUG (0.001) None; args=(1,)
DB_FOR_READ_OVERRIDE MY_DATABASE        # The local_router seems to get the given db Name,
Returning myDbCon: None                 # But disapears in the Router
DEBUG (0.000) None; args=()
Returning myDbCon: None
DEBUG (0.001) None; args=()
Returning myDbCon: None
DEBUG (0.001) None; args=()
Returning myDbCon: None
DEBUG (0.001) None; args=()
Returning myDbCon: None
DEBUG (0.001) None; args=()
Returning myDbCon: None
DEBUG (0.002) None; args=()
ERROR Internal Server Error: /app/env/list/    # It switches back to the default
Traceback (most recent call last):
  File "/.../lib64/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/.../lib64/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/.../lib64/python3.6/site-packages/MySQLdb/cursors.py", line 255, in execute
    self.errorhandler(self, exc, value)
  File "/.../lib64/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/.../lib64/python3.6/site-packages/MySQLdb/cursors.py", line 252, in execute
    res = self._query(query)
  File "/.../lib64/python3.6/site-packages/MySQLdb/cursors.py", line 378, in _query
    db.query(q)
  File "/.../lib64/python3.6/site-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.ProgrammingError: (1146, "Table 'app.myModel' doesn't exist")

The above exception was the direct cause of the following exception:

更新2: 这是使用会话的尝试。

我通过会话中的中间件存储数据库连接。 在路由器中,我想访问请求的会话。我的期望是,Django 处理这个并且知道请求者。但我必须将会话密钥作为

 s = SessionStore(session_key='???')

我没有连接到路由器...

.middleware.py

from django.contrib.sessions.backends.file import SessionStore

class myMiddleware:

    def  process_view(self, request, view_func, view_args, view_kwargs):
        s = SessionStore()
        s['app.dbConnection'] = view_kwargs['MY_DATABASE']
        s.create()    

.myRouter.py

class myRouter:
    def db_for_read(self, model, **hints):
        from django.contrib.sessions.backends.file import SessionStore
        s = SessionStore(session_key='???')
        return s['app.dbConnection']

这与 threading.local 的结果相同......一个空值 :-(

在响应请求时使用 threading.local 设置变量 (doc)。自定义路由器是必经之路。

来源:

摘自django-dynamic-db-router:

from dynamic_db_router import in_database

with in_database('non-default-db'):
    result = run_complex_query()

请注意,该项目适用于 Django 1.11 max,可能存在兼容性问题。尽管如此,两个来源中描述的路由器 class 和装饰器都非常简单。

在您的情况下,设置变量 per-user。提醒一下,您会找到 request.user.

的用户

至少我有时间测试 threadlocals 包,它适用于 Django 2.1 和 Python 3.6.3。

.app/middleware.py

from threadlocals.threadlocals import set_request_variable

try:
    from django.utils.deprecation import MiddlewareMixin
except ImportError:
    MiddlewareMixin = object

class MyMiddleware(MiddlewareMixin):
    def  process_view(self, request, view_func, view_args, view_kwargs):
        set_request_variable('dbConnection', view_kwargs['environment'])

...

.app/router.py

from threadlocals.threadlocals import get_request_variable

class MyRouter:
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'app':
            return get_request_variable("dbConnection")
        return None
...