diff --git a/src/database/Track.cpp b/src/database/Track.cpp index d66327f0..a222e77a 100644 --- a/src/database/Track.cpp +++ b/src/database/Track.cpp @@ -271,6 +271,15 @@ ClusterType::ClusterType(std::string name) { } +std::vector +ClusterType::getAllOrphans(Wt::Dbo::Session& session) +{ + Wt::Dbo::collection res = session.query>("select c_t from cluster_type c_t LEFT OUTER JOIN cluster c ON c_t.id = c.cluster_type_id WHERE c.id IS NULL"); + + return std::vector(res.begin(), res.end()); +} + + ClusterType::pointer ClusterType::getByName(Wt::Dbo::Session& session, std::string name) { diff --git a/src/database/Track.hpp b/src/database/Track.hpp index 878ef796..3bec8ee9 100644 --- a/src/database/Track.hpp +++ b/src/database/Track.hpp @@ -96,6 +96,7 @@ class ClusterType : public Wt::Dbo::Dbo ClusterType() {} ClusterType(std::string name); + static std::vector getAllOrphans(Wt::Dbo::Session& session); static pointer getByName(Wt::Dbo::Session& session, std::string name); static std::vector getAll(Wt::Dbo::Session& session); diff --git a/src/scanner/MediaScanner.cpp b/src/scanner/MediaScanner.cpp index 2e56944f..e4b12f43 100644 --- a/src/scanner/MediaScanner.cpp +++ b/src/scanner/MediaScanner.cpp @@ -725,6 +725,7 @@ MediaScanner::checkAudioFiles( Stats& stats ) { Wt::Dbo::Transaction transaction(_db.getSession()); + // TODO better query for this // Now process orphan Cluster (no track) auto clusters = Cluster::getAll(_db.getSession()); for (auto cluster : clusters) @@ -735,6 +736,14 @@ MediaScanner::checkAudioFiles( Stats& stats ) cluster.remove(); } } + + // Now process orphan cluster types + auto clusterTypes = ClusterType::getAllOrphans(_db.getSession()); + for (auto clusterType : clusterTypes) + { + LMS_LOG(DBUPDATER, DEBUG) << "Removing orphan cluster type '" << clusterType->getName() << "'"; + clusterType.remove(); + } } LMS_LOG(DBUPDATER, DEBUG) << "Checking artists...";