ushka.orm.reflect
Reads an existing database and writes Ushka models for it.
ushka db inspect uses this to adopt a schema that another ORM (Django,
SQLAlchemy, Prisma...) created. Reflection goes through SQLAlchemy's generic
inspector, so it works with every backend SQLAlchemy supports.
What the database cannot say matters as much as what it does: defaults,
auto_now and ON DELETE are often applied in Python by the old ORM and
never reach the schema. Every such gap is reported as a Finding and written
into the generated code as a TODO(ushka db inspect) comment. Guesses are
only ever suggestions: they are never applied silently.
In this module
class ColumnInfo | |
class ForeignKeyInfo | |
class IndexInfo | |
class TableInfo | |
class Finding | Something the database does not say, or a guess that needs a human. |
def reflect() | Reflects the tables of the database at url (an async URL). |
def table_from_model() | Describes a mapped Table the way reflect describes a database table. |
def kind_of() | The family of a SQLAlchemy type: what the Ushka helper for it would be. |
def same_kind() | True when two type families store the same thing (sqlite keeps a UUID as text). |
def parse_default() | Translates a server default into (mode, code). |
def class_name() | |
class Generator | Turns reflected tables into Ushka model source plus a list of Findings. |
def generate() | Returns (source, findings) for the reflected tables. |
class Divergence | |
def compare() | Lists where the models and the database disagree. |
class ColumnInfo
class ForeignKeyInfo
class IndexInfo
class TableInfo
class Finding
Something the database does not say, or a guess that needs a human.
where
def where() -> strdef reflect
async def reflect(
url: str,
schema: str | None = None,
only: list[str] | None = None,
) -> list[TableInfo]Reflects the tables of the database at url (an async URL).
Raises LookupError when a requested table does not exist.
def table_from_model
def table_from_model(table: sa.Table) -> TableInfoDescribes a mapped Table the way reflect describes a database table.
def kind_of
def kind_of(t: Any) -> strThe family of a SQLAlchemy type: what the Ushka helper for it would be.
def same_kind
def same_kind(a: str, b: str) -> boolTrue when two type families store the same thing (sqlite keeps a UUID as text).
def parse_default
def parse_default(sql: str, kind: str) -> tuple[str, str]Translates a server default into (mode, code).
mode is py (code is a Python literal), now / today (the
Ushka auto_now), uuid (uuid.uuid4), serial (an
auto-increment, nothing to map), none (DEFAULT NULL) or raw
(not understood: code is the original SQL, kept as server_default).
def class_name
def class_name(table: str) -> strclass Generator
Turns reflected tables into Ushka model source plus a list of Findings.
generate
def generate() -> strdef generate
def generate(tables: list[TableInfo]) -> tuple[str, list[Finding]]Returns (source, findings) for the reflected tables.
class Divergence
where
def where() -> strdef compare
def compare(
database: list[TableInfo],
models: list[TableInfo],
) -> list[Divergence]Lists where the models and the database disagree.
Checks tables, columns, nullability, type families and the ON DELETE / ON UPDATE of foreign keys. Tables that have no model are not reported.