Skip to content

Models

rn_forge.sqlalchemy.models

Declarative base, UTC timestamps and the audit and version mixins.

NAMING_CONVENTION = {'ix': 'ix_%(column_0_label)s', 'uq': 'uq_%(table_name)s_%(column_0_name)s', 'ck': 'ck_%(table_name)s_%(constraint_name)s', 'fk': 'fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s', 'pk': 'pk_%(table_name)s'} module-attribute

Constraint names Alembic can address in a migration.

AuditMixin

AIP-148 timestamps and the actors behind them.

create_time and update_time default to the current UTC time, and update_time is refreshed on every ORM or Core update. created_by and updated_by are set by the caller; :func:rn_forge.sqlalchemy.upsert sets all four.

Base

Bases: DeclarativeBase

Declarative base whose metadata carries :data:NAMING_CONVENTION.

SoftDeleteMixin

A nullable, indexed delete_time; None while the row is live (AIP-164).

Pair it with :class:AuditMixin for updated_by. Nothing filters queries implicitly: select live rows with :func:live.

UTCDateTime

Bases: TypeDecorator[datetime]

A timezone-aware datetime stored as UTC on every dialect.

Writing a naive value raises ValueError. Reading returns an aware UTC value, including from SQLite, which stores no zone.

VersionMixin

A version counter, starting at 1, for :func:update_versioned.

live(stmt, model, *, show_deleted=False)

Return stmt restricted to live rows of model.

Parameters:

Name Type Description Default
stmt Select[T]

The select to restrict.

required
model type[SoftDeleteMixin]

A mapped class using :class:SoftDeleteMixin.

required
show_deleted bool

Return stmt unchanged, soft-deleted rows included.

False

soft_delete(obj, *, actor, now=None)

Mark obj soft-deleted at now (default: the current UTC time).

Sets delete_time and updated_by, and update_time on an :class:AuditMixin row. Increments version on a :class:VersionMixin row. Does not flush or commit.

undelete(obj, *, actor, now=None)

Clear obj's delete_time, recording actor and now.

Sets updated_by, update_time (an :class:AuditMixin row) and version (a :class:VersionMixin row) as :func:soft_delete does. Does not flush or commit.