Merge pull request #21 from mattack108/master

Allow env variable for database connection
This commit is contained in:
Friedrich Lindenberg 2013-07-15 00:35:04 -07:00
commit 3860b76dcf
3 changed files with 21 additions and 4 deletions

View File

@ -1,3 +1,4 @@
import os
# shut up useless SA warning: # shut up useless SA warning:
import warnings import warnings
warnings.filterwarnings( warnings.filterwarnings(
@ -10,10 +11,11 @@ from dataset.freeze.app import freeze
__all__ = ['Database', 'Table', 'freeze', 'connect'] __all__ = ['Database', 'Table', 'freeze', 'connect']
def connect(url, reflectMetadata=True): def connect(url=None, reflectMetadata=True):
""" """
Opens a new connection to a database. *url* can be any valid `SQLAlchemy engine URL`_. Returns Opens a new connection to a database. *url* can be any valid `SQLAlchemy engine URL`_.
an instance of :py:class:`Database <dataset.Database>`. Set *reflectMetadata* to False if you If *url* is not defined it will try to use *DATABASE_URL* from environment variable.
Returns an instance of :py:class:`Database <dataset.Database>`. Set *reflectMetadata* to False if you
don't want the entire database schema to be pre-loaded. This significantly speeds up don't want the entire database schema to be pre-loaded. This significantly speeds up
connecting to large databases with lots of tables. connecting to large databases with lots of tables.
:: ::
@ -22,4 +24,5 @@ def connect(url, reflectMetadata=True):
.. _SQLAlchemy Engine URL: http://docs.sqlalchemy.org/en/latest/core/engines.html#sqlalchemy.create_engine .. _SQLAlchemy Engine URL: http://docs.sqlalchemy.org/en/latest/core/engines.html#sqlalchemy.create_engine
""" """
url = os.environ.get('DATABASE_URL', url)
return Database(url, reflectMetadata) return Database(url, reflectMetadata)

View File

@ -23,6 +23,11 @@ To connect to a database you need to identify it by its `URL <http://docs.sqlalc
# connecting to a PostgreSQL database # connecting to a PostgreSQL database
db = dataset.connect('postgresql://scott:tiger@localhost:5432/mydatabase') db = dataset.connect('postgresql://scott:tiger@localhost:5432/mydatabase')
It is also possible to define the `URL` as an environment variable called `DATABASE_URL`
so you can initialize database connection without explicitly passing an `URL`::
db = dataset.connect()
Depending on which database you're using, you may also have to install Depending on which database you're using, you may also have to install
the database bindings to support that database. SQLite is included in the database bindings to support that database. SQLite is included in
the Python core, but PostgreSQL requires ``psycopg2`` to be installed. the Python core, but PostgreSQL requires ``psycopg2`` to be installed.

View File

@ -1,3 +1,4 @@
import os
import unittest import unittest
from datetime import datetime from datetime import datetime
@ -9,11 +10,19 @@ from sample_data import TEST_DATA
class DatabaseTestCase(unittest.TestCase): class DatabaseTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.db = connect('sqlite:///:memory:') os.environ['DATABASE_URL'] = 'sqlite:///:memory:'
self.db = connect()
self.tbl = self.db['weather'] self.tbl = self.db['weather']
for row in TEST_DATA: for row in TEST_DATA:
self.tbl.insert(row) self.tbl.insert(row)
def tearDown(self):
# ensure env variable was unset
del os.environ['DATABASE_URL']
def test_valid_database_url(self):
assert self.db.url, os.environ['DATABASE_URL']
def test_tables(self): def test_tables(self):
assert self.db.tables == ['weather'], self.db.tables assert self.db.tables == ['weather'], self.db.tables