MigrationRunner

Struct MigrationRunner 

Source
pub struct MigrationRunner<D: MigrationDialect> { /* private fields */ }
Expand description

Runs migrations in dependency order.

The runner tracks which migrations are registered and uses the provided MigrationState to determine which migrations need to be applied.

§Example

use oxide_sql_core::migrations::{
    Migration, MigrationRunner, MigrationState, Operation,
    CreateTableBuilder, SqliteDialect, bigint,
};

// Define a migration
pub struct Migration0001;
impl Migration for Migration0001 {
    const ID: &'static str = "0001_initial";
    fn up() -> Vec<Operation> {
        vec![CreateTableBuilder::new()
            .name("test")
            .column(bigint("id").primary_key().build())
            .build()
            .into()]
    }
    fn down() -> Vec<Operation> {
        vec![Operation::drop_table("test")]
    }
}

// Create runner
let mut runner = MigrationRunner::new(SqliteDialect::new());
runner.register::<Migration0001>();

// Check status
let state = MigrationState::new();
let pending = runner.pending_migrations(&state);
assert_eq!(pending.len(), 1);

Implementations§

Source§

impl<D: MigrationDialect> MigrationRunner<D>

Source

pub fn new(dialect: D) -> Self

Creates a new migration runner with the given dialect.

Source

pub fn register<M: Migration>(&mut self) -> &mut Self

Registers a migration.

Source

pub fn migrations(&self) -> &[RegisteredMigration]

Returns all registered migrations.

Source

pub fn dialect(&self) -> &D

Returns the dialect.

Source

pub fn pending_migrations( &self, state: &MigrationState, ) -> Vec<&RegisteredMigration>

Returns migrations that haven’t been applied yet.

Source

pub fn status(&self, state: &MigrationState) -> Vec<MigrationStatus>

Returns the status of all migrations.

Source

pub fn sorted_migrations( &self, ) -> Result<Vec<&RegisteredMigration>, MigrationError>

Returns migrations in dependency order (topological sort).

Returns Err if there’s a circular dependency.

Source

pub fn sql_for_pending( &self, state: &MigrationState, ) -> Result<Vec<(&'static str, Vec<String>)>, MigrationError>

Generates SQL for all pending migrations.

Returns a list of (migration_id, sql_statements) pairs.

Source

pub fn sql_for_rollback( &self, state: &MigrationState, count: usize, ) -> Result<Vec<(&'static str, Vec<String>)>, MigrationError>

Generates SQL for rolling back migrations.

Returns a list of (migration_id, sql_statements) pairs in reverse order.

Source

pub fn validate(&self) -> Result<(), MigrationError>

Validates that all dependencies exist and are registered.

Auto Trait Implementations§

§

impl<D> Freeze for MigrationRunner<D>
where D: Freeze,

§

impl<D> RefUnwindSafe for MigrationRunner<D>
where D: RefUnwindSafe,

§

impl<D> Send for MigrationRunner<D>
where D: Send,

§

impl<D> Sync for MigrationRunner<D>
where D: Sync,

§

impl<D> Unpin for MigrationRunner<D>
where D: Unpin,

§

impl<D> UnwindSafe for MigrationRunner<D>
where D: UnwindSafe,

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.