pub struct Migrator<T: Versioned + DeserializeOwned> { /* private fields */ }Expand description
Schema migration pipeline for a Versioned type.
Add from → from + 1 steps with Migrator::step; the order in which
they’re added does not matter — Migrator::run walks them in
version order.
Migrator<T> is cheaply Clone (each step’s closure lives behind an
Arc, so cloning is a handful of refcount bumps, not a deep copy) and
Send + Sync whenever T is — which is what lets a Patch
(crate::flush::Patch) closure retain its own copy of the migrator and re-run it against the
document read fresh on the shared I/O worker thread, instead of the
stale, possibly-out-of-date value this handle loaded at construction.
Implementations§
Source§impl<T: Versioned + DeserializeOwned> Migrator<T>
impl<T: Versioned + DeserializeOwned> Migrator<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty migrator with no steps registered.
If T::CURRENT_VERSION is 1 (the initial schema) or the file
is already at the current version, no steps are needed and
run will succeed immediately.
Sourcepub fn step<F>(self, from: u32, func: F) -> Self
pub fn step<F>(self, from: u32, func: F) -> Self
Register a step that promotes a value from from to from + 1.
Steps may be registered in any order; run finds
the right one for the current version on demand.
Sourcepub fn run(&self, raw: Value) -> Result<T, MigrationError>
pub fn run(&self, raw: Value) -> Result<T, MigrationError>
Migrate raw from its on-disk version up to
T::CURRENT_VERSION, then deserialize.
Reads the version directly from the version field of the raw
toml::Value — never deserializes-then-checks, because a v1
payload typically fails to deserialize as the v2 type.
Files missing the version field are treated as v1 (legacy).
Trait Implementations§
Source§impl<T: Versioned + DeserializeOwned> Clone for Migrator<T>
impl<T: Versioned + DeserializeOwned> Clone for Migrator<T>
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Hand-written rather than #[derive(Clone)]: a derive would add a
spurious T: Clone bound (from the PhantomData<T> field) even
though nothing here actually needs it — Vec<Step> clones just fine
on its own (each Step::func is an Arc, so this is a handful of
refcount bumps).
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more