Skip to main content

MigrationEngine

Struct MigrationEngine 

Source
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

Source

pub fn new(current_version: u32) -> Self

Create a new engine targeting current_version.

Source

pub fn register(&mut self, step: Box<dyn MigrationStep>)

Register a migration step.

Source

pub fn current_version(&self) -> u32

The current (target) schema version.

Source

pub fn needs_migration(&self, data_version: u32) -> bool

Check if data needs migration.

Source

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.

Source

pub fn validate_chain(&self, min_version: u32) -> Result<(), MigrationError>

Validate that the migration chain is complete from min_version to current_version.

Source

pub fn registered_steps(&self) -> Vec<(u32, u32)>

List all registered migration steps as (from, to) pairs.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.