Migration Hooks¶
Here's the reference information for the included migration hooks classes, with all their parameters, attributes and methods.
You can import these classes directly from pyrmute:
pyrmute.MigrationHook
¶
Base class for migration hooks.
Hooks are read-only observers that allow you to inject custom behavior before, after, or on error during migrations. Useful for logging, monitoring, metrics, validation, or auditing.
Important
Hook methods receive read-only views of data and should NOT modify it. Data transformation should only happen in migration functions.
Example
class LoggingHook(MigrationHook):
def before_migrate(
self,
name: str,
from_version: ModelVersion,
to_version: ModelVersion,
data: Mapping[str, Any],
) -> None:
logger.info(f"Migrating {name} from {from_version} to {to_version}")
def after_migrate(
self,
name: str,
from_version: ModelVersion,
to_version: ModelVersion,
original_data: Mapping[str, Any],
migrated_data: Mapping[str, Any],
) -> None:
logger.info(f"Successfully migrated {name}")
def on_error(
self,
name: str,
from_version: ModelVersion,
to_version: ModelVersion,
data: Mapping[str, Any],
error: Exception,
) -> None:
logger.error(f"Migration failed: {error}")
before_migrate
¶
Called before migration starts.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Model name being migrated.
TYPE:
|
from_version
|
Source version.
TYPE:
|
to_version
|
Target version.
TYPE:
|
data
|
Original data to be migrated (read-only view).
TYPE:
|
Warning
Do not modify the data parameter. Hooks are for observation only.
Source code in src/pyrmute/migration_hooks.py
after_migrate
¶
Called after successful migration.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Model name that was migrated.
TYPE:
|
from_version
|
Source version.
TYPE:
|
to_version
|
Target version.
TYPE:
|
original_data
|
Original input data (read-only view).
TYPE:
|
migrated_data
|
Result of migration (read-only view).
TYPE:
|
Warning
Do not modify the data parameters. Hooks are for observation only.
Source code in src/pyrmute/migration_hooks.py
on_error
¶
Called when migration fails.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Model name that failed to migrate.
TYPE:
|
from_version
|
Source version.
TYPE:
|
to_version
|
Target version.
TYPE:
|
data
|
Original data that failed to migrate (read-only view).
TYPE:
|
error
|
Exception that occurred.
TYPE:
|
Warning
Do not modify the data parameter. Hooks are for observation only.
Source code in src/pyrmute/migration_hooks.py
pyrmute.MetricsHook
¶
Bases: MigrationHook
Hook for collecting migration metrics.
Tracks migration counts, timing, and errors for monitoring purposes.
Example
Initialize metrics collection.
Source code in src/pyrmute/migration_hooks.py
success_rate
property
¶
Calculate overall success rate.
| RETURNS | DESCRIPTION |
|---|---|
float
|
Success rate as a float between 0 and 1. |
after_migrate
¶
Called after successful migration.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Model name that was migrated.
TYPE:
|
from_version
|
Source version.
TYPE:
|
to_version
|
Target version.
TYPE:
|
original_data
|
Original input data (read-only view).
TYPE:
|
migrated_data
|
Result of migration (read-only view).
TYPE:
|
Warning
Do not modify the data parameters. Hooks are for observation only.
Source code in src/pyrmute/migration_hooks.py
before_migrate
¶
Track migration attempt.
Source code in src/pyrmute/migration_hooks.py
on_error
¶
Track migration error.