I'm moving a Django 1.8 project from a single database setup to a writer/reader setup. I ran into the issue described in Django bug 23718 but the work arounds described didn't seem to help.
Has anyone run into similar issues? Relevant code segments are below:
Router:
class DatabaseRouter(object):
"""Router to handle separation of reads and writes."""
def db_for_read(self, model, **hints):
"""Reads go to a read replica."""
return 'read'
def db_for_write(self, model, **hints):
"""Writes always go to default."""
return 'default'
def allow_relation(self, obj1, obj2, **hints):
"""Allow relations bet/n objects in the same DB cluster."""
db_list = ('default', 'read')
if obj1._state.db in db_list and obj2._state.db in db_list:
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""All models end up in this pool."""
return True
relevant DB settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': DB_NAME,
'USER': DB_USER,
'PASSWORD': DB_PASS,
'HOST': DB_WRITE
},
'read': {
'ENGINE': 'django.db.backends.mysql',
'NAME': DB_NAME,
'USER': DB_USER,
'PASSWORD': DB_PASS,
'HOST': DB_READ,
'TEST': {
'MIRROR': 'default',
},
}
}
DATABASE_ROUTERS = ['my_project.routers.DatabaseRouter']
work around replication test case:
class ReplicationTestCase(TestCase):
@classmethod
def setUpClass(cls):
super(ReplicationTestCase, cls).setUpClass()
connections['read']._orig_cursor = connections['read'].cursor
connections['read'].cursor = connections['default'].cursor
@classmethod
def tearDownClass(cls):
connections['read'].cursor = connections['read']._orig_cursor
super(ReplicationTestCase, cls).tearDownClass()
Anything pop out? I'm happy to provide stacktraces from our test environment if that would be useful as well. Thanks!