feat: add ToDelete state to entity and perform deletion while persisting

This commit is contained in:
2025-10-06 07:52:12 +02:00
parent 4c95b709c0
commit 8c0afe0c9c
3 changed files with 22 additions and 2 deletions

View File

@@ -41,6 +41,14 @@ fn write_attribute(
Ok(())
}
fn delete_entity(entity: &Entity, transaction: &rusqlite::Transaction) -> Result<(), FailedTo> {
let entity_id = [&entity.id];
transaction
.execute(DELETE_ENTITY_STATEMENT, entity_id)
.map_err(|_| sqlite::FailedTo::ExecuteStatement)?;
Ok(())
}
fn write_entity(entity: &Entity, transaction: &rusqlite::Transaction) -> Result<(), FailedTo> {
let entity_id = [&entity.id];
let entity_values = (&entity.id, &entity.class, entity.ref_date);
@@ -64,9 +72,13 @@ pub fn run(path: &path::Path, catalog: &Catalog) -> result::Result<(), FailedTo>
for entity in catalog
.items
.values()
.filter(|item| item.state == EntityState::New)
.filter(|item| item.state == EntityState::New || item.state == EntityState::ToDelete)
{
write_entity(entity, &transaction)?;
match entity.state {
EntityState::New => write_entity(entity, &transaction)?,
EntityState::ToDelete => delete_entity(entity, &transaction)?,
_ => unreachable!(),
}
}
transaction
.commit()

View File

@@ -138,6 +138,13 @@ impl O {
.map(|item| T::from(item.clone()))
}
pub fn delete(&mut self, id: &str) {
let entity = self.items.get_mut(id);
if let Some(entity) = entity {
entity.state = EntityState::ToDelete;
}
}
/// Persists the current state of the catalog to the database.
pub fn persist(&self) -> result::Result<(), FailedTo> {
let path = path::Path::new(&self.path);

View File

@@ -4,4 +4,5 @@ pub enum E {
New,
Unknown,
Loaded,
ToDelete,
}