Merge pull request #21 from mattack108/master
Allow env variable for database connection
This commit is contained in:
commit
3860b76dcf
@ -1,3 +1,4 @@
|
||||
import os
|
||||
# shut up useless SA warning:
|
||||
import warnings
|
||||
warnings.filterwarnings(
|
||||
@ -10,10 +11,11 @@ from dataset.freeze.app import freeze
|
||||
__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
|
||||
an instance of :py:class:`Database <dataset.Database>`. Set *reflectMetadata* to False if you
|
||||
Opens a new connection to a database. *url* can be any valid `SQLAlchemy engine URL`_.
|
||||
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
|
||||
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
|
||||
"""
|
||||
url = os.environ.get('DATABASE_URL', url)
|
||||
return Database(url, reflectMetadata)
|
||||
|
||||
@ -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
|
||||
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
|
||||
the database bindings to support that database. SQLite is included in
|
||||
the Python core, but PostgreSQL requires ``psycopg2`` to be installed.
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import os
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
|
||||
@ -9,11 +10,19 @@ from sample_data import TEST_DATA
|
||||
class DatabaseTestCase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.db = connect('sqlite:///:memory:')
|
||||
os.environ['DATABASE_URL'] = 'sqlite:///:memory:'
|
||||
self.db = connect()
|
||||
self.tbl = self.db['weather']
|
||||
for row in TEST_DATA:
|
||||
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):
|
||||
assert self.db.tables == ['weather'], self.db.tables
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user