save point (see note)

Added initial database schema and ability to install it, added init-db command
to pyramid scaffold.
This commit is contained in:
Lance Edgar 2012-03-24 12:15:11 -05:00
parent 925cd30b96
commit 8ceb98baf4
29 changed files with 1116 additions and 191 deletions

View file

@ -32,52 +32,77 @@ from sqlalchemy.orm import sessionmaker
import edbob
__all__ = ['engines', 'engine', 'Session', 'metadata',
'get_setting', 'save_setting']
inited = False
engines = None
engine = None
Session = sessionmaker()
metadata = None
def init():
def init(config):
"""
Called whenever ``'edbob.db'`` is configured to be auto-initialized.
Initializes the database connection(s); called by :func:`edbob.init()` if
config includes something like::
This function is responsible for establishing the primary database engine
(a ``sqlalchemy.Engine`` instance, read from config), and extending the
root ``edbob`` namespace with the ORM classes (``Person``, ``User``, etc.),
as well as a few other things, e.g. ``engine``, ``Session`` and
``metadata``.
.. highlight:: ini
In addition to this, if a connection to the primary database can be
obtained, it will be consulted to see which extensions are active within
it. If any are found, edbob's ORM will be extended in-place accordingly.
[edbob]
init = ['edbob.db']
[edbob.db]
sqlalchemy.urls = {
'default': 'postgresql://user:pass@localhost/edbob,
}
This function reads connection info from ``config`` and builds a dictionary
or :class:`sqlalchemy.Engine` instances accordingly. It also extends the
root ``edbob`` namespace with the ORM classes (:class:`edbob.Person`,
:class:`edbob.User`, etc.), as well as a few other things
(e.g. :attr:`edbob.engine`, :attr:`edbob.Session`, :attr:`edbob.metadata`).
"""
config = edbob.config.get_dict('edbob.db')
engine = engine_from_config(config)
edbob.graft(edbob, locals(), 'engine')
Session.configure(bind=engine)
edbob.graft(edbob, globals(), 'Session')
import edbob.db
from edbob.db import classes
from edbob.db import enum
from edbob.db.model import get_metadata
metadata = get_metadata(bind=engine)
edbob.graft(edbob, locals(), 'metadata')
from edbob.db.mappers import make_mappers
make_mappers(metadata)
from edbob.db.extensions import extend_framework
from edbob.db.ext import extend_framework
global inited, engines, engine, metadata
keys = config.get('edbob.db', 'sqlalchemy.keys')
if keys:
keys = keys.split()
else:
keys = ['default']
engines = {}
cfg = config.get_dict('edbob.db')
for key in keys:
try:
engines[key] = engine_from_config(cfg, 'sqlalchemy.%s.' % key)
except KeyError:
if key == 'default':
try:
engines[key] = engine_from_config(cfg)
except KeyError:
pass
engine = engines.get('default')
if engine:
Session.configure(bind=engine)
metadata = get_metadata(bind=engine)
make_mappers(metadata)
extend_framework()
# Note that we extend the framework before we graft the 'classes' module
# contents, since extensions may graft things to that module.
import edbob.db.classes as classes
edbob.graft(edbob, edbob.db)
edbob.graft(edbob, classes)
# Same goes for the enum module.
import edbob.db.enum as enum
edbob.graft(edbob, enum)
# Add settings functions.
edbob.graft(edbob, globals(), ('get_setting', 'save_setting'))
inited = True
def get_setting(name, session=None):