Skip to content

Commit 74f912b

Browse files
committed
specify database backend to use
1 parent e6d26ca commit 74f912b

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

README.md

+56-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ To disable queue name validation, set `QUEUES` to `[]`.
125125

126126
### The database backend worker
127127

128-
First, you'll need to add `django_tasks.backends.database` to `INSTALLED_APPS`:
128+
First, you'll need to add `django_tasks.backends.database` to `INSTALLED_APPS`:
129129

130130
```python
131131
INSTALLED_APPS = [
@@ -238,6 +238,61 @@ Whilst signals are available, they may not be the most maintainable approach.
238238
- `django_tasks.signals.task_enqueued`: Called when a task is enqueued. The sender is the backend class. Also called with the enqueued `task_result`.
239239
- `django_tasks.signals.task_finished`: Called when a task finishes (`SUCCEEDED` or `FAILED`). The sender is the backend class. Also called with the finished `task_result`.
240240

241+
### Database Routers
242+
243+
**Define Multiple Databases in settings.py**
244+
245+
```python
246+
DATABASES = {
247+
'default': {
248+
'ENGINE': 'django.db.backends.postgresql',
249+
'NAME': 'default_db',
250+
'USER': 'default_user',
251+
'PASSWORD': 'default_password',
252+
'HOST': 'localhost',
253+
'PORT': '5432',
254+
},
255+
'queue_db': {
256+
'ENGINE': 'django.db.backends.postgresql',
257+
'NAME': 'queue_db',
258+
'USER': 'queue_user',
259+
'PASSWORD': 'queue_password',
260+
'HOST': 'localhost',
261+
'PORT': '5432',
262+
},
263+
'analytics_db': {
264+
'ENGINE': 'django.db.backends.postgresql',
265+
'NAME': 'analytics_db',
266+
'USER': 'analytics_user',
267+
'PASSWORD': 'analytics_password',
268+
'HOST': 'localhost',
269+
'PORT': '5432',
270+
},
271+
}
272+
```
273+
274+
**Specify the Router in settings.py**
275+
276+
In settings.py, specify the `DATABASE_ROUTERS` setting to point to your custom router class.
277+
278+
```
279+
DATABASE_ROUTERS = ['django_tasks.backends.database.routers.DynamicDatabaseRouter']
280+
```
281+
282+
**Add Models to Apps**
283+
284+
Ensure that the APP_TO_DB_MAP variable is defined correctly in settings.py and contains the necessary mappings for your apps. For example:
285+
286+
```python
287+
APP_TO_DB_MAP = {
288+
'task_queue': 'queue_db',
289+
'analytics': 'analytics_db',
290+
'default_app': 'default',
291+
}
292+
```
293+
294+
This way, the database router can dynamically access this mapping and direct queries to the appropriate database.
295+
241296
## Contributing
242297

243298
See [CONTRIBUTING.md](./CONTRIBUTING.md) for information on how to contribute.
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from django.conf import settings
2+
3+
class DynamicDatabaseRouter:
4+
"""
5+
A database router that retrieves the app-to-database map from settings.py.
6+
"""
7+
8+
def get_db(self, app_label):
9+
"""
10+
Retrieves the database for the given app label from settings.
11+
If the app is not found in the map, returns 'default' as a fallback.
12+
"""
13+
app_db_map = getattr(settings, 'APP_TO_DB_MAP', {})
14+
return app_db_map.get(app_label, 'default')
15+
16+
def db_for_read(self, model, **hints):
17+
"""
18+
Directs read operations to the appropriate database.
19+
"""
20+
return self.get_db(model._meta.app_label)
21+
22+
def db_for_write(self, model, **hints):
23+
"""
24+
Directs write operations to the appropriate database.
25+
"""
26+
return self.get_db(model._meta.app_label)
27+
28+
def allow_migrate(self, db, app_label, model_name=None, **hints):
29+
"""
30+
Ensures migrations happen only on the appropriate database.
31+
"""
32+
return db == self.get_db(app_label)

0 commit comments

Comments
 (0)