This repository was archived by the owner on Jun 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy path__init__.py
More file actions
69 lines (56 loc) · 2.21 KB
/
__init__.py
File metadata and controls
69 lines (56 loc) · 2.21 KB
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
import logging
from django.conf import settings
from django.db.models import Field, Lookup
log = logging.getLogger(__name__)
class DatabaseRouter:
"""
A router to control all database operations on models across multiple databases.
https://docs.djangoproject.com/en/4.0/topics/db/multi-db/#automatic-database-routing
"""
def db_for_read(self, model, **hints):
if model._meta.app_label == "timeseries":
if settings.TIMESERIES_DATABASE_READ_REPLICA_ENABLED:
return "timeseries_read"
else:
return "timeseries"
else:
if settings.DATABASE_READ_REPLICA_ENABLED:
return "default_read"
else:
return "default"
def db_for_write(self, model, **hints):
if model._meta.app_label == "timeseries":
return "timeseries"
else:
return "default"
def allow_migrate(self, db, app_label, model_name=None, **hints):
if (
db == "timeseries" or db == "timeseries_read"
) and not settings.TIMESERIES_ENABLED:
log.warning("Skipping timeseries migration")
return False
if db == "default_read" or db == "timeseries_read":
log.warning("Skipping migration of read-only database")
return False
if app_label == "timeseries":
return db == "timeseries"
else:
return db == "default"
def allow_relation(self, obj1, obj2, **hints):
obj1_app = obj1._meta.app_label
obj2_app = obj2._meta.app_label
# cannot form relationship across default <-> timeseries dbs
if obj1_app == "timeseries" and obj2_app != "timeseries":
return False
if obj1_app != "timeseries" and obj2_app == "timeseries":
return False
# otherwise we allow it
return True
@Field.register_lookup
class IsNot(Lookup):
lookup_name = "isnot"
def as_sql(self, compiler, connection):
lhs, lhs_params = self.process_lhs(compiler, connection)
rhs, rhs_params = self.process_rhs(compiler, connection)
params = tuple(lhs_params) + tuple(rhs_params)
return "%s is not %s" % (lhs, rhs), params