Skip to content

Commit

Permalink
SQL transaction for deleting task
Browse files Browse the repository at this point in the history
remove task tree  (tasks with they subtasks)
Remove records and reminders related to them
Update task and subtasks position
  • Loading branch information
iman-salmani committed Jul 31, 2023
1 parent 8fee168 commit cf8818f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
49 changes: 27 additions & 22 deletions src/db/operations/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,31 212,36 @@ fn set_subtasks_suspended(
Ok(())
}

pub fn delete_task(task: &Task) -> Result<()> {
pub fn delete_task(task_id: i64) -> Result<()> {
let conn = get_connection();
// Notify: Not return error when id not exists
let task_id = task.id();
conn.execute("DELETE FROM tasks WHERE id = ?", (task_id,))?;
conn.execute("DELETE FROM records WHERE task = ?", (task_id,))?;
conn.execute("DELETE FROM reminders WHERE task = ?", (task_id,))?;
// No return error when id not exists
conn.execute_batch(&format!(
"BEGIN TRANSACTION;
CREATE TEMPORARY TABLE temp_task_tree (id INT, project INT, section INT, position INT, parent INT);
let subtasks = read_tasks(None, None, None, Some(task_id), None, false).unwrap();
for subtask in subtasks {
delete_task(&subtask).unwrap();
}
WITH RECURSIVE cte_task_tree(id, project, section, position, parent) AS (
SELECT id, project, section, position, parent FROM tasks WHERE id={task_id}
UNION ALL
SELECT tasks.id, tasks.project, tasks.section, tasks.position, tasks.parent
FROM tasks
JOIN cte_task_tree ON tasks.parent=cte_task_tree.id
)
INSERT INTO temp_task_tree (id, project, section, position, parent) SELECT * FROM cte_task_tree;
// Decrease upper tasks position
if task.parent() == 0 {
conn.execute(
"UPDATE tasks SET position = position - 1 WHERE position > ?1 AND section = ?2",
(task.position(), task.section()),
)?;
} else {
conn.execute(
"UPDATE tasks SET position = position - 1 WHERE position > ?1 AND parent = ?2",
(task.position(), task.parent()),
)?;
}
DELETE FROM records WHERE task IN (SELECT id from temp_task_tree);
DELETE FROM reminders WHERE task IN (SELECT id from temp_task_tree);
DELETE FROM tasks WHERE id in (SELECT id from temp_task_tree);
UPDATE tasks
SET position = tasks.position - 1
FROM temp_task_tree
WHERE tasks.position > temp_task_tree.position
AND tasks.section = temp_task_tree.section
AND tasks.parent = temp_task_tree.parent;
DROP TABLE temp_task_tree;
COMMIT;"
))?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/views/task/task_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 567,7 @@ impl TaskRow {
move |_toast| {
let task = obj.task();
if task.suspended() { // Checking Undo button
delete_task(&task).unwrap();
delete_task(task.id()).unwrap();
}
}
));
Expand Down

0 comments on commit cf8818f

Please sign in to comment.