type_bridge.migration.executor¶
executor
¶
Migration executor for applying and rolling back migrations.
MigrationError
¶
Bases: Exception
Error during migration execution.
MigrationPlan
dataclass
¶
Plan for migration execution.
Attributes:
| Name | Type | Description |
|---|---|---|
to_apply |
list[LoadedMigration]
|
Migrations to apply (forward) |
to_rollback |
list[LoadedMigration]
|
Migrations to rollback (reverse) |
MigrationResult
dataclass
¶
Result of a migration operation.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Migration name |
action |
str
|
"applied" or "rolled_back" |
success |
bool
|
Whether the operation succeeded |
error |
str | None
|
Error message if failed |
MigrationExecutor
¶
Executes migrations against a TypeDB database.
Handles: - Applying pending migrations - Rolling back applied migrations - Previewing migration TypeQL - Listing migration status
Example
executor = MigrationExecutor(db, Path("migrations"))
Apply all pending migrations¶
results = executor.migrate()
Migrate to specific version¶
results = executor.migrate(target="0002_add_company")
Show migration status¶
status = executor.showmigrations() for name, is_applied in status: print(f"[{'X' if is_applied else ' '}] {name}")
Preview TypeQL¶
typeql = executor.sqlmigrate("0002_add_company") print(typeql)
Initialize executor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Database
|
Database connection |
required |
migrations_dir
|
Path
|
Directory containing migration files |
required |
dry_run
|
bool
|
If True, preview operations without executing |
False
|
Source code in type_bridge/migration/executor.py
migrate
¶
Apply pending migrations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str | None
|
Optional target migration name (e.g., "0002_add_company") If None, apply all pending migrations. If specified, migrate to that exact state (may rollback). |
None
|
Returns:
| Type | Description |
|---|---|
list[MigrationResult]
|
List of migration results |
Raises:
| Type | Description |
|---|---|
MigrationError
|
If migration fails |
Source code in type_bridge/migration/executor.py
showmigrations
¶
List all migrations with their applied status.
Returns:
| Type | Description |
|---|---|
list[tuple[str, bool]]
|
List of (migration_name, is_applied) tuples |
Source code in type_bridge/migration/executor.py
sqlmigrate
¶
Preview TypeQL for a migration without executing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
migration_name
|
str
|
Name of the migration |
required |
reverse
|
bool
|
If True, show rollback TypeQL |
False
|
Returns:
| Type | Description |
|---|---|
str
|
TypeQL string that would be executed |
Raises:
| Type | Description |
|---|---|
MigrationError
|
If migration not found or not reversible |
Source code in type_bridge/migration/executor.py
plan
¶
Get the migration plan without executing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str | None
|
Optional target migration name |
None
|
Returns:
| Type | Description |
|---|---|
MigrationPlan
|
MigrationPlan showing what would be applied/rolled back |