Skip to main content

RustMigrationSource

Struct RustMigrationSource 

Source
pub struct RustMigrationSource<Conn>
where Conn: Connection,
{ /* private fields */ }
Expand description

A migration source that allows to register rust functions as migrations

The main use-case of this migration source is to allow writing migrations in Rust instead of in SQL. You need to specify at construction time which for which connection type this migration source is indented to be used with. It allows you to register different kinds of hooks to be executed as migrations later:

  • A closure Fn(&mut Conn) -> QueryResult<()> that accepts a mutable connection reference as argument and returns a QueryResult<()>. This closure is executed as up migration.
  • A function with a signature of fn(&mut Conn) -> QueryResult<()>. This function is executed as up migration.
  • An instance of RustMigration, which allows you to register an up and a down migration as required. This type allows you also to configure the migration behaviour. See the documentation there for details.
  • Any type implementing TypedMigration. This allows you to provide a custom type with custom fields to supply additional information to your migration. This trait gives you the full control over how the migration should behave. See the documentation there for details.

A single migration source can mix all of the variants of migrations listed above.

§Example

use diesel::prelude::*;
use diesel_migrations::MigrationHarness;
use diesel_migrations::RustMigration;
use diesel_migrations::RustMigrationSource;
use diesel_migrations::TypedMigration;

fn migration_function(conn: &mut SqliteConnection) -> QueryResult<()> {
    diesel::sql_query("SELECT 'EXECUTE YOUR MIGRATION HERE'").execute(conn)?;
    Ok(())
}

struct CustomMigration(&'static str);

impl TypedMigration<SqliteConnection> for CustomMigration {
    fn up(&self, conn: &mut SqliteConnection) -> QueryResult<()> {
        diesel::sql_query("SELECT 'YOUR MIGRATION', ?")
            .bind::<diesel::sql_types::Text, _>(self.0)
            .execute(conn)?;
        Ok(())
    }
}

let mut rust_migrations = RustMigrationSource::<SqliteConnection>::new();

// register migrations callback
rust_migrations.add_migration(
    "2026-01-30-121720",
    "callback",
    |conn: &mut SqliteConnection| {
        diesel::sql_query("SELECT 'EXECUTE YOUR MIGRATION HERE'").execute(conn)?;
        Ok(())
    },
);

// register a migration as function
rust_migrations.add_migration("2026-01-30-122420", "function", migration_function);

// register a RustMigration
let migration = RustMigration::new(|conn| {
    diesel::sql_query("SELECT 'EXECUTE YOUR MIGRATION HERE'").execute(conn)?;
    Ok(())
})
.with_down(|conn| {
    diesel::sql_query("SELECT 'REVERT YOUR MIGRATION HERE'").execute(conn)?;
    Ok(())
});
rust_migrations.add_migration("2026-01-30-122520", "rust_migration", migration);

// register a custom migration type
rust_migrations.add_migration(
    "2026-01-30-122920", "custom_type",
    CustomMigration("your custom args"),
);

// run the migrations
let mut conn = SqliteConnection::establish(&connection_url)?;
conn.run_pending_migrations(rust_migrations)?;

Implementations§

Source§

impl<Conn> RustMigrationSource<Conn>
where Conn: Connection,

Source

pub fn new() -> Self

Create a new empty migration source

Source

pub fn add_migration( &mut self, version: impl Into<String>, name: impl Into<String>, migration: impl TypedMigration<Conn> + 'static, ) -> Result<&mut Self, MigrationError>

Register a new migration

See the documentation on the type itself for examples

Trait Implementations§

Source§

impl<Conn> Clone for RustMigrationSource<Conn>
where Conn: Connection,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Conn> Default for RustMigrationSource<Conn>
where Conn: Connection,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<Conn> MigrationSource<<Conn as Connection>::Backend> for RustMigrationSource<Conn>
where Conn: Connection + 'static,

Source§

fn migrations(&self) -> Result<Vec<Box<dyn Migration<Conn::Backend>>>>

Get a list of migrations associated with this migration source.

Auto Trait Implementations§

§

impl<Conn> !RefUnwindSafe for RustMigrationSource<Conn>

§

impl<Conn> !Send for RustMigrationSource<Conn>

§

impl<Conn> !Sync for RustMigrationSource<Conn>

§

impl<Conn> !UnwindSafe for RustMigrationSource<Conn>

§

impl<Conn> Freeze for RustMigrationSource<Conn>

§

impl<Conn> Unpin for RustMigrationSource<Conn>

§

impl<Conn> UnsafeUnpin for RustMigrationSource<Conn>

Blanket Implementations§

Source§

impl<T> AggregateExpressionMethods for T

Source§

fn aggregate_distinct(self) -> Self::Output
where Self: DistinctDsl,

DISTINCT modifier for aggregate functions Read more
Source§

fn aggregate_all(self) -> Self::Output
where Self: AllDsl,

ALL modifier for aggregate functions Read more
Source§

fn aggregate_filter<P>(self, f: P) -> Self::Output
where P: AsExpression<Bool>, Self: FilterDsl<<P as AsExpression<Bool>>::Expression>,

Add an aggregate function filter Read more
Source§

fn aggregate_order<O>(self, o: O) -> Self::Output
where Self: OrderAggregateDsl<O>,

Add an aggregate function order Read more
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
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> IntoSql for T

Source§

fn into_sql<T>(self) -> Self::Expression

Convert self to an expression for Diesel’s query builder. Read more
Source§

fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
where &'a Self: AsExpression<T>, T: SqlType + TypedExpressionType,

Convert &self to an expression for Diesel’s query builder. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WindowExpressionMethods for T

Source§

fn over(self) -> Self::Output
where Self: OverDsl,

Turn a function call into a window function call Read more
Source§

fn window_filter<P>(self, f: P) -> Self::Output
where P: AsExpression<Bool>, Self: FilterDsl<<P as AsExpression<Bool>>::Expression>,

Add a filter to the current window function Read more
Source§

fn partition_by<E>(self, expr: E) -> Self::Output
where Self: PartitionByDsl<E>,

Add a partition clause to the current window function Read more
Source§

fn window_order<E>(self, expr: E) -> Self::Output
where Self: OrderWindowDsl<E>,

Add a order clause to the current window function Read more
Source§

fn frame_by<E>(self, expr: E) -> Self::Output
where Self: FrameDsl<E>,

Add a frame clause to the current window function Read more