|
| 1 | +use tokio_rusqlite::Connection as AsyncConnection; |
| 2 | + |
| 3 | +use crate::errors::Result; |
| 4 | +use crate::{Migrations, SchemaVersion, M}; |
| 5 | + |
| 6 | +/// Adapter to make `Migrations` available in an async context. |
| 7 | +#[derive(Debug, PartialEq, Eq, Clone)] |
| 8 | +pub struct AsyncMigrations { |
| 9 | + migrations: Migrations<'static>, |
| 10 | +} |
| 11 | + |
| 12 | +impl AsyncMigrations { |
| 13 | + /// Adapt a [Migrations](crate::Migrations) instance for use in an asynchronous context. |
| 14 | + /// |
| 15 | + /// # Example |
| 16 | + /// |
| 17 | + /// ```rust |
| 18 | + /// use rusqlite_migration::{Migrations, AsyncMigrations, M}; |
| 19 | + /// |
| 20 | + /// let migrations = AsyncMigrations::new(vec![ |
| 21 | + /// M::up("CREATE TABLE animals (name TEXT);"), |
| 22 | + /// M::up("CREATE TABLE food (name TEXT);"), |
| 23 | + /// ]); |
| 24 | + /// ``` |
| 25 | + pub fn new(ms: Vec<M<'static>>) -> Self { |
| 26 | + Self { |
| 27 | + migrations: Migrations::new(ms), |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /// Asynchronous version of the same method in the [Migrations](crate::Migrations::current_version) struct. |
| 32 | + /// |
| 33 | + /// # Example |
| 34 | + /// |
| 35 | + /// ```rust |
| 36 | + /// # tokio_test::block_on(async { |
| 37 | + /// use rusqlite_migration::{Migrations, AsyncMigrations, M, SchemaVersion}; |
| 38 | + /// use std::num::NonZeroUsize; |
| 39 | + /// |
| 40 | + /// let mut conn = tokio_rusqlite::Connection::open_in_memory().await.unwrap(); |
| 41 | + /// |
| 42 | + /// let migrations = AsyncMigrations::new(vec![ |
| 43 | + /// M::up("CREATE TABLE animals (name TEXT);"), |
| 44 | + /// M::up("CREATE TABLE food (name TEXT);"), |
| 45 | + /// ]); |
| 46 | + /// |
| 47 | + /// assert_eq!(SchemaVersion::NoneSet, migrations.current_version(&conn).await.unwrap()); |
| 48 | + /// |
| 49 | + /// // Go to the latest version |
| 50 | + /// migrations.to_latest(&mut conn).await.unwrap(); |
| 51 | + /// |
| 52 | + /// assert_eq!(SchemaVersion::Inside(NonZeroUsize::new(2).unwrap()), migrations.current_version(&conn).await.unwrap()); |
| 53 | + /// # }) |
| 54 | + /// ``` |
| 55 | + pub async fn current_version(&self, async_conn: &AsyncConnection) -> Result<SchemaVersion> { |
| 56 | + let m = self.migrations.clone(); |
| 57 | + async_conn.call(move |conn| m.current_version(conn)).await |
| 58 | + } |
| 59 | + |
| 60 | + /// Asynchronous version of the same method in the [Migrations](super::Migrations::to_latest) struct. |
| 61 | + /// |
| 62 | + /// # Example |
| 63 | + /// |
| 64 | + /// ```rust |
| 65 | + /// # tokio_test::block_on(async { |
| 66 | + /// use rusqlite_migration::{Migrations, AsyncMigrations, M}; |
| 67 | + /// let mut conn = tokio_rusqlite::Connection::open_in_memory().await.unwrap(); |
| 68 | + /// |
| 69 | + /// let migrations = AsyncMigrations::new(vec![ |
| 70 | + /// M::up("CREATE TABLE animals (name TEXT);"), |
| 71 | + /// M::up("CREATE TABLE food (name TEXT);"), |
| 72 | + /// ]); |
| 73 | + /// |
| 74 | + /// // Go to the latest version |
| 75 | + /// migrations.to_latest(&mut conn).await.unwrap(); |
| 76 | + /// |
| 77 | + /// // You can then insert values in the database |
| 78 | + /// conn.call(|conn| conn.execute("INSERT INTO animals (name) VALUES (?)", ["dog"])).await.unwrap(); |
| 79 | + /// conn.call(|conn| conn.execute("INSERT INTO food (name) VALUES (?)", ["carrot"])).await.unwrap(); |
| 80 | + /// # }); |
| 81 | + /// ``` |
| 82 | + pub async fn to_latest(&self, async_conn: &mut AsyncConnection) -> Result<()> { |
| 83 | + let m = self.migrations.clone(); |
| 84 | + async_conn.call(move |conn| m.to_latest(conn)).await |
| 85 | + } |
| 86 | + |
| 87 | + /// Asynchronous version of the same method in the [Migrations](crate::Migrations::to_version) struct. |
| 88 | + /// |
| 89 | + /// # Example |
| 90 | + /// |
| 91 | + /// ```rust |
| 92 | + /// # tokio_test::block_on(async { |
| 93 | + /// use rusqlite_migration::{Migrations, AsyncMigrations, M}; |
| 94 | + /// let mut conn = tokio_rusqlite::Connection::open_in_memory().await.unwrap(); |
| 95 | + /// let migrations = AsyncMigrations::new(vec![ |
| 96 | + /// // 0: version 0, before having run any migration |
| 97 | + /// M::up("CREATE TABLE animals (name TEXT);").down("DROP TABLE animals;"), |
| 98 | + /// // 1: version 1, after having created the “animals” table |
| 99 | + /// M::up("CREATE TABLE food (name TEXT);").down("DROP TABLE food;"), |
| 100 | + /// // 2: version 2, after having created the food table |
| 101 | + /// ]); |
| 102 | + /// |
| 103 | + /// migrations.to_latest(&mut conn).await.unwrap(); // Create all tables |
| 104 | + /// |
| 105 | + /// // Go back to version 1, i.e. after running the first migration |
| 106 | + /// migrations.to_version(&mut conn, 1).await; |
| 107 | + /// conn.call(|conn| conn.execute("INSERT INTO animals (name) VALUES (?)", ["dog"])).await.unwrap(); |
| 108 | + /// conn.call(|conn| conn.execute("INSERT INTO food (name) VALUES (?)", ["carrot"]).unwrap_err()).await; |
| 109 | + /// |
| 110 | + /// // Go back to an empty database |
| 111 | + /// migrations.to_version(&mut conn, 0).await; |
| 112 | + /// conn.call(|conn| conn.execute("INSERT INTO animals (name) VALUES (?)", ["cat"]).unwrap_err()).await; |
| 113 | + /// conn.call(|conn| conn.execute("INSERT INTO food (name) VALUES (?)", ["milk"]).unwrap_err()).await; |
| 114 | + /// # }) |
| 115 | + /// ``` |
| 116 | + pub async fn to_version(&self, async_conn: &mut AsyncConnection, version: usize) -> Result<()> { |
| 117 | + let m = self.migrations.clone(); |
| 118 | + async_conn |
| 119 | + .call(move |conn| m.to_version(conn, version)) |
| 120 | + .await |
| 121 | + } |
| 122 | + |
| 123 | + /// Asynchronous version of the same method in the [Migrations](crate::Migrations::validate) struct. |
| 124 | + /// |
| 125 | + /// # Example |
| 126 | + /// |
| 127 | + /// ```rust |
| 128 | + /// #[cfg(test)] |
| 129 | + /// mod tests { |
| 130 | + /// |
| 131 | + /// // … Other tests … |
| 132 | + /// |
| 133 | + /// #[tokio::test] |
| 134 | + /// fn migrations_test() { |
| 135 | + /// assert!(migrations.validate().await.is_ok()); |
| 136 | + /// } |
| 137 | + /// } |
| 138 | + /// ``` |
| 139 | + pub async fn validate(&self) -> Result<()> { |
| 140 | + let mut async_conn = AsyncConnection::open_in_memory().await?; |
| 141 | + self.to_latest(&mut async_conn).await |
| 142 | + } |
| 143 | +} |
0 commit comments