1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import threading
threadlocal = threading.local()
def get_thread_local(attr, default=None):
""" use this method from lower in the stack to get the value """
return getattr(threadlocal, attr, default)
class ThreadLocalRouter(object):
"""A router to control all database operations base on threadlocal variables.
"""
def db_for_read(self, model, **hints):
return get_thread_local('DB_FOR_READ_OVERRIDE')
def db_for_write(self, model, **hints):
return get_thread_local('DB_FOR_WRITE_OVERRIDE')
def allow_relation(self, obj1, obj2, **hints):
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
return None
class DorisRouter(object):
def db_for_read(self, model, **hints):
if model._meta.app_label == "doris":
return 'doris'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == "doris":
return 'doris'
return None
def allow_relation(self, obj1, obj2, **hints):
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
return None