add poolclass awareness to engine_from_config()
This commit is contained in:
parent
9bb5078cb0
commit
b0bb83514c
2 changed files with 39 additions and 11 deletions
|
@ -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']
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue