Skip to content

Commit f717d52

Browse files
Fix read replicas (#118)
* fix loading database urls * modify db router allow_relation method * Update sensorsafrica/router.py Co-authored-by: Clemence Kyara <kilemensi@users.noreply.github.com> * Update sensorsafrica/settings.py Co-authored-by: Clemence Kyara <kilemensi@users.noreply.github.com> Co-authored-by: Clemence Kyara <kilemensi@users.noreply.github.com>
1 parent cebd53e commit f717d52

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

sensorsafrica/router.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ def db_for_write(self, model, **hints):
1616

1717
def allow_relation(self, obj1, obj2, **hints):
1818
"""
19-
Relations between objects are allowed if both objects are
20-
in the primary/replica pool.
19+
Relation not applicable for our use case
2120
"""
22-
db_set = {'default', 'read_replica'}
23-
if obj1._state.db in db_set and obj2._state.db in db_set:
24-
return True
2521
return None
22+
23+
def allow_migrate(self,db,app_label,model_name=None, **hints):
24+
"""
25+
Restrict migration operations to the master db i.e. default
26+
"""
27+
return db == "default"
28+
29+

sensorsafrica/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@
110110
"postgres://sensorsafrica:sensorsafrica@localhost:5432/sensorsafrica",
111111
)
112112

113-
READ_DATABASE_URLS = os.getenv("SENSORSAFRICA_READ_DATABASE_URLS", DATABASE_URL).split(",")
114-
115113
DATABASES = {"default": dj_database_url.parse(DATABASE_URL), }
116114

115+
READ_DATABASE_URLS = os.getenv("SENSORSAFRICA_READ_DATABASE_URLS", DATABASE_URL).split(",")
116+
117117
for index, read_database_url in enumerate(READ_DATABASE_URLS,start=1):
118118
DATABASES[f"read_replica_{index}"] = dj_database_url.parse(read_database_url)
119119

0 commit comments

Comments
 (0)