The Database Utils Module (tendril.utils.db
)¶
This module provides utilities to deal with Tendril’s Database. While the
actual functionality is provided by the sqlalchemy
package, the
contents of this utility module simplify and specify the application code’s
interaction with sqlalchemy
Module Contents
-
tendril.utils.db.
init_db_engine
()[source]¶ Initializes the database engine and binds it to the Database URI defined by the
tendril.utils.config
module.This function is called within the module and an engine is readily available in the module variable
tendril.utils.db.engine
. Application code should not have to create a new engine for normal use cases.
-
tendril.utils.db.
engine
= Engine(postgresql://tendril:***@localhost:5432/tendril_qtpl)¶ The
sqlalchemy.Engine
object
-
tendril.utils.db.
Session
= sessionmaker(class_='Session',autoflush=True, bind=Engine(postgresql://tendril:***@localhost:5432/tendril_qtpl), autocommit=False, expire_on_commit=False)[source]¶ The
sqlalchemy.sessionmaker
bound to the database engine
-
tendril.utils.db.
get_session
(*args, **kwds)[source]¶ Application executable code will typically only have to interact with this
contextmanager
or thewith_db()
decorator. It should use this to create a database session, perform its tasks, whatever they may be, within this context, and then exit the context.If any Exception is thrown, the session is rolled back completely. If no Exception is thrown or Exceptions are handled by the application code within the context, the session is committed when the context exits.
See also
-
tendril.utils.db.
with_db
(func)[source]¶ Application executable code will typically only have to interact with this function or the
get_session()
contextmanager
. Thewith_db()
decorator is intended to decorate functions which interact primarily with the db.Such a function would accept only keyword arguments, one of which is
session
, which can be a database session (created byget_session()
) provided by the caller. Ifsession
isNone
, this decorator creates a new session and calls the decorated function using it.Any function which returns objects that still need to be bound to a db session should be called with a valid session, if you intend to do anything with the returned objects. They will still execute without exception if no session is provided, but the returned value may not be useful.
See also
-
tendril.utils.db.
DeclBase
¶ The
sqlalchemy
declarative base for all Models in Tendrilalias of
Base
-
class
tendril.utils.db.
BaseMixin
[source]¶ Bases:
object
This Mixin can / should be used (by inheriting from) by all Model classes defined by application code. It defines the
__tablename__
attribute of the Model class to the name of the class and creates a Primary Key Column named id in the table for the Model.-
id
= Column(None, Integer(), table=None, primary_key=True, nullable=False)¶
-
-
class
tendril.utils.db.
CreatedTimestampMixin
[source]¶ Bases:
object
This Mixin can be used by any Models which require a creation timestamp to be created. It adds a column named
created_at
, which defaults to the time at which the object is created.-
created_at
= Column(None, ArrowType(), table=None, default=ColumnDefault(<function utcnow>))¶
-
-
class
tendril.utils.db.
UpdateTimestampMixin
[source]¶ Bases:
object
This Mixin can be used by any Models which require an update timestamp to be created. It adds a column named
updated_at
, which defaults to the time at which the object is updated.-
updated_at
= Column(None, ArrowType(), table=None, onupdate=ColumnDefault(<function utcnow>))¶
-
-
class
tendril.utils.db.
TimestampMixin
[source]¶ Bases:
tendril.utils.db.CreatedTimestampMixin
,tendril.utils.db.UpdateTimestampMixin
This Mixin can be used for any Models which contain data that has time dependence to any degree. It adds both the
updated_at
andcreated_at
columns.
-
tendril.utils.db.
get_metadata
()[source]¶ This function populates the database metadata with all the models used by tendril.
-
tendril.utils.db.
commit_metadata
()[source]¶ This function commits all metadata to the table. This function should be run after importing all the Model classes, and it will create the tables in the database.
-
tendril.utils.db.
metadata
= MetaData(bind=None)¶ The full Tendril database/sqlalchemy metadata. Rendered by
sqlalchemyviz
into the following ER diagram.