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 aQueryResult<()>. 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,
impl<Conn> RustMigrationSource<Conn>where
Conn: Connection,
Sourcepub fn add_migration(
&mut self,
version: impl Into<String>,
name: impl Into<String>,
migration: impl TypedMigration<Conn> + 'static,
) -> Result<&mut Self, MigrationError>
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,
impl<Conn> Clone for RustMigrationSource<Conn>where
Conn: Connection,
Source§impl<Conn> Default for RustMigrationSource<Conn>where
Conn: Connection,
impl<Conn> Default for RustMigrationSource<Conn>where
Conn: Connection,
Source§impl<Conn> MigrationSource<<Conn as Connection>::Backend> for RustMigrationSource<Conn>where
Conn: Connection + 'static,
impl<Conn> MigrationSource<<Conn as Connection>::Backend> for RustMigrationSource<Conn>where
Conn: Connection + 'static,
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
impl<T> AggregateExpressionMethods for T
Source§fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
DISTINCT modifier for aggregate functions Read moreSource§fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
ALL modifier for aggregate functions Read moreSource§fn aggregate_filter<P>(self, f: P) -> Self::Output
fn aggregate_filter<P>(self, f: P) -> Self::Output
Add an aggregate function filter Read more
Source§fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
Add an aggregate function order Read more
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,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
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>
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)
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)
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> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expression
fn into_sql<T>(self) -> Self::Expression
Convert
self to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
Convert
&self to an expression for Diesel’s query builder. Read moreSource§impl<T> WindowExpressionMethods for T
impl<T> WindowExpressionMethods for T
Source§fn over(self) -> Self::Outputwhere
Self: OverDsl,
fn over(self) -> Self::Outputwhere
Self: OverDsl,
Turn a function call into a window function call Read more
Source§fn window_filter<P>(self, f: P) -> Self::Output
fn window_filter<P>(self, f: P) -> Self::Output
Add a filter to the current window function Read more
Source§fn partition_by<E>(self, expr: E) -> Self::Outputwhere
Self: PartitionByDsl<E>,
fn partition_by<E>(self, expr: E) -> Self::Outputwhere
Self: PartitionByDsl<E>,
Add a partition clause to the current window function Read more
Source§fn window_order<E>(self, expr: E) -> Self::Outputwhere
Self: OrderWindowDsl<E>,
fn window_order<E>(self, expr: E) -> Self::Outputwhere
Self: OrderWindowDsl<E>,
Add a order clause to the current window function Read more