I am trying to set up 2 databases in django. The second database is meant for testing purposes, so the 2 databases practically have the same models.
My question is how to start the server using the second database.
The steps I did for setting up the databases:
- I created a second app called 'testing', so now in my project root folder there is the app
api
(the real one) andtesting
(the app meant for the test database). Afterwards, added the same models fromapi/models.py
totesting/models.py
- Created router for the first database and the second database:
class AuthRouter:
route_app_labels = {'auth', 'contenttypes', 'admin', 'sessions'}
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'default'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'default'
return None
def allow_migrate(self, db, app_label, model_name = None, **hints):
if app_label in self.route_app_labels:
return db=='default'
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
class SpotTesting:
route_app_labels = {'spot_testing'}
def db_for_read(self, model, **hints):
if model._meta.app_label == "spot_testing":
return 'test_db'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == "spot_testing":
return 'test_db'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Relations between objects are allowed if both objects are
in the primary/replica pool.
"""
db_list = ('primary', 'replica1', 'replica2')
if obj1._state.db == "spot_testing" and obj2._state.db == "spot_testing":
return 'test_db'
return None
def allow_migrate(self, db, app_label, model_name = None, **hints):
if app_label == "spot_testing":
return 'test_db'
return None
- Edited
api/settings.py
:
DATABASE_ROUTERS = [
'api.routers.db_routers.AuthRouter',
'api.routers.TestRouter.SpotTesting',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': DBNAME,
'USER': USER,
'PASSWORD': PASSWORD,
'HOST': HOST,
'PORT': PORT,
},
'test_db': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': "spot_db_testing",
'USER': USER,
'PASSWORD': PASSWORD,
'HOST': HOST,
'PORT': PORT,
}
}
- Ran
python manage.py migrate --database=test_db
This is how the test db looks after migrations. It looks like the some tables that django would normally create are missing
Now I am not sure what command should I run in order to make django runserver
use the test_db
instead of the normal one.