diff options
author | Michael Fenn <fennm@deshawresearch.com> | 2014-04-09 11:12:40 -0400 |
---|---|---|
committer | Michael Fenn <fennm@deshawresearch.com> | 2014-04-09 11:12:40 -0400 |
commit | 44c4d214a1f3ea057fbcf6d22de4c586b31cf661 (patch) | |
tree | 18cf2c2755a1ea20555df0a42c673e4302a92a17 /src/lib | |
parent | b527fed1109c958dcb39c3a45c4789379ada39e0 (diff) | |
download | bcfg2-44c4d214a1f3ea057fbcf6d22de4c586b31cf661.tar.gz bcfg2-44c4d214a1f3ea057fbcf6d22de4c586b31cf661.tar.bz2 bcfg2-44c4d214a1f3ea057fbcf6d22de4c586b31cf661.zip |
Reporting: update non-ORM bits to know about separate database
There is a little bit of code in the reporting web interface that
uses raw SQL rather than the django ORM. This bypassed the database
router functionality (since there is no model to bind to). Django
supports keeping track of multiple connections in the raw SQL interface,
so this commit does the refactoring necessary to support the multiple
databases.
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/Bcfg2/DBSettings.py | 17 | ||||
-rw-r--r-- | src/lib/Bcfg2/Reporting/models.py | 10 |
2 files changed, 18 insertions, 9 deletions
diff --git a/src/lib/Bcfg2/DBSettings.py b/src/lib/Bcfg2/DBSettings.py index e45807094..3b2edd849 100644 --- a/src/lib/Bcfg2/DBSettings.py +++ b/src/lib/Bcfg2/DBSettings.py @@ -154,6 +154,17 @@ def migrate_databases(**kwargs): **kwargs) +def get_db_label(application): + """ Get the name of the database for a given Django "application". The + rule is that if a database with the same name as the application exists, + use it. Otherwise use the default. Returns a string suitible for use as a + key in the Django database settings dict """ + if application in settings['DATABASES']: + return application + + return 'default' + + class PerApplicationRouter(object): """ Django database router for redirecting different applications to their own database """ @@ -161,11 +172,7 @@ class PerApplicationRouter(object): def _db_per_app(self, model, **hints): """ If a database with the same name as the application exists, use it. Otherwise use the default """ - app = model._meta.app_label - if app in settings['DATABASES']: - return app - - return 'default' + return get_db_label(model._meta.app_label) def db_for_read(self, model, **hints): """ Called when Django wants to find out what database to read from """ diff --git a/src/lib/Bcfg2/Reporting/models.py b/src/lib/Bcfg2/Reporting/models.py index 0598e4d33..2d96990b1 100644 --- a/src/lib/Bcfg2/Reporting/models.py +++ b/src/lib/Bcfg2/Reporting/models.py @@ -3,7 +3,7 @@ import sys from django.core.exceptions import ImproperlyConfigured try: - from django.db import models, backend, connection + from django.db import models, backend, connections except ImproperlyConfigured: e = sys.exc_info()[1] print("Reports: unable to import django models: %s" % e) @@ -12,6 +12,7 @@ except ImproperlyConfigured: from django.core.cache import cache from datetime import datetime, timedelta from Bcfg2.Compat import cPickle +from Bcfg2.DBSettings import get_db_label TYPE_GOOD = 0 @@ -61,7 +62,8 @@ def _quote(value): global _our_backend if not _our_backend: try: - _our_backend = backend.DatabaseOperations(connection) + _our_backend = backend.DatabaseOperations( + connections[get_db_label('Reporting')]) except TypeError: _our_backend = backend.DatabaseOperations() return _our_backend.quote_name(value) @@ -91,8 +93,8 @@ class InteractionManager(models.Manager): maxdate -- datetime object. Most recent date to pull. (default None) """ - from django.db import connection - cursor = connection.cursor() + from django.db import connections + cursor = connections[get_db_label('Reporting')].cursor() cfilter = "expiration is null" sql = 'select ri.id, x.client_id from ' + \ |