|
| 1 | +use anyhow::Result; |
| 2 | +use lazy_static::lazy_static; |
| 3 | +use rusqlite::params; |
| 4 | +use rusqlite_migration::{AsyncMigrations, M}; |
| 5 | +use tokio_rusqlite::Connection; |
| 6 | + |
| 7 | +// Test that migrations are working |
| 8 | +#[cfg(test)] |
| 9 | +mod tests { |
| 10 | + use super::*; |
| 11 | + |
| 12 | + #[test] |
| 13 | + fn migrations_test() { |
| 14 | + assert!(MIGRATIONS.validate().is_ok()); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +// Define migrations. These are applied atomically. |
| 19 | +lazy_static! { |
| 20 | + static ref MIGRATIONS: AsyncMigrations = |
| 21 | + AsyncMigrations::new(vec![ |
| 22 | + M::up(include_str!("../../friend_car.sql")), |
| 23 | + // PRAGMA are better applied outside of migrations, see below for details. |
| 24 | + M::up(r#" |
| 25 | + ALTER TABLE friend ADD COLUMN birthday TEXT; |
| 26 | + ALTER TABLE friend ADD COLUMN comment TEXT; |
| 27 | + "#), |
| 28 | + |
| 29 | + // This migration can be reverted |
| 30 | + M::up("CREATE TABLE animal(name TEXT);") |
| 31 | + .down("DROP TABLE animal;") |
| 32 | + |
| 33 | + // In the future, if the need to change the schema arises, put |
| 34 | + // migrations here, like so: |
| 35 | + // M::up("CREATE INDEX UX_friend_email ON friend(email);"), |
| 36 | + // M::up("CREATE INDEX UX_friend_name ON friend(name);"), |
| 37 | + ]); |
| 38 | +} |
| 39 | + |
| 40 | +pub async fn init_db() -> Result<Connection> { |
| 41 | + let mut async_conn = Connection::open("./my_db.db3").await?; |
| 42 | + |
| 43 | + // Update the database schema, atomically |
| 44 | + MIGRATIONS.to_latest(&mut async_conn).await?; |
| 45 | + |
| 46 | + Ok(async_conn) |
| 47 | +} |
| 48 | + |
| 49 | +#[tokio::main] |
| 50 | +async fn main() { |
| 51 | + env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init(); |
| 52 | + |
| 53 | + let mut async_conn = init_db().await.unwrap(); |
| 54 | + |
| 55 | + // Apply some PRAGMA. These are often better applied outside of migrations, as some needs to be |
| 56 | + // executed for each connection (like `foreign_keys`) or to be executed outside transactions |
| 57 | + // (`journal_mode` is a noop in a transaction). |
| 58 | + async_conn |
| 59 | + .call(|conn| conn.pragma_update(None, "journal_mode", "WAL")) |
| 60 | + .await |
| 61 | + .unwrap(); |
| 62 | + async_conn |
| 63 | + .call(|conn| conn.pragma_update(None, "foreign_keys", "ON")) |
| 64 | + .await |
| 65 | + .unwrap(); |
| 66 | + |
| 67 | + // Use the db 🥳 |
| 68 | + async_conn |
| 69 | + .call(|conn| { |
| 70 | + conn.execute( |
| 71 | + "INSERT INTO friend (name, birthday) VALUES (?1, ?2)", |
| 72 | + params!["John", "1970-01-01"], |
| 73 | + ) |
| 74 | + }) |
| 75 | + .await |
| 76 | + .unwrap(); |
| 77 | + |
| 78 | + async_conn |
| 79 | + .call(|conn| conn.execute("INSERT INTO animal (name) VALUES (?1)", params!["dog"])) |
| 80 | + .await |
| 81 | + .unwrap(); |
| 82 | + |
| 83 | + // If we want to revert the last migration |
| 84 | + MIGRATIONS.to_version(&mut async_conn, 2).await.unwrap(); |
| 85 | + |
| 86 | + // The table was removed |
| 87 | + async_conn |
| 88 | + .call(|conn| conn.execute("INSERT INTO animal (name) VALUES (?1)", params!["cat"])) |
| 89 | + .await |
| 90 | + .unwrap_err(); |
| 91 | +} |
0 commit comments