Skip to content

Commit

Permalink
Merge pull request #2321 from uklotzde/2.2_libraryscanner_db_cleanup
Browse files Browse the repository at this point in the history
lp1846971: Fix database inconsistencies caused by LibraryScanner
  • Loading branch information
daschuer authored Oct 22, 2019
2 parents 9e6f1c0 + 4690111 commit 8903745
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/library/scanner/libraryscanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
#include "util/trace.h"
#include "util/file.h"
#include "util/timer.h"
#include "util/performancetimer.h"
#include "library/scanner/scannerutil.h"
#include "util/db/dbconnectionpooler.h"
#include "util/db/dbconnectionpooled.h"
#include "util/db/fwdsqlquery.h"

namespace {

Expand All @@ -24,6 +26,21 @@ mixxx::Logger kLogger("LibraryScanner");

QAtomicInt s_instanceCounter(0);

const QString kDeleteOrphanedLibraryScannerDirectories =
"delete from LibraryHashes where hash <> 0 and directory_path not in (select directory from track_locations)";

// Returns the number of affected rows or -1 on error
int execCleanupQuery(QSqlDatabase database, const QString& statement) {
FwdSqlQuery query(database, statement);
VERIFY_OR_DEBUG_ASSERT(query.isPrepared()) {
return -1;
}
if (!query.execPrepared()) {
return -1;
}
return query.numRowsAffected();
}

} // anonymous namespace

LibraryScanner::LibraryScanner(
Expand Down Expand Up @@ -103,6 +120,26 @@ void LibraryScanner::run() {
return;
}

// Clean up the database and fix inconsistencies from previous runs.
// See also: https://bugs.launchpad.net/mixxx/+bug/1846945
{
kLogger.info() << "Cleaning up database...";
PerformanceTimer timer;
timer.start();
auto numRows = execCleanupQuery(dbConnection,
kDeleteOrphanedLibraryScannerDirectories);
if (numRows < 0) {
kLogger.warning()
<< "Failed to delete orphaned directory hashes";
} else if (numRows > 0) {
kLogger.info()
<< "Deleted" << numRows << "orphaned directory hashes)";
}
kLogger.info()
<< "Finished database cleanup:"
<< timer.elapsed().debugMillisWithUnit();
}

m_libraryHashDao.initialize(dbConnection);
m_cueDao.initialize(dbConnection);
m_trackDao.initialize(dbConnection);
Expand Down

0 comments on commit 8903745

Please sign in to comment.