type_bridge.migration¶
migration
¶
Read-only archive migration recovery and introspection surfaces.
Canonical Split-YAML workspace migrations own all new planning and writes. The Python package retains only frozen-history loading, state inspection, sidecar conversion, and schema introspection needed for one-way adoption.
MIGRATION_STATE_SCHEMA
module-attribute
¶
MIGRATION_STATE_SCHEMA = _label_projection(migration_state_schema())
Immutable labels for all schema objects owned by TypeBridge migration state.
SchemaConflictError
¶
Bases: Exception
A retained conflict diagnostic for existing compatibility callers.
Source code in type_bridge/migration/exceptions.py
has_breaking_changes
¶
Report whether the supplied historical diff has breaking members.
Source code in type_bridge/migration/exceptions.py
SchemaValidationError
¶
Bases: Exception
A retained schema-validation diagnostic.
IntrospectedAttribute
dataclass
¶
IntrospectedAttribute(name, value_type, parent_type=None, is_abstract=False, is_independent=False, regex=None, allowed_values=None, range=None, doc=None, meta=dict())
An attribute type from the database schema.
IntrospectedEntity
dataclass
¶
An entity type from the database schema.
IntrospectedOwnership
dataclass
¶
An ownership relationship between a type and an attribute.
IntrospectedRelation
dataclass
¶
A relation type from the database schema.
IntrospectedRole
dataclass
¶
A role in a relation.
IntrospectedSchema
dataclass
¶
Complete introspected schema from TypeDB database.
This is a database-centric view of the schema that can be compared against Python model definitions.
is_empty
¶
Check if the schema is empty (no custom types).
Source code in type_bridge/migration/introspection.py
get_entity_names
¶
get_relation_names
¶
get_attribute_names
¶
get_ownerships_for
¶
Get all ownerships for a specific owner type.
from_rust_schema_info
classmethod
¶
Build the compatibility DTO from Rust SchemaInfo live introspection.
Source code in type_bridge/migration/introspection.py
to_rust_schema_info
¶
Serialize introspected database schema to the Rust SchemaInfo dict shape.
Source code in type_bridge/migration/introspection.py
SchemaIntrospector
¶
Introspects TypeDB database schema.
Queries the database to discover all types, attributes, ownerships, and relations defined in the schema.
Example
introspector = SchemaIntrospector(db) schema = introspector.introspect()
print(f"Found {len(schema.entities)} entities") print(f"Found {len(schema.relations)} relations") print(f"Found {len(schema.attributes)} attributes")
Initialize introspector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Database
|
Database connection |
required |
Source code in type_bridge/migration/introspection.py
introspect_for_models
¶
Introspect database schema for specific model types.
This is the TypeDB 3.x compatible approach that checks each model type individually instead of enumerating all types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
models
|
list[type[_QueryEntity] | type[_QueryRelation]]
|
List of model classes to check |
required |
Returns:
| Type | Description |
|---|---|
IntrospectedSchema
|
IntrospectedSchema with info about existing types |
Source code in type_bridge/migration/introspection.py
introspect
¶
Query TypeDB schema and return structured info.
Returns:
| Type | Description |
|---|---|
IntrospectedSchema
|
IntrospectedSchema with all discovered types |
Source code in type_bridge/migration/introspection.py
LoadedMigration
dataclass
¶
LoadedMigration(migration, path, checksum, *, execution_spec=None, source_sha256=None, execution_sidecar_sha256=None, execution_sidecar_json=None, execution_sidecar_entry=None)
A migration loaded from a file.
Attributes:
| Name | Type | Description |
|---|---|---|
migration |
_ArchivedMigration
|
The Migration instance |
path |
Path
|
Path to the migration file |
checksum |
str
|
SHA256 hash of file content (first 16 chars) |
execution_spec |
dict[str, Any] | None
|
Optional pre-lowered MigrationSpec dict loaded from
the JSON sidecar. Present only for generated migrations that carry
a |
MigrationLoader
¶
MigrationLoader(migrations_dir, *, use_sidecars=True, adoption_limits=False, directory_authority=None, adoption_import_dir=None)
Loads migration files from a directory.
Migration files must follow the naming pattern: NNNN_*.py where NNNN is a 4-digit number (e.g., 0001_initial.py, 0002_add_company.py)
Example
loader = MigrationLoader(Path("migrations")) migrations = loader.discover()
for loaded in migrations: print(f"{loaded.migration.name}: {loaded.checksum}")
Initialize loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
migrations_dir
|
Path
|
Directory containing migration files |
required |
use_sidecars
|
bool
|
Prefer checked execution sidecars when present. Adoption metadata generation preserves this released behavior: a retained valid sidecar is authoritative and prevents Python execution; only a source with no sidecar is imported. |
True
|
adoption_limits
|
bool
|
Apply the bounded, no-follow reader used only by
adoption metadata generation. The released loader path stays
byte-for-byte compatible with its unbounded |
False
|
directory_authority
|
AdoptionDirectoryAuthority | None
|
Retained adoption-only directory capability. When absent, an adoption-limited discovery retains one for the duration of that discovery. Ordinary V1 discovery ignores it. |
None
|
adoption_import_dir
|
Path | None
|
Private retained mirror of |
None
|
Source code in type_bridge/migration/loader.py
ignored_sources
property
¶
Return ignored-source evidence captured by the last discovery.
This is populated only by the adoption-limited trusted-reader path. Ordinary released discovery retains its historical return contract.
adoption_entries
property
¶
Return captured source revisions from the last adoption discovery.
discover
¶
Discover all migration files in order.
Returns:
| Type | Description |
|---|---|
list[LoadedMigration]
|
List of loaded migrations, sorted by filename |
Source code in type_bridge/migration/loader.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
get_by_name
¶
Get a specific migration by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Migration name (e.g., "0001_initial") |
required |
Returns:
| Type | Description |
|---|---|
LoadedMigration | None
|
LoadedMigration or None if not found |
Source code in type_bridge/migration/loader.py
get_by_number
¶
Get a specific migration by number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number
|
int
|
Migration number (e.g., 1 for 0001_initial) |
required |
Returns:
| Type | Description |
|---|---|
LoadedMigration | None
|
LoadedMigration or None if not found |
Source code in type_bridge/migration/loader.py
get_next_number
¶
Get the next available migration number.
Returns:
| Type | Description |
|---|---|
int
|
Next migration number (1 if no migrations exist) |
Source code in type_bridge/migration/loader.py
validate_dependencies
¶
Validate that all migration dependencies are satisfied.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of error messages (empty if valid) |
Source code in type_bridge/migration/loader.py
MigrationLoadError
¶
Bases: Exception
Error loading a migration file.
SidecarConversionError
¶
Bases: Exception
A migration history cannot produce trustworthy archival artifacts.
blockers maps each migration or artifact identity to the failed
trust/integrity condition. Preflight failures write nothing. A failure
after the conversion journal is published can leave resumable immutable
artifacts, and native adoption rejects the history until a retry clears
the journal.
Source code in type_bridge/migration/sidecar.py
MigrationRecord
dataclass
¶
One applied record read from the frozen ledger.
MigrationRunRecord
dataclass
¶
MigrationRunRecord(run_id, app_label, name, checksum, direction, status, started_at, finished_at=None, error=None, executor_ip=None, executor_mac=None)
One historical migration execution record.
MigrationState
dataclass
¶
In-memory projection of applied archived migration records.
MigrationStateManager
¶
MigrationStateSchema
dataclass
¶
Immutable label projection of the canonical migration-state schema.
Role labels are qualified as relation:role so an application relation
can use the same unqualified role name without being classified as
TypeBridge infrastructure.
generate_sidecars
¶
Generate checked JSON sidecars for every py-only migration in a directory.
Discovers the history through the released loader's frozen Python import
path, lowers each executable migration with the released execution
lowering, and binds every migration-shaped source to its .py checksum.
Returns:
| Type | Description |
|---|---|
list[Path]
|
Paths of the adoption records and executable sidecars written, in |
list[Path]
|
source order. Empty when every required artifact already exists. |
Raises:
| Type | Description |
|---|---|
SidecarConversionError
|
When the graph is invalid, a schema-affecting migration lacks its exact immutable snapshot, or a schema-neutral RunPython/no-op migration cannot inherit one converged parent authority. Nothing is written in that case. |
MigrationLoadError
|
When discovery itself fails (unreadable file, stale existing sidecar, broken migration module). |
Source code in type_bridge/migration/sidecar.py
is_migration_state_type
¶
Return whether label is a TypeBridge migration-state schema object.
Role labels must use the qualified relation:role form.
Source code in type_bridge/migration/state_schema.py
migration_state_schema
¶
Return the full canonical migration-state schema descriptor from Rust.
without_migration_state_schema
¶
Return a copy of schema without TypeBridge migration-state objects.
Source code in type_bridge/migration/state_schema.py
type_exists
¶
Check if a type exists in the database schema.
Uses a simple query to check if the type name is valid in the schema. If the type doesn't exist, the query will raise an error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Database
|
Database connection |
required |
type_name
|
str
|
Name of the type to check (entity, relation, or attribute) |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if type exists in schema, False otherwise |
Example
type_exists(db, "person") True type_exists(db, "nonexistent") False