pub struct Migration { /* private fields */ }Expand description
Abstract base class for user-defined migrations.
§Purpose
Represents a versioned database schema migration that can be applied to upgrade the database from one schema version to another. Encapsulates migration steps and provides lazy execution semantics.
§Characteristics
- Immutable public API with internal mutable state via Arc<Mutex<>>
- Clone-able: clones share the same underlying migration state via Arc
- Lazy execution: migration steps are generated on first call to steps()
- Idempotent: multiple steps() calls return cached results (execute() called once)
- Closure-based: migration logic defined by user-provided function
§Usage
ⓘ
let migration = Migration::new(1, 2, |instruction| {
instruction.for_database().add_user("admin", "password")
.drop_collection("test");
instruction.for_collection("users").rename("customers");
instruction.for_repository("books", None).delete_field("price");
Ok(())
});
// Apply to database
let db = Nitrite::builder()
.schema_version(2)
.add_migration(migration)
.open_or_create(Some("admin"), Some("password"))?;Implementations§
Source§impl Migration
impl Migration
Sourcepub fn new(
from_version: u32,
to_version: u32,
migrate: impl Fn(&InstructionSet) -> NitriteResult<()> + Send + Sync + 'static,
) -> Self
pub fn new( from_version: u32, to_version: u32, migrate: impl Fn(&InstructionSet) -> NitriteResult<()> + Send + Sync + 'static, ) -> Self
Creates a new migration for upgrading from one schema version to another.
§Arguments
from_version- Source schema versionto_version- Target schema versionmigrate- Closure that defines migration operations via InstructionSet
§Returns
Migration instance wrapping the migration logic
§Behavior
- The migrate closure is not executed immediately
- Execution is deferred until first call to steps() or execute()
- Closure receives immutable InstructionSet reference for building instructions
Sourcepub fn from_version(&self) -> u32
pub fn from_version(&self) -> u32
Sourcepub fn to_version(&self) -> u32
pub fn to_version(&self) -> u32
Sourcepub fn steps(&self) -> NitriteResult<Vec<MigrationStep>>
pub fn steps(&self) -> NitriteResult<Vec<MigrationStep>>
Returns all migration steps, triggering lazy execution on first call.
§Returns
Ok(Vec<MigrationStep>) - All migration steps generated by the migration closure
Err(NitriteError) - If migration execution fails or state access fails
§Behavior
- First call executes the migration closure to generate steps
- Subsequent calls return cached steps without re-execution
- Executed flag prevents duplicate execution
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Migration
impl !UnwindSafe for Migration
impl Freeze for Migration
impl Send for Migration
impl Sync for Migration
impl Unpin for Migration
impl UnsafeUnpin for Migration
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more