split store and store_sqlite implementatio into multiple crates

This commit is contained in:
Lennart
2024-10-28 15:34:20 +01:00
parent 53d2ea10e6
commit c013ffa117
13 changed files with 213 additions and 107 deletions

16
Cargo.lock generated
View File

@@ -2519,6 +2519,7 @@ dependencies = [
"rustical_carddav", "rustical_carddav",
"rustical_frontend", "rustical_frontend",
"rustical_store", "rustical_store",
"rustical_store_sqlite",
"serde", "serde",
"sqlx", "sqlx",
"tokio", "tokio",
@@ -2632,16 +2633,25 @@ dependencies = [
"lazy_static", "lazy_static",
"password-auth", "password-auth",
"pbkdf2", "pbkdf2",
"rand_core",
"regex", "regex",
"rstest", "rstest",
"rstest_reuse", "rstest_reuse",
"serde", "serde",
"sha2", "sha2",
"thiserror",
"tracing",
]
[[package]]
name = "rustical_store_sqlite"
version = "0.1.0"
dependencies = [
"anyhow",
"async-trait",
"rustical_store",
"serde",
"sqlx", "sqlx",
"thiserror", "thiserror",
"tokio",
"toml",
"tracing", "tracing",
] ]

View File

@@ -73,6 +73,7 @@ ical = { version = "0.11", features = ["generator", "serde"] }
toml = "0.8" toml = "0.8"
rustical_dav = { path = "./crates/dav/" } rustical_dav = { path = "./crates/dav/" }
rustical_store = { path = "./crates/store/" } rustical_store = { path = "./crates/store/" }
rustical_store_sqlite = { path = "./crates/store_sqlite/" }
rustical_caldav = { path = "./crates/caldav/" } rustical_caldav = { path = "./crates/caldav/" }
rustical_carddav = { path = "./crates/carddav/" } rustical_carddav = { path = "./crates/carddav/" }
rustical_frontend = { path = "./crates/frontend/" } rustical_frontend = { path = "./crates/frontend/" }
@@ -80,6 +81,7 @@ chrono-tz = "0.10.0"
[dependencies] [dependencies]
rustical_store = { workspace = true } rustical_store = { workspace = true }
rustical_store_sqlite = { workspace = true }
rustical_caldav = { workspace = true } rustical_caldav = { workspace = true }
rustical_carddav = { workspace = true } rustical_carddav = { workspace = true }
rustical_frontend = { workspace = true } rustical_frontend = { workspace = true }

View File

@@ -11,15 +11,10 @@ anyhow = { workspace = true }
async-trait = { workspace = true } async-trait = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
sha2 = { workspace = true } sha2 = { workspace = true }
sqlx = { workspace = true }
tokio = { workspace = true }
toml = { workspace = true }
ical = { workspace = true } ical = { workspace = true }
chrono = { workspace = true } chrono = { workspace = true }
regex = { workspace = true } regex = { workspace = true }
lazy_static = { workspace = true } lazy_static = { workspace = true }
rstest = { workspace = true }
rstest_reuse = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
password-auth = { workspace = true } password-auth = { workspace = true }
actix-web = { workspace = true } actix-web = { workspace = true }
@@ -27,6 +22,9 @@ actix-session = { workspace = true }
actix-web-httpauth = { workspace = true } actix-web-httpauth = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
pbkdf2 = { workspace = true } pbkdf2 = { workspace = true }
rand_core = { workspace = true }
chrono-tz = { workspace = true } chrono-tz = { workspace = true }
derive_more = { workspace = true } derive_more = { workspace = true }
[dev-dependencies]
rstest = { workspace = true }
rstest_reuse = { workspace = true }

View File

@@ -1,4 +1,5 @@
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum Error { pub enum Error {
#[error("Not found")] #[error("Not found")]
NotFound, NotFound,
@@ -9,21 +10,9 @@ pub enum Error {
#[error("Invalid ics/vcf input: {0}")] #[error("Invalid ics/vcf input: {0}")]
InvalidData(String), InvalidData(String),
#[error(transparent)]
SqlxError(sqlx::Error),
#[error(transparent)] #[error(transparent)]
Other(#[from] anyhow::Error), Other(#[from] anyhow::Error),
#[error(transparent)] #[error(transparent)]
ParserError(#[from] ical::parser::ParserError), ParserError(#[from] ical::parser::ParserError),
} }
impl From<sqlx::Error> for Error {
fn from(value: sqlx::Error) -> Self {
match value {
sqlx::Error::RowNotFound => Error::NotFound,
err => Error::SqlxError(err),
}
}
}

View File

@@ -2,7 +2,6 @@ pub mod addressbook_store;
pub mod calendar_store; pub mod calendar_store;
pub mod error; pub mod error;
pub mod model; pub mod model;
pub mod sqlite_store;
pub mod timestamp; pub mod timestamp;
pub use error::Error; pub use error::Error;
pub mod auth; pub mod auth;

View File

@@ -0,0 +1,16 @@
[package]
name = "rustical_store_sqlite"
version.workspace = true
edition.workspace = true
description.workspace = true
repository.workspace = true
publish = false
[dependencies]
rustical_store = { workspace = true }
anyhow = { workspace = true }
async-trait = { workspace = true }
serde = { workspace = true }
sqlx = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }

View File

@@ -1,10 +1,10 @@
use super::{ChangeOperation, SqliteStore}; use super::{ChangeOperation, SqliteStore};
use crate::Error; use crate::Error;
use crate::{ use async_trait::async_trait;
use rustical_store::{
model::{AddressObject, Addressbook}, model::{AddressObject, Addressbook},
AddressbookStore, AddressbookStore,
}; };
use async_trait::async_trait;
use sqlx::{Sqlite, Transaction}; use sqlx::{Sqlite, Transaction};
use tracing::instrument; use tracing::instrument;
@@ -18,7 +18,7 @@ impl TryFrom<AddressObjectRow> for AddressObject {
type Error = Error; type Error = Error;
fn try_from(value: AddressObjectRow) -> Result<Self, Error> { fn try_from(value: AddressObjectRow) -> Result<Self, Error> {
Self::from_vcf(value.id, value.vcf) Ok(Self::from_vcf(value.id, value.vcf)?)
} }
} }
@@ -60,7 +60,11 @@ async fn log_object_operation(
#[async_trait] #[async_trait]
impl AddressbookStore for SqliteStore { impl AddressbookStore for SqliteStore {
#[instrument] #[instrument]
async fn get_addressbook(&self, principal: &str, id: &str) -> Result<Addressbook, Error> { async fn get_addressbook(
&self,
principal: &str,
id: &str,
) -> Result<Addressbook, rustical_store::Error> {
let addressbook = sqlx::query_as!( let addressbook = sqlx::query_as!(
Addressbook, Addressbook,
r#"SELECT principal, id, synctoken, displayname, description, deleted_at r#"SELECT principal, id, synctoken, displayname, description, deleted_at
@@ -70,12 +74,16 @@ impl AddressbookStore for SqliteStore {
id id
) )
.fetch_one(&self.db) .fetch_one(&self.db)
.await?; .await
.map_err(Error::SqlxError)?;
Ok(addressbook) Ok(addressbook)
} }
#[instrument] #[instrument]
async fn get_addressbooks(&self, principal: &str) -> Result<Vec<Addressbook>, Error> { async fn get_addressbooks(
&self,
principal: &str,
) -> Result<Vec<Addressbook>, rustical_store::Error> {
let addressbooks = sqlx::query_as!( let addressbooks = sqlx::query_as!(
Addressbook, Addressbook,
r#"SELECT principal, id, synctoken, displayname, description, deleted_at r#"SELECT principal, id, synctoken, displayname, description, deleted_at
@@ -84,7 +92,8 @@ impl AddressbookStore for SqliteStore {
principal principal
) )
.fetch_all(&self.db) .fetch_all(&self.db)
.await?; .await
.map_err(Error::SqlxError)?;
Ok(addressbooks) Ok(addressbooks)
} }
@@ -94,7 +103,7 @@ impl AddressbookStore for SqliteStore {
principal: String, principal: String,
id: String, id: String,
addressbook: Addressbook, addressbook: Addressbook,
) -> Result<(), Error> { ) -> Result<(), rustical_store::Error> {
let result = sqlx::query!( let result = sqlx::query!(
r#"UPDATE addressbooks SET principal = ?, id = ?, displayname = ?, description = ? r#"UPDATE addressbooks SET principal = ?, id = ?, displayname = ?, description = ?
WHERE (principal, id) = (?, ?)"#, WHERE (principal, id) = (?, ?)"#,
@@ -106,15 +115,19 @@ impl AddressbookStore for SqliteStore {
id id
) )
.execute(&self.db) .execute(&self.db)
.await?; .await
.map_err(Error::SqlxError)?;
if result.rows_affected() == 0 { if result.rows_affected() == 0 {
return Err(Error::NotFound); return Err(rustical_store::Error::NotFound);
} }
Ok(()) Ok(())
} }
#[instrument] #[instrument]
async fn insert_addressbook(&self, addressbook: Addressbook) -> Result<(), Error> { async fn insert_addressbook(
&self,
addressbook: Addressbook,
) -> Result<(), rustical_store::Error> {
sqlx::query!( sqlx::query!(
r#"INSERT INTO addressbooks (principal, id, displayname, description) r#"INSERT INTO addressbooks (principal, id, displayname, description)
VALUES (?, ?, ?, ?)"#, VALUES (?, ?, ?, ?)"#,
@@ -124,7 +137,8 @@ impl AddressbookStore for SqliteStore {
addressbook.description, addressbook.description,
) )
.execute(&self.db) .execute(&self.db)
.await?; .await
.map_err(Error::SqlxError)?;
Ok(()) Ok(())
} }
@@ -134,7 +148,7 @@ impl AddressbookStore for SqliteStore {
principal: &str, principal: &str,
addressbook_id: &str, addressbook_id: &str,
use_trashbin: bool, use_trashbin: bool,
) -> Result<(), Error> { ) -> Result<(), rustical_store::Error> {
match use_trashbin { match use_trashbin {
true => { true => {
sqlx::query!( sqlx::query!(
@@ -142,7 +156,7 @@ impl AddressbookStore for SqliteStore {
principal, addressbook_id principal, addressbook_id
) )
.execute(&self.db) .execute(&self.db)
.await?; .await.map_err(Error::SqlxError)?;
} }
false => { false => {
sqlx::query!( sqlx::query!(
@@ -151,7 +165,8 @@ impl AddressbookStore for SqliteStore {
addressbook_id addressbook_id
) )
.execute(&self.db) .execute(&self.db)
.await?; .await
.map_err(Error::SqlxError)?;
} }
}; };
Ok(()) Ok(())
@@ -162,14 +177,15 @@ impl AddressbookStore for SqliteStore {
&self, &self,
principal: &str, principal: &str,
addressbook_id: &str, addressbook_id: &str,
) -> Result<(), Error> { ) -> Result<(), rustical_store::Error> {
sqlx::query!( sqlx::query!(
r"UPDATE addressbooks SET deleted_at = NULL WHERE (principal, id) = (?, ?)", r"UPDATE addressbooks SET deleted_at = NULL WHERE (principal, id) = (?, ?)",
principal, principal,
addressbook_id addressbook_id
) )
.execute(&self.db) .execute(&self.db)
.await?; .await
.map_err(Error::SqlxError)?;
Ok(()) Ok(())
} }
@@ -179,7 +195,7 @@ impl AddressbookStore for SqliteStore {
principal: &str, principal: &str,
addressbook_id: &str, addressbook_id: &str,
synctoken: i64, synctoken: i64,
) -> Result<(Vec<AddressObject>, Vec<String>, i64), Error> { ) -> Result<(Vec<AddressObject>, Vec<String>, i64), rustical_store::Error> {
struct Row { struct Row {
object_id: String, object_id: String,
synctoken: i64, synctoken: i64,
@@ -194,7 +210,7 @@ impl AddressbookStore for SqliteStore {
synctoken synctoken
) )
.fetch_all(&self.db) .fetch_all(&self.db)
.await?; .await.map_err(Error::SqlxError)?;
let mut objects = vec![]; let mut objects = vec![];
let mut deleted_objects = vec![]; let mut deleted_objects = vec![];
@@ -207,7 +223,7 @@ impl AddressbookStore for SqliteStore {
for Row { object_id, .. } in changes { for Row { object_id, .. } in changes {
match self.get_object(principal, addressbook_id, &object_id).await { match self.get_object(principal, addressbook_id, &object_id).await {
Ok(object) => objects.push(object), Ok(object) => objects.push(object),
Err(Error::NotFound) => deleted_objects.push(object_id), Err(rustical_store::Error::NotFound) => deleted_objects.push(object_id),
Err(err) => return Err(err), Err(err) => return Err(err),
} }
} }
@@ -220,7 +236,7 @@ impl AddressbookStore for SqliteStore {
&self, &self,
principal: &str, principal: &str,
addressbook_id: &str, addressbook_id: &str,
) -> Result<Vec<AddressObject>, Error> { ) -> Result<Vec<AddressObject>, rustical_store::Error> {
sqlx::query_as!( sqlx::query_as!(
AddressObjectRow, AddressObjectRow,
"SELECT id, vcf FROM addressobjects WHERE principal = ? AND addressbook_id = ? AND deleted_at IS NULL", "SELECT id, vcf FROM addressobjects WHERE principal = ? AND addressbook_id = ? AND deleted_at IS NULL",
@@ -228,9 +244,9 @@ impl AddressbookStore for SqliteStore {
addressbook_id addressbook_id
) )
.fetch_all(&self.db) .fetch_all(&self.db)
.await? .await.map_err(Error::SqlxError)?
.into_iter() .into_iter()
.map(|row| row.try_into()) .map(|row| row.try_into().map_err(rustical_store::Error::from))
.collect() .collect()
} }
@@ -240,7 +256,7 @@ impl AddressbookStore for SqliteStore {
principal: &str, principal: &str,
addressbook_id: &str, addressbook_id: &str,
object_id: &str, object_id: &str,
) -> Result<AddressObject, Error> { ) -> Result<AddressObject, rustical_store::Error> {
Ok(sqlx::query_as!( Ok(sqlx::query_as!(
AddressObjectRow, AddressObjectRow,
"SELECT id, vcf FROM addressobjects WHERE (principal, addressbook_id, id) = (?, ?, ?)", "SELECT id, vcf FROM addressobjects WHERE (principal, addressbook_id, id) = (?, ?, ?)",
@@ -249,7 +265,8 @@ impl AddressbookStore for SqliteStore {
object_id object_id
) )
.fetch_one(&self.db) .fetch_one(&self.db)
.await? .await
.map_err(Error::SqlxError)?
.try_into()?) .try_into()?)
} }
@@ -261,8 +278,8 @@ impl AddressbookStore for SqliteStore {
object: AddressObject, object: AddressObject,
// TODO: implement // TODO: implement
overwrite: bool, overwrite: bool,
) -> Result<(), Error> { ) -> Result<(), rustical_store::Error> {
let mut tx = self.db.begin().await?; let mut tx = self.db.begin().await.map_err(Error::SqlxError)?;
let (object_id, vcf) = (object.get_id(), object.get_vcf()); let (object_id, vcf) = (object.get_id(), object.get_vcf());
@@ -274,7 +291,8 @@ impl AddressbookStore for SqliteStore {
vcf vcf
) )
.execute(&mut *tx) .execute(&mut *tx)
.await?; .await
.map_err(Error::SqlxError)?;
log_object_operation( log_object_operation(
&mut tx, &mut tx,
@@ -283,9 +301,10 @@ impl AddressbookStore for SqliteStore {
object_id, object_id,
ChangeOperation::Add, ChangeOperation::Add,
) )
.await?; .await
.map_err(rustical_store::Error::from)?;
tx.commit().await?; tx.commit().await.map_err(Error::SqlxError)?;
Ok(()) Ok(())
} }
@@ -296,8 +315,8 @@ impl AddressbookStore for SqliteStore {
addressbook_id: &str, addressbook_id: &str,
object_id: &str, object_id: &str,
use_trashbin: bool, use_trashbin: bool,
) -> Result<(), Error> { ) -> Result<(), rustical_store::Error> {
let mut tx = self.db.begin().await?; let mut tx = self.db.begin().await.map_err(Error::SqlxError)?;
match use_trashbin { match use_trashbin {
true => { true => {
@@ -308,7 +327,7 @@ impl AddressbookStore for SqliteStore {
object_id object_id
) )
.execute(&mut *tx) .execute(&mut *tx)
.await?; .await.map_err(Error::SqlxError)?;
} }
false => { false => {
sqlx::query!( sqlx::query!(
@@ -317,7 +336,8 @@ impl AddressbookStore for SqliteStore {
object_id object_id
) )
.execute(&mut *tx) .execute(&mut *tx)
.await?; .await
.map_err(Error::SqlxError)?;
} }
}; };
log_object_operation( log_object_operation(
@@ -327,8 +347,9 @@ impl AddressbookStore for SqliteStore {
object_id, object_id,
ChangeOperation::Delete, ChangeOperation::Delete,
) )
.await?; .await
tx.commit().await?; .map_err(rustical_store::Error::from)?;
tx.commit().await.map_err(Error::SqlxError)?;
Ok(()) Ok(())
} }
@@ -338,8 +359,8 @@ impl AddressbookStore for SqliteStore {
principal: &str, principal: &str,
addressbook_id: &str, addressbook_id: &str,
object_id: &str, object_id: &str,
) -> Result<(), Error> { ) -> Result<(), rustical_store::Error> {
let mut tx = self.db.begin().await?; let mut tx = self.db.begin().await.map_err(Error::SqlxError)?;
sqlx::query!( sqlx::query!(
r#"UPDATE addressobjects SET deleted_at = NULL, updated_at = datetime() WHERE (principal, addressbook_id, id) = (?, ?, ?)"#, r#"UPDATE addressobjects SET deleted_at = NULL, updated_at = datetime() WHERE (principal, addressbook_id, id) = (?, ?, ?)"#,
@@ -348,7 +369,7 @@ impl AddressbookStore for SqliteStore {
object_id object_id
) )
.execute(&mut *tx) .execute(&mut *tx)
.await?; .await.map_err(Error::SqlxError)?;
log_object_operation( log_object_operation(
&mut tx, &mut tx,
@@ -357,8 +378,9 @@ impl AddressbookStore for SqliteStore {
object_id, object_id,
ChangeOperation::Add, ChangeOperation::Add,
) )
.await?; .await
tx.commit().await?; .map_err(rustical_store::Error::from)?;
tx.commit().await.map_err(Error::SqlxError)?;
Ok(()) Ok(())
} }
} }

View File

@@ -1,9 +1,10 @@
use super::{ChangeOperation, SqliteStore}; use super::{ChangeOperation, SqliteStore};
use crate::model::object::CalendarObject; use crate::Error;
use crate::model::Calendar;
use crate::{CalendarStore, Error};
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use rustical_store::model::object::CalendarObject;
use rustical_store::model::Calendar;
use rustical_store::CalendarStore;
use sqlx::Sqlite; use sqlx::Sqlite;
use sqlx::Transaction; use sqlx::Transaction;
use tracing::instrument; use tracing::instrument;
@@ -15,9 +16,9 @@ struct CalendarObjectRow {
} }
impl TryFrom<CalendarObjectRow> for CalendarObject { impl TryFrom<CalendarObjectRow> for CalendarObject {
type Error = Error; type Error = rustical_store::Error;
fn try_from(value: CalendarObjectRow) -> Result<Self, Error> { fn try_from(value: CalendarObjectRow) -> Result<Self, Self::Error> {
CalendarObject::from_ics(value.id, value.ics) CalendarObject::from_ics(value.id, value.ics)
} }
} }
@@ -29,7 +30,7 @@ async fn log_object_operation(
cal_id: &str, cal_id: &str,
object_id: &str, object_id: &str,
operation: ChangeOperation, operation: ChangeOperation,
) -> Result<(), Error> { ) -> Result<(), rustical_store::Error> {
sqlx::query!( sqlx::query!(
r#" r#"
UPDATE calendars UPDATE calendars
@@ -39,7 +40,8 @@ async fn log_object_operation(
cal_id cal_id
) )
.execute(&mut **tx) .execute(&mut **tx)
.await?; .await
.map_err(Error::SqlxError)?;
sqlx::query!( sqlx::query!(
r#" r#"
@@ -53,14 +55,19 @@ async fn log_object_operation(
operation operation
) )
.execute(&mut **tx) .execute(&mut **tx)
.await?; .await
.map_err(Error::SqlxError)?;
Ok(()) Ok(())
} }
#[async_trait] #[async_trait]
impl CalendarStore for SqliteStore { impl CalendarStore for SqliteStore {
#[instrument] #[instrument]
async fn get_calendar(&self, principal: &str, id: &str) -> Result<Calendar, Error> { async fn get_calendar(
&self,
principal: &str,
id: &str,
) -> Result<Calendar, rustical_store::Error> {
let cal = sqlx::query_as!( let cal = sqlx::query_as!(
Calendar, Calendar,
r#"SELECT principal, id, synctoken, "order", displayname, description, color, timezone, deleted_at r#"SELECT principal, id, synctoken, "order", displayname, description, color, timezone, deleted_at
@@ -70,12 +77,12 @@ impl CalendarStore for SqliteStore {
id id
) )
.fetch_one(&self.db) .fetch_one(&self.db)
.await?; .await.map_err(Error::SqlxError)?;
Ok(cal) Ok(cal)
} }
#[instrument] #[instrument]
async fn get_calendars(&self, principal: &str) -> Result<Vec<Calendar>, Error> { async fn get_calendars(&self, principal: &str) -> Result<Vec<Calendar>, rustical_store::Error> {
let cals = sqlx::query_as!( let cals = sqlx::query_as!(
Calendar, Calendar,
r#"SELECT principal, id, synctoken, displayname, "order", description, color, timezone, deleted_at r#"SELECT principal, id, synctoken, displayname, "order", description, color, timezone, deleted_at
@@ -84,12 +91,12 @@ impl CalendarStore for SqliteStore {
principal principal
) )
.fetch_all(&self.db) .fetch_all(&self.db)
.await?; .await.map_err(Error::SqlxError)?;
Ok(cals) Ok(cals)
} }
#[instrument] #[instrument]
async fn insert_calendar(&self, calendar: Calendar) -> Result<(), Error> { async fn insert_calendar(&self, calendar: Calendar) -> Result<(), rustical_store::Error> {
sqlx::query!( sqlx::query!(
r#"INSERT INTO calendars (principal, id, displayname, description, "order", color, timezone) r#"INSERT INTO calendars (principal, id, displayname, description, "order", color, timezone)
VALUES (?, ?, ?, ?, ?, ?, ?)"#, VALUES (?, ?, ?, ?, ?, ?, ?)"#,
@@ -102,7 +109,7 @@ impl CalendarStore for SqliteStore {
calendar.timezone calendar.timezone
) )
.execute(&self.db) .execute(&self.db)
.await?; .await.map_err(Error::SqlxError)?;
Ok(()) Ok(())
} }
@@ -112,7 +119,7 @@ impl CalendarStore for SqliteStore {
principal: String, principal: String,
id: String, id: String,
calendar: Calendar, calendar: Calendar,
) -> Result<(), Error> { ) -> Result<(), rustical_store::Error> {
let result = sqlx::query!( let result = sqlx::query!(
r#"UPDATE calendars SET principal = ?, id = ?, displayname = ?, description = ?, "order" = ?, color = ?, timezone = ? r#"UPDATE calendars SET principal = ?, id = ?, displayname = ?, description = ?, "order" = ?, color = ?, timezone = ?
WHERE (principal, id) = (?, ?)"#, WHERE (principal, id) = (?, ?)"#,
@@ -125,9 +132,9 @@ impl CalendarStore for SqliteStore {
calendar.timezone, calendar.timezone,
principal, principal,
id id
).execute(&self.db).await?; ).execute(&self.db).await.map_err(Error::SqlxError)?;
if result.rows_affected() == 0 { if result.rows_affected() == 0 {
return Err(Error::NotFound); return Err(rustical_store::Error::NotFound);
} }
Ok(()) Ok(())
} }
@@ -139,7 +146,7 @@ impl CalendarStore for SqliteStore {
principal: &str, principal: &str,
id: &str, id: &str,
use_trashbin: bool, use_trashbin: bool,
) -> Result<(), Error> { ) -> Result<(), rustical_store::Error> {
match use_trashbin { match use_trashbin {
true => { true => {
sqlx::query!( sqlx::query!(
@@ -147,7 +154,7 @@ impl CalendarStore for SqliteStore {
principal, id principal, id
) )
.execute(&self.db) .execute(&self.db)
.await?; .await.map_err(Error::SqlxError)?;
} }
false => { false => {
sqlx::query!( sqlx::query!(
@@ -156,21 +163,27 @@ impl CalendarStore for SqliteStore {
id id
) )
.execute(&self.db) .execute(&self.db)
.await?; .await
.map_err(Error::SqlxError)?;
} }
}; };
Ok(()) Ok(())
} }
#[instrument] #[instrument]
async fn restore_calendar(&self, principal: &str, id: &str) -> Result<(), Error> { async fn restore_calendar(
&self,
principal: &str,
id: &str,
) -> Result<(), rustical_store::Error> {
sqlx::query!( sqlx::query!(
r"UPDATE calendars SET deleted_at = NULL WHERE (principal, id) = (?, ?)", r"UPDATE calendars SET deleted_at = NULL WHERE (principal, id) = (?, ?)",
principal, principal,
id id
) )
.execute(&self.db) .execute(&self.db)
.await?; .await
.map_err(Error::SqlxError)?;
Ok(()) Ok(())
} }
@@ -179,7 +192,7 @@ impl CalendarStore for SqliteStore {
&self, &self,
principal: &str, principal: &str,
cal_id: &str, cal_id: &str,
) -> Result<Vec<CalendarObject>, Error> { ) -> Result<Vec<CalendarObject>, rustical_store::Error> {
sqlx::query_as!( sqlx::query_as!(
CalendarObjectRow, CalendarObjectRow,
"SELECT id, ics FROM calendarobjects WHERE principal = ? AND cal_id = ? AND deleted_at IS NULL", "SELECT id, ics FROM calendarobjects WHERE principal = ? AND cal_id = ? AND deleted_at IS NULL",
@@ -187,9 +200,9 @@ impl CalendarStore for SqliteStore {
cal_id cal_id
) )
.fetch_all(&self.db) .fetch_all(&self.db)
.await? .await.map_err(Error::SqlxError)?
.into_iter() .into_iter()
.map(|row| row.try_into()) .map(|row| row.try_into().map_err(rustical_store::Error::from))
.collect() .collect()
} }
@@ -199,7 +212,7 @@ impl CalendarStore for SqliteStore {
principal: &str, principal: &str,
cal_id: &str, cal_id: &str,
object_id: &str, object_id: &str,
) -> Result<CalendarObject, Error> { ) -> Result<CalendarObject, rustical_store::Error> {
Ok(sqlx::query_as!( Ok(sqlx::query_as!(
CalendarObjectRow, CalendarObjectRow,
"SELECT id, ics FROM calendarobjects WHERE (principal, cal_id, id) = (?, ?, ?)", "SELECT id, ics FROM calendarobjects WHERE (principal, cal_id, id) = (?, ?, ?)",
@@ -208,7 +221,8 @@ impl CalendarStore for SqliteStore {
object_id object_id
) )
.fetch_one(&self.db) .fetch_one(&self.db)
.await? .await
.map_err(Error::SqlxError)?
.try_into()?) .try_into()?)
} }
@@ -220,8 +234,8 @@ impl CalendarStore for SqliteStore {
object: CalendarObject, object: CalendarObject,
// TODO: implement // TODO: implement
overwrite: bool, overwrite: bool,
) -> Result<(), Error> { ) -> Result<(), rustical_store::Error> {
let mut tx = self.db.begin().await?; let mut tx = self.db.begin().await.map_err(Error::SqlxError)?;
let (object_id, ics) = (object.get_id(), object.get_ics()); let (object_id, ics) = (object.get_id(), object.get_ics());
@@ -233,7 +247,8 @@ impl CalendarStore for SqliteStore {
ics ics
) )
.execute(&mut *tx) .execute(&mut *tx)
.await?; .await
.map_err(Error::SqlxError)?;
log_object_operation( log_object_operation(
&mut tx, &mut tx,
@@ -244,7 +259,7 @@ impl CalendarStore for SqliteStore {
) )
.await?; .await?;
tx.commit().await?; tx.commit().await.map_err(Error::SqlxError)?;
Ok(()) Ok(())
} }
@@ -255,8 +270,8 @@ impl CalendarStore for SqliteStore {
cal_id: &str, cal_id: &str,
id: &str, id: &str,
use_trashbin: bool, use_trashbin: bool,
) -> Result<(), Error> { ) -> Result<(), rustical_store::Error> {
let mut tx = self.db.begin().await?; let mut tx = self.db.begin().await.map_err(Error::SqlxError)?;
match use_trashbin { match use_trashbin {
true => { true => {
@@ -267,7 +282,7 @@ impl CalendarStore for SqliteStore {
id id
) )
.execute(&mut *tx) .execute(&mut *tx)
.await?; .await.map_err(Error::SqlxError)?;
} }
false => { false => {
sqlx::query!( sqlx::query!(
@@ -276,11 +291,12 @@ impl CalendarStore for SqliteStore {
id id
) )
.execute(&mut *tx) .execute(&mut *tx)
.await?; .await
.map_err(Error::SqlxError)?;
} }
}; };
log_object_operation(&mut tx, principal, cal_id, id, ChangeOperation::Delete).await?; log_object_operation(&mut tx, principal, cal_id, id, ChangeOperation::Delete).await?;
tx.commit().await?; tx.commit().await.map_err(Error::SqlxError)?;
Ok(()) Ok(())
} }
@@ -290,8 +306,8 @@ impl CalendarStore for SqliteStore {
principal: &str, principal: &str,
cal_id: &str, cal_id: &str,
object_id: &str, object_id: &str,
) -> Result<(), Error> { ) -> Result<(), rustical_store::Error> {
let mut tx = self.db.begin().await?; let mut tx = self.db.begin().await.map_err(Error::SqlxError)?;
sqlx::query!( sqlx::query!(
r#"UPDATE calendarobjects SET deleted_at = NULL, updated_at = datetime() WHERE (principal, cal_id, id) = (?, ?, ?)"#, r#"UPDATE calendarobjects SET deleted_at = NULL, updated_at = datetime() WHERE (principal, cal_id, id) = (?, ?, ?)"#,
@@ -300,10 +316,10 @@ impl CalendarStore for SqliteStore {
object_id object_id
) )
.execute(&mut *tx) .execute(&mut *tx)
.await?; .await.map_err(Error::SqlxError)?;
log_object_operation(&mut tx, principal, cal_id, object_id, ChangeOperation::Add).await?; log_object_operation(&mut tx, principal, cal_id, object_id, ChangeOperation::Add).await?;
tx.commit().await?; tx.commit().await.map_err(Error::SqlxError)?;
Ok(()) Ok(())
} }
@@ -313,7 +329,7 @@ impl CalendarStore for SqliteStore {
principal: &str, principal: &str,
cal_id: &str, cal_id: &str,
synctoken: i64, synctoken: i64,
) -> Result<(Vec<CalendarObject>, Vec<String>, i64), Error> { ) -> Result<(Vec<CalendarObject>, Vec<String>, i64), rustical_store::Error> {
struct Row { struct Row {
object_id: String, object_id: String,
synctoken: i64, synctoken: i64,
@@ -328,7 +344,7 @@ impl CalendarStore for SqliteStore {
synctoken synctoken
) )
.fetch_all(&self.db) .fetch_all(&self.db)
.await?; .await.map_err(Error::SqlxError)?;
let mut objects = vec![]; let mut objects = vec![];
let mut deleted_objects = vec![]; let mut deleted_objects = vec![];
@@ -341,7 +357,7 @@ impl CalendarStore for SqliteStore {
for Row { object_id, .. } in changes { for Row { object_id, .. } in changes {
match self.get_object(principal, cal_id, &object_id).await { match self.get_object(principal, cal_id, &object_id).await {
Ok(object) => objects.push(object), Ok(object) => objects.push(object),
Err(Error::NotFound) => deleted_objects.push(object_id), Err(rustical_store::Error::NotFound) => deleted_objects.push(object_id),
Err(err) => return Err(err), Err(err) => return Err(err),
} }
} }

View File

@@ -0,0 +1,51 @@
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Not found")]
NotFound,
#[error("Resource already exists and overwrite=false")]
AlreadyExists,
#[error("Invalid ics/vcf input: {0}")]
InvalidData(String),
#[error(transparent)]
SqlxError(sqlx::Error),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl From<sqlx::Error> for Error {
fn from(value: sqlx::Error) -> Self {
match value {
sqlx::Error::RowNotFound => Error::NotFound,
err => Error::SqlxError(err),
}
}
}
// TODO: clean up error types
impl From<Error> for rustical_store::Error {
fn from(value: Error) -> Self {
match value {
Error::NotFound => Self::NotFound,
Error::AlreadyExists => Self::AlreadyExists,
Error::InvalidData(b) => Self::InvalidData(b),
Error::SqlxError(err) => Self::Other(err.into()),
Error::Other(err) => Self::Other(err),
}
}
}
impl From<rustical_store::Error> for Error {
fn from(value: rustical_store::Error) -> Self {
match value {
rustical_store::Error::NotFound => Self::NotFound,
rustical_store::Error::AlreadyExists => Self::AlreadyExists,
rustical_store::Error::InvalidData(b) => Self::InvalidData(b),
rustical_store::Error::Other(err) => Self::Other(err),
rustical_store::Error::ParserError(err) => Self::Other(err.into()),
}
}
}

View File

@@ -3,6 +3,9 @@ use sqlx::{sqlite::SqliteConnectOptions, Pool, Sqlite, SqlitePool};
pub mod addressbook_store; pub mod addressbook_store;
pub mod calendar_store; pub mod calendar_store;
pub mod error;
pub use error::Error;
#[derive(Debug, Clone, Serialize, sqlx::Type)] #[derive(Debug, Clone, Serialize, sqlx::Type)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]

View File

@@ -14,8 +14,8 @@ use opentelemetry_sdk::{runtime, Resource};
use opentelemetry_semantic_conventions::resource::{SERVICE_NAME, SERVICE_VERSION}; use opentelemetry_semantic_conventions::resource::{SERVICE_NAME, SERVICE_VERSION};
use opentelemetry_semantic_conventions::SCHEMA_URL; use opentelemetry_semantic_conventions::SCHEMA_URL;
use rustical_store::auth::StaticUserStore; use rustical_store::auth::StaticUserStore;
use rustical_store::sqlite_store::{create_db_pool, SqliteStore};
use rustical_store::{AddressbookStore, CalendarStore}; use rustical_store::{AddressbookStore, CalendarStore};
use rustical_store_sqlite::{create_db_pool, SqliteStore};
use std::fs; use std::fs;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;