From b0336dfd505c7dde1f95b894b58083950a0940e0 Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Sun, 20 Mar 2016 15:16:05 +0100 Subject: [PATCH] CmdPurge: Move dependency handling into separate method --- src/commands/CmdPurge.cpp | 32 ++++++++++++++++++++------------ src/commands/CmdPurge.h | 2 ++ 2 files changed, 22 insertions(+), 12 deletions(-) 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&);