Skip to content

Commit 2f93c2e

Browse files
authored
Fix read replicas (#122)
* fix loading database urls * modify db router allow_relation method * remove app label in router * add file based session management * update router.py read replicas * fix custom migration by specifying default db * using schema editor to define migration db * update migration schema editor connection * update migration schema editor connection * update migration schema editor connection
1 parent 2d57882 commit 2f93c2e

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ services:
2626
context: .
2727
environment:
2828
SENSORSAFRICA_DATABASE_URL: postgres://sensorsafrica:sensorsafrica@postgres:5432/sensorsafrica
29-
SENSORSAFRICA_READ_DATABASE_URL: postgres://sensorsafrica:sensorsafrica@postgres:5432/read_replica
29+
SENSORSAFRICA_READ_DATABASE_URLS: postgres://sensorsafrica:sensorsafrica@postgres:5432/sensorsafrica
3030
SENSORSAFRICA_RABBITMQ_URL: amqp://sensorsafrica:sensorsafrica@rabbitmq//
3131
SENSORSAFRICA_FLOWER_ADMIN_USERNAME: admin
3232
SENSORSAFRICA_FLOWER_ADMIN_PASSWORD: password

sensorsafrica/openstuttgart/feinstaub/sensors/migrations/0011_auto_20150807_1927.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ def migrate_sensor(apps, schema_editor):
1212
# if we directly import it, it'll be the wrong version
1313
Sensor = apps.get_model("sensors", "Sensor")
1414
Node = apps.get_model("sensors", "Node")
15-
for sensor in Sensor.objects.all():
16-
print("sensor: {}".format(sensor.id))
17-
node = Node.objects.create(uid=sensor.uid,
18-
description=sensor.description,
19-
owner=sensor.owner,
20-
location=sensor.location)
15+
db_alias = schema_editor.connection.alias
16+
if db_alias != "default":
17+
return
18+
for sensor in Sensor.objects.using(db_alias).all():
19+
node = Node.objects.using(db_alias).create(uid=sensor.uid,
20+
description=sensor.description,
21+
owner=sensor.owner,
22+
location=sensor.location)
2123
sensor.node = node
2224
sensor.save()
2325

sensorsafrica/router.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
from django.conf import settings
44

55
class ReplicaRouter:
6-
read_replica_app_labels = {'sensors', }
7-
read_replicas = list(settings.DATABASES.keys() - {'default', })
6+
read_replicas = settings.READ_DATABASE_URLS
87

98
def db_for_read(self, model, **hints):
10-
if model._meta.app_label in self.read_replica_app_labels:
11-
return random.choice(self.read_replicas)
12-
return 'default'
13-
9+
return random.choice(self.read_replicas)
10+
1411
def db_for_write(self, model, **hints):
1512
return "default"
1613

sensorsafrica/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101

102102
WSGI_APPLICATION = "sensorsafrica.wsgi.application"
103103

104+
SESSION_ENGINE = "django.contrib.sessions.backends.file"
104105

105106
# Database
106107
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

0 commit comments

Comments
 (0)