bag.sqlalchemy.context module

Convenient, encapsulated SQLALchemy initialization.

Usage:

from bag.sqlalchemy.context import SAContext

sa = SAContext()  # you can provide create_engine's args here
# Now define your models with sa.metadata and sa.base

# At runtime:
# Add a working engine:
sa.create_engine('sqlite:///db.sqlite3', echo=False)
# or...
sa.use_memory()  # This one immediately creates the tables.

# Now use it:
sa.drop_tables().create_tables()
session = sa.Session()
# Use that session...
session.commit()

# You can also create a copy of sa, bound to another engine:
sa2 = sa.clone('sqlite://')
class bag.sqlalchemy.context.SAContext(base=None, base_class=None, metadata=None, session_extensions=None, zope_transaction=False, *args, **k)[source]

Bases: object

Provide convenient and encapsulated SQLAlchemy initialization.

Session
base
clone(**k)[source]

Copy this object. If keyword args, create another engine.

create_engine(dburi, **k)[source]
create_tables(tables=None)[source]

Create tables.

dburi
drop_tables(tables=None)[source]

Drop tables.

engine
metadata
session_extensions
ss

Return a (memoized) scoped session.

This is created only when first used and then stored.

subtransaction(fn)[source]

Decorator that encloses the decorated function in a subtransaction.

Your system must use our ss scoped session and it does not need to call commit() on the session.

tables_in(context)[source]

Return a list containing the tables in the passed context.

context may be a dictionary or a module:

tables = sa.tables_in(globals())
transaction(fn)[source]

Decorator that encloses the decorated function in a transaction.

Your system must use our ss scoped session and it does not need to call commit() on the session.

transient(fn)[source]

Decorator. Create a subtransaction which is always rewinded.

It is recommended that you apply this decorator to each of your integrated tests; then you only need to create the tables once, instead of once per test, because nothing ever gets persisted. This makes tests run faster.

use_memory(tables=None, **k)[source]