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, use_transaction=False, *args, **k)[source]¶
Bases:
object
Provide convenient and encapsulated SQLAlchemy initialization.
- Session¶
- base¶
- dburi¶
- engine¶
- property metadata¶
- property scoped_session¶
Return a (memoized) scoped session.
This is created only when first used and then stored.
- subtransaction(fn)[source]¶
Enclose in a subtransaction a decorated function.
Your system must use our
ss
scoped session and it does not need to callcommit()
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]¶
Enclose a decorated function in a transaction.
Your system must use our
ss
scoped session and it does not need to callcommit()
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_transaction¶