AsyncMigrationHarness

Struct AsyncMigrationHarness 

Source
pub struct AsyncMigrationHarness<C> { /* private fields */ }
Available on crate feature migrations only.
Expand description

A diesel-migration MigrationHarness to run migrations via an AsyncConnection

Internally this harness is using tokio::task::block_in_place and AsyncConnectionWrapper to utilize sync Diesel’s migration infrastructure. For most applications this shouldn’t be problematic as migrations are usually run at application startup and most applications default to use the multithreaded tokio runtime. In turn this also means that you cannot use this migration harness if you use the current thread variant of the tokio runtime or if you run migrations in a very special setup (e.g by using tokio::select! or tokio::join! on a future produced by running the migrations). Consider manually construct a blocking task via tokio::task::spawn_blocking instead.

§Example

use diesel_async::AsyncMigrationHarness;
use diesel_migrations::{FileBasedMigrations, MigrationHarness};

let mut connection = connection_no_data().await;

// Alternativly use `diesel_migrations::embed_migrations!()`
// to get a list of migrations
let migrations = FileBasedMigrations::find_migrations_directory()?;

let mut harness = AsyncMigrationHarness::new(connection);
harness.run_pending_migrations(migrations)?;
// get back the connection from the harness
let connection = harness.into_inner();

§Example with pool

let config = AsyncDieselConnectionManager::<diesel_async::AsyncPgConnection>::new(db_url);
use diesel_async::pooled_connection::deadpool::Pool;
use diesel_async::AsyncMigrationHarness;
use diesel_migrations::{FileBasedMigrations, MigrationHarness};

// Alternativly use `diesel_migrations::embed_migrations!()`
// to get a list of migrations
let migrations = FileBasedMigrations::find_migrations_directory()?;

let pool = Pool::builder(get_config()).build()?;
let mut harness = AsyncMigrationHarness::new(pool.get().await?);
harness.run_pending_migrations(migrations)?;

Implementations§

Source§

impl<C> AsyncMigrationHarness<C>
where C: AsyncConnection,

Source

pub fn new(connection: C) -> Self

Construct a new AsyncMigrationHarness from a given connection

Source

pub fn into_inner(self) -> C

Return the connection stored inside this instance of AsyncMigrationHarness

Trait Implementations§

Source§

impl<C> From<C> for AsyncMigrationHarness<C>
where C: AsyncConnection,

Source§

fn from(value: C) -> Self

Converts to this type from the input type.
Source§

impl<C> MigrationHarness<<C as AsyncConnectionCore>::Backend> for AsyncMigrationHarness<C>

Source§

fn run_migration( &mut self, migration: &dyn Migration<C::Backend>, ) -> Result<MigrationVersion<'static>>

Apply a single migration Read more
Source§

fn revert_migration( &mut self, migration: &dyn Migration<C::Backend>, ) -> Result<MigrationVersion<'static>>

Revert a single migration Read more
Source§

fn applied_migrations(&mut self) -> Result<Vec<MigrationVersion<'static>>>

Get a list of already applied migration versions
Source§

fn has_pending_migration<S>( &mut self, source: S, ) -> Result<bool, Box<dyn Error + Sync + Send>>
where S: MigrationSource<DB>,

Checks if the database represented by the current harness has unapplied migrations
Source§

fn run_pending_migrations<S>( &mut self, source: S, ) -> Result<Vec<MigrationVersion<'_>>, Box<dyn Error + Sync + Send>>
where S: MigrationSource<DB>,

Execute all unapplied migrations for a given migration source Read more
Source§

fn run_next_migration<S>( &mut self, source: S, ) -> Result<MigrationVersion<'_>, Box<dyn Error + Sync + Send>>
where S: MigrationSource<DB>,

Execute the next migration from the given migration source
Source§

fn revert_all_migrations<S>( &mut self, source: S, ) -> Result<Vec<MigrationVersion<'_>>, Box<dyn Error + Sync + Send>>
where S: MigrationSource<DB>,

Revert all applied migrations from a given migration source
Source§

fn revert_last_migration<S>( &mut self, source: S, ) -> Result<MigrationVersion<'static>, Box<dyn Error + Sync + Send>>
where S: MigrationSource<DB>,

Revert the last migration from a given migration source Read more
Source§

fn pending_migrations<S>( &mut self, source: S, ) -> Result<Vec<Box<dyn Migration<DB>>>, Box<dyn Error + Sync + Send>>
where S: MigrationSource<DB>,

Get a list of non applied migrations for a specific migration source Read more

Auto Trait Implementations§

§

impl<C> !Freeze for AsyncMigrationHarness<C>

§

impl<C> RefUnwindSafe for AsyncMigrationHarness<C>
where C: RefUnwindSafe,

§

impl<C> Send for AsyncMigrationHarness<C>
where C: Send,

§

impl<C> Sync for AsyncMigrationHarness<C>
where C: Sync,

§

impl<C> Unpin for AsyncMigrationHarness<C>
where C: Unpin,

§

impl<C> UnwindSafe for AsyncMigrationHarness<C>
where C: UnwindSafe,

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> 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> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

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

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Sync + Send>

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

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, Conn> RunQueryDsl<Conn> for T

Source§

fn execute<'conn, 'query>( self, conn: &'conn mut Conn, ) -> Conn::ExecuteFuture<'conn, 'query>
where Conn: AsyncConnectionCore + Send, Self: ExecuteDsl<Conn> + 'query,

Executes the given command, returning the number of rows affected. Read more
Source§

fn load<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> LoadFuture<'conn, 'query, Self, Conn, U>
where U: Send, Conn: AsyncConnectionCore, Self: LoadQuery<'query, Conn, U> + 'query,

Executes the given query, returning a Vec with the returned rows. Read more
Source§

fn load_stream<'conn, 'query, U>( self, conn: &'conn mut Conn, ) -> Self::LoadFuture<'conn>
where Conn: AsyncConnectionCore, U: 'conn, Self: LoadQuery<'query, Conn, U> + 'query,

Executes the given query, returning a [Stream] with the returned rows. Read more
Source§

fn get_result<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> GetResult<'conn, 'query, Self, Conn, U>
where U: Send + 'conn, Conn: AsyncConnectionCore, Self: LoadQuery<'query, Conn, U> + 'query,

Runs the command, and returns the affected row. Read more
Source§

fn get_results<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> LoadFuture<'conn, 'query, Self, Conn, U>
where U: Send, Conn: AsyncConnectionCore, Self: LoadQuery<'query, Conn, U> + 'query,

Runs the command, returning an Vec with the affected rows. Read more
Source§

fn first<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> GetResult<'conn, 'query, Limit<Self>, Conn, U>
where U: Send + 'conn, Conn: AsyncConnectionCore, Self: LimitDsl, Limit<Self>: LoadQuery<'query, Conn, U> + Send + 'query,

Attempts to load a single record. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,