API Reference¶
Mixins¶
- class mortar_sqlalchemy.Common¶
This mixin provides a common set of functionality that is slightly opinionated.
- __tablename__¶
The table name is, by default, the snake-case version of the class name.
- class mortar_sqlalchemy.Temporal(**kw)¶
This mixin takes advantage of the Postgres range types in order to help store tabular information that changes over time.
- __init__(period: Range[datetime] = None, value_from: datetime = None, value_to: datetime = None, ...)¶
This behaves the same as the normal constructor for a mapped object, but the
periodmay alternatively be set by passing either or both ofvalue_fromandvalue_to.
- key_columns: List[str]¶
The names of the columns that uniquely identify this row. In a non-temporal model, this would be the table’s primary key columns.
- value_columns: List[str] = None¶
The columns to consider as “values” for this type of object, by default this is all columns that are not
key_columnsexcludingidandperiod.
- exclude_constraint: bool = True¶
Controls whether an exclude constraint is generated for this table, such that accidental overlaps of data are prevented.
- id: Mapped[int] = Column(None, Integer(), table=None, primary_key=True, nullable=False)¶
The identifier for this row, which is an automatically generated integer that is unique throughout the whole table.
- period: Mapped[Range[datetime]] = Column(None, TSRANGE(), table=None, nullable=False)¶
The
Columnspecifying the period represented by this row. When being set, this should be aRange[datetime]. When being retrieved, it will likewise be aRange[datetime].
- property value_tuple: tuple[..., ...]¶
Return the tuple representing the
valueof this object. By default, this is constructed from the attributes for allvalue_columns.
- overlaps(session)¶
Returns a query for all objects that overlap with this one. This means that they are valid for the an overlapping
periodand share the samekey.
- set_for_period(session, coalesce=True, set_logging=20, change_logging=30, unchanged_logging=10)¶
Set the temporal value using the
valuesinselffor the keys specified inselfover the period specified inself.selfshould not be attached to any session; the session to the database in which changes should be made should be passed assession.It is assumed that the bounds of periods in the table are
[).If
coalesceis True, then where values are the same over a period affected by this operation, they will be merged into one database row.The case where period is open ended going forwards is special cased such that if it would replace future records that have a different value, it is instead closed at the point in time where the period of the earliest overlapping record ends.
Testing¶
- mortar_sqlalchemy.testing.drop_tables(connection: Connection) None¶
Drop alltables that can bereflectedfrom the supplied connection.
- mortar_sqlalchemy.testing.connection_in_transaction(url: str = None) Iterable[Connection]¶
Create an
engineusing the supplied url or, in not specified, the contents of theDB_URLenvironment variable.The context return is a
Connectionthat is in an activetransactionthat will berolled backwhen exiting the context.
- mortar_sqlalchemy.testing.create_tables_and_session(connection: Connection, base: DeclarativeBase) Iterable[Session]¶
Create all the tables found in the
MetaDataof the suppliedDeclarativeBase.The context returned is a
Sessionjoined to the transaction open on the supplied connection using the create_savepoint mode as described in the pattern Joining a Session into an External Transaction (such as for test suites).