mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
Add startup routine to fix orphaned objects
fixes #145, related to #142
This commit is contained in:
@@ -9,7 +9,7 @@ use rustical_store::{
|
||||
};
|
||||
use sqlx::{Acquire, Executor, Sqlite, SqlitePool, Transaction};
|
||||
use tokio::sync::mpsc::Sender;
|
||||
use tracing::{error, instrument};
|
||||
use tracing::{error, instrument, warn};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct AddressObjectRow {
|
||||
@@ -32,6 +32,60 @@ pub struct SqliteAddressbookStore {
|
||||
}
|
||||
|
||||
impl SqliteAddressbookStore {
|
||||
// Commit "orphaned" objects to the changelog table
|
||||
pub async fn repair_orphans(&self) -> Result<(), Error> {
|
||||
struct Row {
|
||||
principal: String,
|
||||
addressbook_id: String,
|
||||
id: String,
|
||||
deleted: bool,
|
||||
}
|
||||
|
||||
let mut tx = self
|
||||
.db
|
||||
.begin_with(BEGIN_IMMEDIATE)
|
||||
.await
|
||||
.map_err(crate::Error::from)?;
|
||||
|
||||
let rows = sqlx::query_as!(
|
||||
Row,
|
||||
r#"
|
||||
SELECT principal, addressbook_id, id, (deleted_at IS NOT NULL) AS "deleted: bool"
|
||||
FROM addressobjects
|
||||
WHERE (principal, addressbook_id, id) NOT IN (
|
||||
SELECT DISTINCT principal, addressbook_id, object_id FROM addressobjectchangelog
|
||||
)
|
||||
;
|
||||
"#,
|
||||
)
|
||||
.fetch_all(&mut *tx)
|
||||
.await
|
||||
.map_err(crate::Error::from)?;
|
||||
|
||||
for row in rows {
|
||||
let operation = if row.deleted {
|
||||
ChangeOperation::Delete
|
||||
} else {
|
||||
ChangeOperation::Add
|
||||
};
|
||||
warn!(
|
||||
"Commiting orphaned addressbook object ({},{},{}), deleted={}",
|
||||
&row.principal, &row.addressbook_id, &row.id, &row.deleted
|
||||
);
|
||||
log_object_operation(
|
||||
&mut tx,
|
||||
&row.principal,
|
||||
&row.addressbook_id,
|
||||
&row.id,
|
||||
operation,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
tx.commit().await.map_err(crate::Error::from)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn _get_addressbook<'e, E: Executor<'e, Database = Sqlite>>(
|
||||
executor: E,
|
||||
principal: &str,
|
||||
|
||||
@@ -11,7 +11,7 @@ use rustical_store::{CollectionOperation, CollectionOperationInfo};
|
||||
use sqlx::types::chrono::NaiveDateTime;
|
||||
use sqlx::{Acquire, Executor, Sqlite, SqlitePool, Transaction};
|
||||
use tokio::sync::mpsc::Sender;
|
||||
use tracing::{error, instrument};
|
||||
use tracing::{error, instrument, warn};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct CalendarObjectRow {
|
||||
@@ -94,6 +94,53 @@ pub struct SqliteCalendarStore {
|
||||
}
|
||||
|
||||
impl SqliteCalendarStore {
|
||||
// Commit "orphaned" objects to the changelog table
|
||||
pub async fn repair_orphans(&self) -> Result<(), Error> {
|
||||
struct Row {
|
||||
principal: String,
|
||||
cal_id: String,
|
||||
id: String,
|
||||
deleted: bool,
|
||||
}
|
||||
|
||||
let mut tx = self
|
||||
.db
|
||||
.begin_with(BEGIN_IMMEDIATE)
|
||||
.await
|
||||
.map_err(crate::Error::from)?;
|
||||
|
||||
let rows = sqlx::query_as!(
|
||||
Row,
|
||||
r#"
|
||||
SELECT principal, cal_id, id, (deleted_at IS NOT NULL) AS "deleted: bool"
|
||||
FROM calendarobjects
|
||||
WHERE (principal, cal_id, id) NOT IN (
|
||||
SELECT DISTINCT principal, cal_id, object_id FROM calendarobjectchangelog
|
||||
)
|
||||
;
|
||||
"#,
|
||||
)
|
||||
.fetch_all(&mut *tx)
|
||||
.await
|
||||
.map_err(crate::Error::from)?;
|
||||
|
||||
for row in rows {
|
||||
let operation = if row.deleted {
|
||||
ChangeOperation::Delete
|
||||
} else {
|
||||
ChangeOperation::Add
|
||||
};
|
||||
warn!(
|
||||
"Commiting orphaned calendar object ({},{},{}), deleted={}",
|
||||
&row.principal, &row.cal_id, &row.id, &row.deleted
|
||||
);
|
||||
log_object_operation(&mut tx, &row.principal, &row.cal_id, &row.id, operation).await?;
|
||||
}
|
||||
tx.commit().await.map_err(crate::Error::from)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn _get_calendar<'e, E: Executor<'e, Database = Sqlite>>(
|
||||
executor: E,
|
||||
principal: &str,
|
||||
|
||||
Reference in New Issue
Block a user