Skip to content

Commit

Permalink
Merge f6e0fa9 into 315a33c
Browse files Browse the repository at this point in the history
  • Loading branch information
doismellburning committed Feb 22, 2015
2 parents 315a33c + f6e0fa9 commit 2d3306a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

* Python 3.2 and 3.4 support
* Improved test suite - 100% coverage + flake8
* Multiple database support - `TEST_DATABASE_URL` will create `settings.DATABASES['test']`

## 1.2

Expand Down
4 changes: 4 additions & 0 deletions README.rst
Expand Up @@ -132,6 +132,10 @@ Uses
`dj-database-url <https://github.com/kennethreitz/dj-database-url>`__ -
parses ``DATABASE_URL`` if it exists, otherwise falls back to in-memory sqlite.

Anything of the form ``FOO_DATABASE_URL`` will be parsed as
``DATABASES['foo']``, allowing you to configure multiple databases via the
environment.

``ALLOWED_HOSTS``
~~~~~~~~~~~~~~~~~

Expand Down
8 changes: 8 additions & 0 deletions django12factor/__init__.py
Expand Up @@ -73,6 +73,14 @@ def factorise(custom_settings=None):
'default': dj_database_url.config(default='sqlite://:memory:')
}

for (potential_database_url, value) in os.environ.iteritems():
_SUFFIX = "_DATABASE_URL"
_OFFSET = len(_SUFFIX)
if potential_database_url.endswith(_SUFFIX):
dbname = potential_database_url[:-_OFFSET].lower()
db = dj_database_url.parse(os.environ[potential_database_url])
settings['DATABASES'][dbname] = db

settings['DEBUG'] = getenv_bool('DEBUG')
if 'TEMPLATE_DEBUG' in os.environ:
settings['TEMPLATE_DEBUG'] = getenv_bool('TEMPLATE_DEBUG')
Expand Down
20 changes: 20 additions & 0 deletions tests/test_d12f.py
Expand Up @@ -65,3 +65,23 @@ def test_missing_custom_keys(self):
settings = d12f(['PRESENT', 'MISSING'])
self.assertEquals(present, settings['PRESENT'])
self.assertIsNone(settings['MISSING'])

def test_multiple_db_support(self):
DBNAME = "test"
DB_URL_NAME = "%s_DATABASE_URL" % DBNAME.upper()
e = {DB_URL_NAME: "postgres://username:password@host:1234/dbname"}

with debugenv(**e):
dbs = d12f()['DATABASES']
self.assertIn(
'sqlite',
dbs['default']['ENGINE'],
"Failed to load default DATABASE"
)
self.assertIn(
DBNAME,
dbs,
"Failed to parse a database called '%s' from the environment "
"variable %s" % (DBNAME, DB_URL_NAME)
)
self.assertIn('postgres', dbs[DBNAME]['ENGINE'])

0 comments on commit 2d3306a

Please sign in to comment.