-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
217 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from couchdbkit.client import Database | ||
from corehq.util.couch import get_document_class_by_doc_type | ||
from dimagi.utils.decorators.memoized import memoized | ||
from django.conf import settings | ||
|
||
|
||
class CouchConfig(object): | ||
""" | ||
Interface for accessing the couch-related settings | ||
You can use db_uri pass in a different couch instance | ||
from the one specified by COUCH_DATABASE | ||
""" | ||
def __init__(self, db_uri=None): | ||
if db_uri: | ||
self._settings_helper = ( | ||
settings.COUCH_SETTINGS_HELPER._replace(couch_database_url=db_uri)) | ||
else: | ||
self._settings_helper = settings.COUCH_SETTINGS_HELPER | ||
|
||
@property | ||
def db_uri(self): | ||
return self._settings_helper.couch_database_url | ||
|
||
@property | ||
@memoized | ||
def all_db_uris_by_slug(self): | ||
dbs = self._settings_helper.get_extra_couchdbs() | ||
dbs[None] = self.db_uri | ||
return dbs | ||
|
||
@property | ||
@memoized | ||
def all_dbs_by_slug(self): | ||
return {slug: Database(db_uri) | ||
for slug, db_uri in self.all_db_uris_by_slug.items()} | ||
|
||
def get_db(self, postfix): | ||
""" | ||
Get the couch database by slug | ||
""" | ||
return Database(self.all_db_uris_by_slug[postfix], create=True) | ||
|
||
@property | ||
@memoized | ||
def app_label_to_db_uri(self): | ||
return dict(self._settings_helper.make_couchdb_tuples()) | ||
|
||
def get_db_uri_for_class(self, klass): | ||
return self.app_label_to_db_uri[getattr(klass._meta, "app_label")] | ||
|
||
def get_db_uri_for_doc_type(self, doc_type): | ||
return self.get_db_uri_for_class(get_document_class_by_doc_type(doc_type)) | ||
|
||
def get_db_for_class(self, klass): | ||
return Database(self.get_db_uri_for_class(klass)) | ||
|
||
def get_db_for_doc_type(self, doc_type): | ||
return Database(self.get_db_uri_for_doc_type(doc_type)) | ||
|
||
|
||
couch_config = CouchConfig() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from django.conf import settings | ||
from django.test import SimpleTestCase | ||
from corehq.util.couchdb_management import CouchConfig, couch_config | ||
|
||
|
||
class CouchConfigTest(SimpleTestCase): | ||
remote_db_uri = 'https://mycouch.com/cchq' | ||
|
||
def test_default_db_uri(self): | ||
config = CouchConfig() | ||
self.assertEqual(config.db_uri, settings.COUCH_DATABASE) | ||
|
||
def test_default_couch_config_db_uri(self): | ||
self.assertEqual(couch_config.db_uri, settings.COUCH_DATABASE) | ||
|
||
def test_remote_db_uri(self): | ||
config = CouchConfig(db_uri=self.remote_db_uri) | ||
self.assertEqual(config.db_uri, self.remote_db_uri) | ||
|
||
def test_all_db_uris_by_slug(self): | ||
config = CouchConfig(db_uri=self.remote_db_uri) | ||
self.assertDictContainsSubset( | ||
{ | ||
None: self.remote_db_uri, | ||
'users': '{}__users'.format(self.remote_db_uri), | ||
'fixtures': '{}__fixtures'.format(self.remote_db_uri), | ||
'meta': '{}__meta'.format(self.remote_db_uri), | ||
}, | ||
config.all_db_uris_by_slug | ||
) | ||
|
||
def test_get_db_for_doc_type(self): | ||
config = CouchConfig(db_uri=self.remote_db_uri) | ||
self.assertEqual(config.get_db_for_doc_type('CommCareCase').uri, self.remote_db_uri) | ||
self.assertEqual(config.get_db_for_doc_type('CommCareUser').uri, | ||
'{}__users'.format(self.remote_db_uri)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.