diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/cli.rs | 14 | ||||
| -rw-r--r-- | src/repo/mod.rs | 1 | ||||
| -rw-r--r-- | src/repo/pool.rs | 18 |
3 files changed, 22 insertions, 11 deletions
@@ -1,12 +1,11 @@ use std::io; -use std::str::FromStr; use axum::{middleware, Router}; use clap::Parser; -use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions}; +use sqlx::sqlite::SqlitePool; use tokio::net; -use crate::{app::App, channel, clock, events, login}; +use crate::{app::App, channel, clock, events, login, repo::pool}; pub type Result<T> = std::result::Result<T, Error>; @@ -26,8 +25,6 @@ impl Args { pub async fn run(self) -> Result<()> { let pool = self.pool().await?; - sqlx::migrate!().run(&pool).await?; - let app = App::from(pool).await?; let app = routers() .route_layer(middleware::from_fn(clock::middleware)) @@ -54,12 +51,7 @@ impl Args { } async fn pool(&self) -> sqlx::Result<SqlitePool> { - let options = SqliteConnectOptions::from_str(&self.database_url)? - .create_if_missing(true) - .optimize_on_close(true, /* analysis_limit */ None); - - let pool = SqlitePoolOptions::new().connect_with(options).await?; - Ok(pool) + pool::prepare(&self.database_url).await } } diff --git a/src/repo/mod.rs b/src/repo/mod.rs index f36f0da..cb9d7c8 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -2,4 +2,5 @@ pub mod channel; pub mod error; pub mod login; pub mod message; +pub mod pool; pub mod token; diff --git a/src/repo/pool.rs b/src/repo/pool.rs new file mode 100644 index 0000000..b4aa6fc --- /dev/null +++ b/src/repo/pool.rs @@ -0,0 +1,18 @@ +use std::str::FromStr; + +use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions}; + +pub async fn prepare(url: &str) -> sqlx::Result<SqlitePool> { + let pool = create(url).await?; + sqlx::migrate!().run(&pool).await?; + Ok(pool) +} + +async fn create(database_url: &str) -> sqlx::Result<SqlitePool> { + let options = SqliteConnectOptions::from_str(database_url)? + .create_if_missing(true) + .optimize_on_close(true, /* analysis_limit */ None); + + let pool = SqlitePoolOptions::new().connect_with(options).await?; + Ok(pool) +} |
