pub struct MigrationEngine { /* private fields */ }Expand description
The migration engine that runs a chain of migration steps.
Steps are registered in order and form a linear chain. When data at version N needs to reach version M (where N < M), the engine runs steps N→N+1, N+1→N+2, …, M-1→M in sequence.
§Example
use crdt_migrate::{MigrationEngine, MigrationStep, MigrationError};
struct AddFieldMigration;
impl MigrationStep for AddFieldMigration {
fn source_version(&self) -> u32 { 1 }
fn target_version(&self) -> u32 { 2 }
fn migrate(&self, data: &[u8]) -> Result<Vec<u8>, MigrationError> {
// In real code: deserialize v1, create v2 with new fields, serialize
let mut result = data.to_vec();
result.extend_from_slice(b"|humidity=none");
Ok(result)
}
}
let mut engine = MigrationEngine::new(2); // current version = 2
engine.register(Box::new(AddFieldMigration));
let v1_data = b"temp=22.5";
let v2_data = engine.migrate_to_current(v1_data, 1).unwrap();
assert_eq!(v2_data, b"temp=22.5|humidity=none");Implementations§
Source§impl MigrationEngine
impl MigrationEngine
Sourcepub fn register(&mut self, step: Box<dyn MigrationStep>)
pub fn register(&mut self, step: Box<dyn MigrationStep>)
Register a migration step.
Sourcepub fn current_version(&self) -> u32
pub fn current_version(&self) -> u32
The current (target) schema version.
Sourcepub fn needs_migration(&self, data_version: u32) -> bool
pub fn needs_migration(&self, data_version: u32) -> bool
Check if data needs migration.
Sourcepub fn migrate_to_current(
&self,
data: &[u8],
from_version: u32,
) -> Result<Vec<u8>, MigrationError>
pub fn migrate_to_current( &self, data: &[u8], from_version: u32, ) -> Result<Vec<u8>, MigrationError>
Migrate data from from_version to current_version.
Runs the chain of steps sequentially. Each step receives the output of the previous step.
Sourcepub fn validate_chain(&self, min_version: u32) -> Result<(), MigrationError>
pub fn validate_chain(&self, min_version: u32) -> Result<(), MigrationError>
Validate that the migration chain is complete from min_version to current_version.
Sourcepub fn registered_steps(&self) -> Vec<(u32, u32)>
pub fn registered_steps(&self) -> Vec<(u32, u32)>
List all registered migration steps as (from, to) pairs.
Auto Trait Implementations§
impl Freeze for MigrationEngine
impl !RefUnwindSafe for MigrationEngine
impl Send for MigrationEngine
impl Sync for MigrationEngine
impl Unpin for MigrationEngine
impl !UnwindSafe for MigrationEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more