diff --git a/CHANGELOG.md b/CHANGELOG.md index 801043bcc6..c24ad07e6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Currently playing music track and artist now shows in the debug menu. - Added a setting to influence the gap between music track plays. - Added a Craft All button. +- Server: Vacuum database on startup ### Changed - Use fluent for translations diff --git a/server/src/lib.rs b/server/src/lib.rs index 8af72f1c0e..bcc50f13e0 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -230,6 +230,10 @@ impl Server { debug!("Running DB migrations..."); persistence::run_migrations(&database_settings); + // Vacuum database + debug!("Vacuuming database..."); + persistence::vacuum_database(&database_settings); + let database_settings = Arc::new(RwLock::new(database_settings)); let registry = Arc::new(Registry::new()); diff --git a/server/src/persistence/mod.rs b/server/src/persistence/mod.rs index 1c0c9e4773..c84f5e1675 100644 --- a/server/src/persistence/mod.rs +++ b/server/src/persistence/mod.rs @@ -172,6 +172,18 @@ pub fn run_migrations(settings: &DatabaseSettings) { info!("Applied {} database migrations", applied_migrations); } +/// Runs after the migrations. In some cases, it can reclaim a significant +/// amount of space (reported 30%) +pub fn vacuum_database(settings: &DatabaseSettings) { + let conn = establish_connection(settings, ConnectionMode::ReadWrite); + + // The params type is phony; it's required, but not meaningful. + conn.execute::<&[u32]>("VACUUM main", &[]) + .expect("Database vacuuming failed, server startup aborted"); + + info!("Database vacuumed"); +} + // These callbacks use info logging because they are never enabled by default, // only when explicitly turned on via CLI arguments or interactive CLI commands. // Setting them to anything other than info would remove the ability to get SQL