diff --git a/src/commands/CmdPurge.cpp b/src/commands/CmdPurge.cpp index 1d9143e43..ef9816e3e 100644 --- a/src/commands/CmdPurge.cpp +++ b/src/commands/CmdPurge.cpp @@ -51,6 +51,25 @@ CmdPurge::CmdPurge () _category = Command::Category::operation; } +//////////////////////////////////////////////////////////////////////////////// +// Makes sure that any task having the dependency on the task being purged +// has that dependency removed, to preserve referential integrity. +void CmdPurge::handleDeps (Task& task) +{ + std::string uuid = task.get ("uuid"); + + for (auto& blockedConst: context.tdb2.all_tasks ()) + { + Task& blocked = const_cast(blockedConst); + if (blocked.has ("depends") && + blocked.get ("depends").find (uuid) != std::string::npos) + { + blocked.removeDependency (uuid); + context.tdb2.modify (blocked); + } + } +} + //////////////////////////////////////////////////////////////////////////////// int CmdPurge::execute (std::string&) { @@ -82,19 +101,8 @@ int CmdPurge::execute (std::string&) if (permission (question, filtered.size ())) { context.tdb2.purge (task); + handleDeps(task); count++; - - // Remove dependencies on the task being purged - for (auto& blockedConst: context.tdb2.all_tasks ()) - { - Task& blocked = const_cast(blockedConst); - if (blocked.has ("depends") && - blocked.get ("depends").find (uuid) != std::string::npos) - { - blocked.removeDependency (uuid); - context.tdb2.modify (blocked); - } - } } } } diff --git a/src/commands/CmdPurge.h b/src/commands/CmdPurge.h index 8e9c76446..76644f3b3 100644 --- a/src/commands/CmdPurge.h +++ b/src/commands/CmdPurge.h @@ -32,6 +32,8 @@ class CmdPurge : public Command { +private: + void handleDeps (Task& task); public: CmdPurge (); int execute (std::string&);