pub struct AsyncMigrations { /* private fields */ }
Available on crate feature alpha-async-tokio-rusqlite only.
Expand description

Adapter to make Migrations available in an async context.

Implementations§

source§

impl AsyncMigrations

source

pub fn new(ms: Vec<M<'static>>) -> Self

Create a proxy struct to a Migrations instance for use in an asynchronous context.

§Example
use rusqlite_migration::{Migrations, AsyncMigrations, M};

let migrations = AsyncMigrations::new(vec![
    M::up("CREATE TABLE animals (name TEXT);"),
    M::up("CREATE TABLE food (name TEXT);"),
]);
source

pub fn from_directory(dir: &'static Dir<'static>) -> Result<Self>

Available on crate feature from-directory only.

Proxy implementation of the same method in the Migrations struct.

§Example
use rusqlite_migration::AsyncMigrations;
use include_dir::{Dir, include_dir};

static MIGRATION_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/../examples/from-directory/migrations");
let migrations = AsyncMigrations::from_directory(&MIGRATION_DIR).unwrap();
source

pub async fn current_version( &self, async_conn: &AsyncConnection ) -> Result<SchemaVersion>

Asynchronous version of the same method in the Migrations struct.

§Example
use rusqlite_migration::{Migrations, AsyncMigrations, M, SchemaVersion};
use std::num::NonZeroUsize;

let mut conn = tokio_rusqlite::Connection::open_in_memory().await.unwrap();

let migrations = AsyncMigrations::new(vec![
    M::up("CREATE TABLE animals (name TEXT);"),
    M::up("CREATE TABLE food (name TEXT);"),
]);

assert_eq!(SchemaVersion::NoneSet, migrations.current_version(&conn).await.unwrap());

// Go to the latest version
migrations.to_latest(&mut conn).await.unwrap();

assert_eq!(SchemaVersion::Inside(NonZeroUsize::new(2).unwrap()), migrations.current_version(&conn).await.unwrap());
source

pub async fn to_latest(&self, async_conn: &mut AsyncConnection) -> Result<()>

Asynchronous version of the same method in the Migrations struct.

§Example
use rusqlite_migration::{Migrations, AsyncMigrations, M};
let mut conn = tokio_rusqlite::Connection::open_in_memory().await.unwrap();

let migrations = AsyncMigrations::new(vec![
    M::up("CREATE TABLE animals (name TEXT);"),
    M::up("CREATE TABLE food (name TEXT);"),
]);

// Go to the latest version
migrations.to_latest(&mut conn).await.unwrap();

// You can then insert values in the database
conn.call_unwrap(|conn| conn.execute("INSERT INTO animals (name) VALUES (?)", ["dog"])).await.unwrap();
conn.call_unwrap(|conn| conn.execute("INSERT INTO food (name) VALUES (?)", ["carrot"])).await.unwrap();
source

pub async fn to_version( &self, async_conn: &mut AsyncConnection, version: usize ) -> Result<()>

Asynchronous version of the same method in the Migrations struct.

§Example
use rusqlite_migration::{Migrations, AsyncMigrations, M};
let mut conn = tokio_rusqlite::Connection::open_in_memory().await.unwrap();
let migrations = AsyncMigrations::new(vec![
    // 0: version 0, before having run any migration
    M::up("CREATE TABLE animals (name TEXT);").down("DROP TABLE animals;"),
    // 1: version 1, after having created the “animals” table
    M::up("CREATE TABLE food (name TEXT);").down("DROP TABLE food;"),
    // 2: version 2, after having created the food table
]);

migrations.to_latest(&mut conn).await.unwrap(); // Create all tables

// Go back to version 1, i.e. after running the first migration
migrations.to_version(&mut conn, 1).await;
conn.call(|conn| Ok(conn.execute("INSERT INTO animals (name) VALUES (?)", ["dog"]))).await.unwrap();
conn.call(|conn| Ok(conn.execute("INSERT INTO food (name) VALUES (?)", ["carrot"]).unwrap_err())).await;

// Go back to an empty database
migrations.to_version(&mut conn, 0).await;
conn.call(|conn| Ok(conn.execute("INSERT INTO animals (name) VALUES (?)", ["cat"]).unwrap_err())).await;
conn.call(|conn| Ok(conn.execute("INSERT INTO food (name) VALUES (?)", ["milk"]).unwrap_err())).await;
source

pub async fn validate(&self) -> Result<()>

Asynchronous version of the same method in the Migrations struct.

§Example
#[cfg(test)]
mod tests {

    // … Other tests …

    #[tokio::test]
    fn migrations_test() {
        assert!(migrations.validate().await.is_ok());
    }
}

Trait Implementations§

source§

impl Clone for AsyncMigrations

source§

fn clone(&self) -> AsyncMigrations

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for AsyncMigrations

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl FromIterator<M<'static>> for AsyncMigrations

source§

fn from_iter<T: IntoIterator<Item = M<'static>>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl PartialEq for AsyncMigrations

source§

fn eq(&self, other: &AsyncMigrations) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for AsyncMigrations

source§

impl StructuralPartialEq for AsyncMigrations

Auto Trait Implementations§

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.