-
-
Notifications
You must be signed in to change notification settings - Fork 801
/
Copy pathdb_router.py
76 lines (59 loc) · 2.43 KB
/
db_router.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
apps_in_beta = {"some_other_app", "this_one_too"}
# These are bare minimum routers to fake the scenario where there is actually a
# decision around where an application's models might live.
class AlphaRouter:
# alpha is where the core Django models are stored including user. To keep things
# simple this is where the oauth2 provider models are stored as well because they
# have a foreign key to User.
def db_for_read(self, model, **hints):
if model._meta.app_label not in apps_in_beta:
return "alpha"
return None
def db_for_write(self, model, **hints):
if model._meta.app_label not in apps_in_beta:
return "alpha"
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._state.db == "alpha" and obj2._state.db == "alpha":
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label not in apps_in_beta:
return db == "alpha"
return None
class BetaRouter:
def db_for_read(self, model, **hints):
if model._meta.app_label in apps_in_beta:
return "beta"
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in apps_in_beta:
return "beta"
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._state.db == "beta" and obj2._state.db == "beta":
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in apps_in_beta:
return db == "beta"
class CrossDatabaseRouter:
# alpha is where the core Django models are stored including user. To keep things
# simple this is where the oauth2 provider models are stored as well because they
# have a foreign key to User.
def db_for_read(self, model, **hints):
if model._meta.model_name == "accesstoken":
return "beta"
return None
def db_for_write(self, model, **hints):
if model._meta.model_name == "accesstoken":
return "beta"
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._state.db == "beta" and obj2._state.db == "beta":
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if model_name == "accesstoken":
return db == "beta"
return None