| 1 | use std::str::FromStr; |
| 2 | |
| 3 | use sqlx::SqlitePool; |
| 4 | use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | pub async fn connect(database_url: &str) -> Result<SqlitePool, sqlx::Error> { |
| 11 | let pool = open(database_url).await?; |
| 12 | |
| 13 | migrate(&pool).await?; |
| 14 | |
| 15 | Ok(pool) |
| 16 | } |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | pub async fn open(database_url: &str) -> Result<SqlitePool, sqlx::Error> { |
| 25 | |
| 26 | |
| 27 | |
| 28 | let options = SqliteConnectOptions::from_str(database_url)?.foreign_keys(true); |
| 29 | |
| 30 | SqlitePoolOptions::new().connect_with(options).await |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | pub async fn migrate(pool: &SqlitePool) -> Result<(), sqlx::Error> { |
| 38 | sqlx::migrate!("./migrations") |
| 39 | .run(pool) |
| 40 | .await |
| 41 | .map_err(|error| sqlx::Error::Migrate(Box::new(error))) |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | #[cfg(test)] |
| 50 | #[path = "database_compat_tests.rs"] |
| 51 | mod compat_tests; |
| 52 | |
| 53 | #[cfg(test)] |
| 54 | pub(crate) mod test_support { |
| 55 | use super::*; |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | pub async fn test_pool() -> SqlitePool { |
| 62 | let options = SqliteConnectOptions::from_str("sqlite::memory:") |
| 63 | .expect("in-memory url should parse") |
| 64 | .foreign_keys(true); |
| 65 | |
| 66 | let pool = SqlitePoolOptions::new() |
| 67 | .max_connections(1) |
| 68 | .connect_with(options) |
| 69 | .await |
| 70 | .expect("in-memory database should open"); |
| 71 | |
| 72 | migrate(&pool).await.expect("migrations should apply"); |
| 73 | |
| 74 | pool |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | #[cfg(test)] |
| 79 | mod tests { |
| 80 | use super::*; |
| 81 | use sqlx::Row; |
| 82 | |
| 83 | #[tokio::test] |
| 84 | async fn migrations_apply_to_a_fresh_database() { |
| 85 | let pool = test_support::test_pool().await; |
| 86 | |
| 87 | let tables: Vec<String> = |
| 88 | sqlx::query("select name from sqlite_master where type = 'table'") |
| 89 | .fetch_all(&pool) |
| 90 | .await |
| 91 | .expect("query") |
| 92 | .into_iter() |
| 93 | .map(|row| row.get::<String, _>("name")) |
| 94 | .collect(); |
| 95 | |
| 96 | for expected in ["orgs", "users", "memberships"] { |
| 97 | assert!( |
| 98 | tables.iter().any(|name| name == expected), |
| 99 | "expected a {expected} table, got {tables:?}" |
| 100 | ); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | #[tokio::test] |
| 105 | async fn foreign_keys_are_enforced() { |
| 106 | let pool = test_support::test_pool().await; |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | let result = sqlx::query( |
| 112 | "insert into users (id, email, password_hash, personal_org_id) |
| 113 | values ('u1', 'dev@example.com', 'hash', 'org-that-does-not-exist')", |
| 114 | ) |
| 115 | .execute(&pool) |
| 116 | .await; |
| 117 | |
| 118 | assert!(result.is_err(), "expected the foreign key to be enforced"); |
| 119 | } |
| 120 | |
| 121 | #[tokio::test] |
| 122 | async fn migrations_are_idempotent() { |
| 123 | let pool = test_support::test_pool().await; |
| 124 | |
| 125 | migrate(&pool).await.expect("re-running should be a no-op"); |
| 126 | } |
| 127 | } |