mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 10:32:19 +00:00
Outsource db pool creation in preparation for unit tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use sqlx::SqlitePool;
|
use sqlx::{sqlite::SqliteConnectOptions, Pool, Sqlite, SqlitePool};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
calendar::{Calendar, CalendarStore},
|
calendar::{Calendar, CalendarStore},
|
||||||
@@ -111,3 +111,17 @@ impl CalendarStore for SqliteCalendarStore {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn create_db_pool(db_url: &str, migrate: bool) -> anyhow::Result<Pool<Sqlite>>{
|
||||||
|
let db = SqlitePool::connect_with(
|
||||||
|
SqliteConnectOptions::new()
|
||||||
|
.filename(db_url)
|
||||||
|
.create_if_missing(true),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
if migrate {
|
||||||
|
println!("Running database migrations");
|
||||||
|
sqlx::migrate!("./migrations").run(&db).await?;
|
||||||
|
}
|
||||||
|
Ok(db)
|
||||||
|
}
|
||||||
15
src/main.rs
15
src/main.rs
@@ -6,10 +6,8 @@ use clap::Parser;
|
|||||||
use config::{CalendarStoreConfig, SqliteCalendarStoreConfig, TomlCalendarStoreConfig};
|
use config::{CalendarStoreConfig, SqliteCalendarStoreConfig, TomlCalendarStoreConfig};
|
||||||
use rustical_auth::AuthProvider;
|
use rustical_auth::AuthProvider;
|
||||||
use rustical_store::calendar::CalendarStore;
|
use rustical_store::calendar::CalendarStore;
|
||||||
use rustical_store::sqlite_store::SqliteCalendarStore;
|
use rustical_store::sqlite_store::{create_db_pool, SqliteCalendarStore};
|
||||||
use rustical_store::toml_store::TomlCalendarStore;
|
use rustical_store::toml_store::TomlCalendarStore;
|
||||||
use sqlx::sqlite::SqliteConnectOptions;
|
|
||||||
use sqlx::SqlitePool;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
@@ -38,16 +36,7 @@ async fn get_cal_store(
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
CalendarStoreConfig::Sqlite(SqliteCalendarStoreConfig { db_url }) => {
|
CalendarStoreConfig::Sqlite(SqliteCalendarStoreConfig { db_url }) => {
|
||||||
let db = SqlitePool::connect_with(
|
let db = create_db_pool(db_url, migrate).await?;
|
||||||
SqliteConnectOptions::new()
|
|
||||||
.filename(db_url)
|
|
||||||
.create_if_missing(true),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
if migrate {
|
|
||||||
println!("Running database migrations");
|
|
||||||
sqlx::migrate!("./migrations").run(&db).await?;
|
|
||||||
}
|
|
||||||
Arc::new(RwLock::new(SqliteCalendarStore::new(db)))
|
Arc::new(RwLock::new(SqliteCalendarStore::new(db)))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user