Struct rusqlite_migration::AsyncMigrations
source · 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
impl AsyncMigrations
sourcepub fn new(ms: Vec<M<'static>>) -> Self
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);"),
]);
sourcepub fn from_directory(dir: &'static Dir<'static>) -> Result<Self>
Available on crate feature from-directory
only.
pub fn from_directory(dir: &'static Dir<'static>) -> Result<Self>
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();
sourcepub async fn current_version(
&self,
async_conn: &AsyncConnection
) -> Result<SchemaVersion>
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());
sourcepub async fn to_latest(&self, async_conn: &mut AsyncConnection) -> Result<()>
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();
sourcepub async fn to_version(
&self,
async_conn: &mut AsyncConnection,
version: usize
) -> Result<()>
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;
Trait Implementations§
source§impl Clone for AsyncMigrations
impl Clone for AsyncMigrations
source§fn clone(&self) -> AsyncMigrations
fn clone(&self) -> AsyncMigrations
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for AsyncMigrations
impl Debug for AsyncMigrations
source§impl FromIterator<M<'static>> for AsyncMigrations
impl FromIterator<M<'static>> for AsyncMigrations
source§impl PartialEq for AsyncMigrations
impl PartialEq for AsyncMigrations
source§fn eq(&self, other: &AsyncMigrations) -> bool
fn eq(&self, other: &AsyncMigrations) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.impl Eq for AsyncMigrations
impl StructuralPartialEq for AsyncMigrations
Auto Trait Implementations§
impl Freeze for AsyncMigrations
impl !RefUnwindSafe for AsyncMigrations
impl Send for AsyncMigrations
impl Sync for AsyncMigrations
impl Unpin for AsyncMigrations
impl !UnwindSafe for AsyncMigrations
Blanket Implementations§
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