feat: add ToDelete state to entity and perform deletion while persisting
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -4,4 +4,5 @@ pub enum E {
|
||||
New,
|
||||
Unknown,
|
||||
Loaded,
|
||||
ToDelete,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user