diff --git a/src/libs/database/impl/Migration.cpp b/src/libs/database/impl/Migration.cpp index 6d53b076..426de394 100644 --- a/src/libs/database/impl/Migration.cpp +++ b/src/libs/database/impl/Migration.cpp @@ -460,7 +460,7 @@ SELECT session.getDboSession()->execute("DROP INDEX " + indexName); } - void doDbMigration(Session& session) + bool doDbMigration(Session& session) { static const std::string outdatedMsg{ "Outdated database, please rebuild it (delete the .db file and restart)" }; @@ -496,6 +496,7 @@ SELECT {56, migrateFromV56}, }; + bool migrationPerformed{}; { LMS_SCOPED_TRACE_OVERVIEW("Database", "Migration"); auto transaction{ session.createWriteTransaction() }; @@ -530,7 +531,10 @@ SELECT VersionInfo::get(session).modify()->setVersion(++version); LMS_LOG(DB, INFO, "Migration complete to version " << version); + migrationPerformed = true; } } + + return migrationPerformed; } } diff --git a/src/libs/database/impl/Migration.hpp b/src/libs/database/impl/Migration.hpp index 5263e9e7..ac528c8f 100644 --- a/src/libs/database/impl/Migration.hpp +++ b/src/libs/database/impl/Migration.hpp @@ -52,6 +52,6 @@ namespace lms::db namespace Migration { - void doDbMigration(Session& session); + bool doDbMigration(Session& session); // return true if migration was performed } } diff --git a/src/libs/database/impl/Session.cpp b/src/libs/database/impl/Session.cpp index ba0e366d..342d8cb4 100644 --- a/src/libs/database/impl/Session.cpp +++ b/src/libs/database/impl/Session.cpp @@ -143,20 +143,23 @@ namespace lms::db } } - void Session::migrateIfNeeded() + bool Session::migrateSchemaIfNeeded() { - Migration::doDbMigration(*this); + const bool migrationPerformed{ Migration::doDbMigration(*this) }; // TODO: move this elsewhere { auto uniqueTransaction{ createWriteTransaction() }; ScanSettings::init(*this); } + + return migrationPerformed; } void Session::createIndexesIfNeeded() { LMS_SCOPED_TRACE_OVERVIEW("Database", "IndexCreation"); + LMS_LOG(DB, INFO, "Creating indexes... This may take a while..."); auto transaction{ createWriteTransaction() }; _session.execute("CREATE INDEX IF NOT EXISTS artist_id_idx ON artist(id)"); @@ -227,6 +230,8 @@ namespace lms::db _session.execute("CREATE INDEX IF NOT EXISTS starred_track_user_backend_idx ON starred_track(user_id,backend)"); _session.execute("CREATE INDEX IF NOT EXISTS starred_track_track_user_backend_idx ON starred_track(track_id,user_id,backend)"); + + LMS_LOG(DB, INFO, "Indexes created!"); } void Session::vacuumIfNeeded() @@ -242,18 +247,21 @@ namespace lms::db LMS_LOG(DB, INFO, "page stats: page_count = " << pageCount << ", freelist_count = " << freeListCount); if (freeListCount >= (pageCount / 10)) + vacuum(); + } + + void Session::vacuum() + { + LMS_SCOPED_TRACE_OVERVIEW("Database", "Vacuum"); + LMS_LOG(DB, INFO, "Performing vacuum... This may take a while..."); + + // We manually take a lock here since vacuum cannot be inside a transaction { - LMS_SCOPED_TRACE_OVERVIEW("Database", "Vacuum"); - LMS_LOG(DB, INFO, "Performing vacuum... This may take a while..."); - - // We manually take a lock here since vacuum cannot be inside a transaction - { - std::unique_lock lock{ _db.getMutex() }; - _db.executeSql("VACUUM"); - } - - LMS_LOG(DB, INFO, "Vacuum complete!"); + std::unique_lock lock{ _db.getMutex() }; + _db.executeSql("VACUUM"); } + + LMS_LOG(DB, INFO, "Vacuum complete!"); } void Session::refreshTracingLoggerStats() diff --git a/src/libs/database/include/database/Session.hpp b/src/libs/database/include/database/Session.hpp index 2c8352af..7983e5d5 100644 --- a/src/libs/database/include/database/Session.hpp +++ b/src/libs/database/include/database/Session.hpp @@ -92,9 +92,10 @@ namespace lms::db void analyzeEntry(const std::string& entry); void prepareTablesIfNeeded(); // need to run only once at startup - void migrateIfNeeded(); + bool migrateSchemaIfNeeded(); // returns true if migration was performed void createIndexesIfNeeded(); void vacuumIfNeeded(); + void vacuum(); void refreshTracingLoggerStats(); // returning a ptr here to ease further wrapping using operator-> diff --git a/src/lms/main.cpp b/src/lms/main.cpp index 6fbc7727..605114aa 100644 --- a/src/lms/main.cpp +++ b/src/lms/main.cpp @@ -293,11 +293,14 @@ namespace lms { db::Session session{ database }; session.prepareTablesIfNeeded(); - session.migrateIfNeeded(); + bool migrationPerformed{ session.migrateSchemaIfNeeded() }; session.createIndexesIfNeeded(); // As this may be quite long, we only do it during startup - session.vacuumIfNeeded(); + if (migrationPerformed) + session.vacuum(); + else + session.vacuumIfNeeded(); // force optimize in case scanner aborted during a large import: // queries may be too slow to even be able to relaunch a scan using the web interface