diff --git a/edbob/db/__init__.py b/edbob/db/__init__.py index c3eac2f..74b01d2 100644 --- a/edbob/db/__init__.py +++ b/edbob/db/__init__.py @@ -28,11 +28,12 @@ from __future__ import absolute_import -from sqlalchemy import engine_from_config, MetaData +from sqlalchemy import MetaData from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base import edbob +from edbob.sqlalchemy import engine_from_config __all__ = ['engines', 'engine', 'Session', 'get_setting', 'save_setting'] diff --git a/edbob/sqlalchemy.py b/edbob/sqlalchemy.py index ffa7642..ddf1eb6 100644 --- a/edbob/sqlalchemy.py +++ b/edbob/sqlalchemy.py @@ -29,14 +29,51 @@ from __future__ import absolute_import from sqlalchemy import Table, Column, String +from sqlalchemy import engine_from_config as _engine_from_config from edbob.core import get_uuid +from edbob.modules import load_spec from edbob.time import utc_time __all__ = ['getset_factory', 'table_with_uuid', 'current_time'] +def current_time(context): + """ + This function may be provided to the ``default`` parameter of a + :class:`sqlalchemy.Column` class definition. Doing so will ensure the + column's default value will be the current time in UTC. + """ + + return utc_time(naive=True) + + +def engine_from_config(config, prefix='sqlalchemy.', **kwargs): + """ + Slightly enhanced version of the :func:`sqlalchemy.engine_from_config()` + function. This version is aware of the ``poolclass`` configuration + parameter, and will coerce it via :func:`edbob.load_spec()`. + + Note that if a pool class is specified, the class should be represented + using the "spec" format and *not* pure dotted path notation, e.g.: + + Correct:: + + [edbob.db] + default.poolclass = sqlqlchemy.pool:NullPool + + Incorrect:: + + [edbob.db] + default.poolclass = sqlalchemy.pool.NullPool + """ + + if config.has_key(prefix + 'poolclass'): + config[prefix + 'poolclass'] = load_spec(config[prefix + 'poolclass']) + return _engine_from_config(config, prefix=prefix, **kwargs) + + def getset_factory(collection_class, proxy): """ Get/set factory for SQLAlchemy association proxy attributes. @@ -77,13 +114,3 @@ def table_with_uuid(name, metadata, *args, **kwargs): return Table(name, metadata, Column('uuid', String(32), primary_key=True, default=get_uuid), *args, **kwargs) - - -def current_time(context): - """ - This function may be provided to the ``default`` parameter of a - :class:`sqlalchemy.Column` class definition. Doing so will ensure the - column's default value will be the current time in UTC. - """ - - return utc_time(naive=True)