mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 05:52:19 +00:00
split store and store_sqlite implementatio into multiple crates
This commit is contained in:
47
crates/store_sqlite/src/lib.rs
Normal file
47
crates/store_sqlite/src/lib.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use serde::Serialize;
|
||||
use sqlx::{sqlite::SqliteConnectOptions, Pool, Sqlite, SqlitePool};
|
||||
|
||||
pub mod addressbook_store;
|
||||
pub mod calendar_store;
|
||||
pub mod error;
|
||||
|
||||
pub use error::Error;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, sqlx::Type)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub(crate) enum ChangeOperation {
|
||||
// There's no distinction between Add and Modify
|
||||
Add,
|
||||
Delete,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SqliteStore {
|
||||
db: SqlitePool,
|
||||
}
|
||||
|
||||
impl SqliteStore {
|
||||
pub fn new(db: SqlitePool) -> Self {
|
||||
Self { db }
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
pub async fn create_test_store() -> anyhow::Result<SqliteStore> {
|
||||
let db = SqlitePool::connect("sqlite::memory:").await?;
|
||||
sqlx::migrate!("./migrations").run(&db).await?;
|
||||
Ok(SqliteStore::new(db))
|
||||
}
|
||||
Reference in New Issue
Block a user