Connection & Registry¶
Functions for managing database connections and the global model registry. connect() registers a (optionally named) connection pool; reset_engine() tears everything down; the registry helpers control schema creation and the identity map. Sessionized routing is exposed via ferro.engines.session(name) / ferro.Session. See the Connections & Databases guide.
Session
dataclass
¶
Source code in src/ferro/session.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
Attributes¶
connection_name = None
class-attribute
instance-attribute
¶
session_id = None
class-attribute
instance-attribute
¶
Functions¶
__aenter__()
async
¶
Source code in src/ferro/session.py
__aexit__(exc_type, exc, tb)
async
¶
close()
async
¶
Close this session and release its runtime state.
Safe to call from a different asyncio context than __aenter__.
Repeated calls are no-ops.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the ambient session in this asyncio context does not match this handle (same-context lifecycle misuse), or if session-scoped transactions are still open. |
Source code in src/ferro/session.py
query(model_cls)
¶
__init__(connection_name=None, session_id=None, _token=None, _enter_context=None, _enter_task=None, _close_lock=None)
¶
connect(url, auto_migrate=False, name=None, default=False, pool=None, *, identity_map=True, migrate_updates=False, migrate_destructive=False)
async
¶
Establish a connection to the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The database connection string (e.g., "sqlite:example.db?mode=rwc"). |
required |
auto_migrate
|
bool
|
If True, automatically create tables for all registered models.
Existing tables are left untouched unless |
False
|
name
|
str | None
|
Optional connection name. Omitted connections register as "default". |
None
|
default
|
bool
|
If True, make this named connection the default for unqualified operations. |
False
|
pool
|
PoolConfig | None
|
Optional per-connection pool configuration. |
None
|
identity_map
|
bool
|
If True (default), keep a per-connection identity map so the same primary
key maps to a single Python instance. If False, each load returns fresh instances and
the map is not consulted (lower memory use; no |
True
|
migrate_updates
|
bool
|
If True, additionally update existing tables to match the
registered models. Implies
After any schema change, the connection pool is refreshed so no cached statement can observe the pre-migration schema. |
False
|
migrate_destructive
|
bool
|
If True, additionally drop live columns that no
longer exist on the model (never whole tables). Implies
|
False
|
For schema changes beyond these (renames, primary-key changes, complex
transforms), use the Alembic bridge — see docs/guide/migrations.md.
Source code in src/ferro/__init__.py
PoolConfig
¶
Bases: BaseModel
Connection pool settings for a named Ferro connection.
Source code in src/ferro/__init__.py
set_default_connection(name)
¶
Source code in src/ferro/_core.pyi
create_tables(using=None)
async
¶
Source code in src/ferro/_core.pyi
migrate(using=None, updates=True, destructive=False)
async
¶
Run the auto-migrate pass against a connected engine.
Creates missing tables, then (with updates, the default) adds missing
model columns to existing tables and reconciles type/nullability drift on
Postgres; with destructive it also drops live columns no longer on the
model. destructive implies updates. The pool is refreshed after any
DDL so no cached statement observes the pre-migration schema.