Perform vacuum if migration is performed

This commit is contained in:
emeric
2024-04-19 12:01:23 +02:00
parent 4432def11f
commit d6b86a9360
5 changed files with 33 additions and 17 deletions
+5 -1
View File
@@ -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;
}
}
+1 -1
View File
@@ -52,6 +52,6 @@ namespace lms::db
namespace Migration
{
void doDbMigration(Session& session);
bool doDbMigration(Session& session); // return true if migration was performed
}
}
+11 -3
View File
@@ -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,6 +247,10 @@ 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...");
@@ -254,7 +263,6 @@ namespace lms::db
LMS_LOG(DB, INFO, "Vacuum complete!");
}
}
void Session::refreshTracingLoggerStats()
{
@@ -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->
+4 -1
View File
@@ -293,10 +293,13 @@ 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
if (migrationPerformed)
session.vacuum();
else
session.vacuumIfNeeded();
// force optimize in case scanner aborted during a large import: