pub struct Migrator { /* private fields */ }Expand description
The migration manager that orchestrates all migrations.
Implementations§
Source§impl Migrator
impl Migrator
Sourcepub fn define(entity: &str) -> MigrationPathBuilder<Start>
pub fn define(entity: &str) -> MigrationPathBuilder<Start>
Starts defining a migration path for an entity.
Sourcepub fn register<D>(
&mut self,
path: MigrationPath<D>,
) -> Result<(), MigrationError>
pub fn register<D>( &mut self, path: MigrationPath<D>, ) -> Result<(), MigrationError>
Registers a migration path with validation.
This method validates the migration path before registering it:
- Checks for circular migration paths
- Validates version ordering follows semver rules
§Errors
Returns an error if validation fails.
Sourcepub fn load_from<D, T>(
&self,
entity: &str,
data: T,
) -> Result<D, MigrationError>where
D: DeserializeOwned,
T: Serialize,
pub fn load_from<D, T>(
&self,
entity: &str,
data: T,
) -> Result<D, MigrationError>where
D: DeserializeOwned,
T: Serialize,
Loads and migrates data from any serde-compatible format.
This is the generic version that accepts any type implementing Serialize.
For JSON strings, use the convenience method load instead.
§Arguments
entity- The entity name used when registering the migration pathdata- Versioned data in any serde-compatible format (e.g.,toml::Value,serde_json::Value)
§Returns
The migrated data as the domain model type
§Errors
Returns an error if:
- The data cannot be converted to the internal format
- The entity is not registered
- A migration step fails
§Example
// Load from TOML
let toml_data: toml::Value = toml::from_str(toml_str)?;
let domain: TaskEntity = migrator.load_from("task", toml_data)?;
// Load from JSON Value
let json_data: serde_json::Value = serde_json::from_str(json_str)?;
let domain: TaskEntity = migrator.load_from("task", json_data)?;Sourcepub fn load<D: DeserializeOwned>(
&self,
entity: &str,
json: &str,
) -> Result<D, MigrationError>
pub fn load<D: DeserializeOwned>( &self, entity: &str, json: &str, ) -> Result<D, MigrationError>
Loads and migrates data from a JSON string.
This is a convenience method for the common case of loading from JSON.
For other formats, use load_from instead.
§Arguments
entity- The entity name used when registering the migration pathjson- A JSON string containing versioned data
§Returns
The migrated data as the domain model type
§Errors
Returns an error if:
- The JSON cannot be parsed
- The entity is not registered
- A migration step fails
§Example
let json = r#"{"version":"1.0.0","data":{"id":"task-1","title":"My Task"}}"#;
let domain: TaskEntity = migrator.load("task", json)?;Sourcepub fn save<T: Versioned + Serialize>(
&self,
data: T,
) -> Result<String, MigrationError>
pub fn save<T: Versioned + Serialize>( &self, data: T, ) -> Result<String, MigrationError>
Saves versioned data to a JSON string.
This method wraps the provided data with its version information and serializes
it to JSON format. The resulting JSON can later be loaded and migrated using
the load method.
§Arguments
data- The versioned data to save
§Returns
A JSON string with the format: {"version":"x.y.z","data":{...}}
§Errors
Returns SerializationError if the data cannot be serialized to JSON.
§Example
let task = TaskV1_0_0 {
id: "task-1".to_string(),
title: "My Task".to_string(),
};
let migrator = Migrator::new();
let json = migrator.save(task)?;
// json: {"version":"1.0.0","data":{"id":"task-1","title":"My Task"}}