Skip to main content

TypedMigration

Trait TypedMigration 

Source
pub trait TypedMigration<Conn> {
    // Required method
    fn up(&self, conn: &mut Conn) -> QueryResult<()>;

    // Provided methods
    fn down(&self, _conn: &mut Conn) -> QueryResult<()> { ... }
    fn run_in_transaction(&self) -> bool { ... }
}
Expand description

A typed rust migration for a given connection type

This type describes a typed rust migration for a specific connection type Conn

This trait only requires you to provide an up migration by implementing the relevant function. Optionally you can overwrite the down migration and the migration settings by providing a custom implementation for the relevant functions.

Required Methods§

Source

fn up(&self, conn: &mut Conn) -> QueryResult<()>

The implementation of the up migration.

This function is supposed to migrate your database from an old schema version considered valid before this migration was written to a new schema version expected after this migration was written.

This will be run inside of a transaction if Self::run_in_transaction is is not customized to return false

Provided Methods§

Source

fn down(&self, _conn: &mut Conn) -> QueryResult<()>

The implementation of the down migration.

This function is supposed to revert everything that’s done in your up migration.

The default implementation doesn’t perform any action. If you never plan to revert migrations it can be fine to not provide a custom implementation of this function.

This will be run inside of a transaction if Self::run_in_transaction is is not customized to return false

Source

fn run_in_transaction(&self) -> bool

Should the given migration be run in a transaction or not

By default diesel runs migrations inside of transactions (if the underlying database system supports that). This ensures that each migration is ever only executed as single unit or fails as single unit.

Nevertheless specific database operations might require to be run outside of transactions. If you plan to use such an operation you want to provide a custom implementation of this function that returns false

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<Conn, F> TypedMigration<Conn> for F
where F: Fn(&mut Conn) -> QueryResult<()>,

Source§

impl<Conn> TypedMigration<Conn> for RustMigration<Conn>