Skip to content

Commit 5971b2f

Browse files
committed
Run a vacuum on the database after cert hashes migration
1 parent 13e67be commit 5971b2f

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

mithril-aggregator/src/commands/tools_command.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clap::{Parser, Subcommand};
22
use config::{builder::DefaultState, ConfigBuilder};
3+
use mithril_common::sqlite::vacuum_database;
34
use slog_scope::debug;
45
use std::{error::Error, sync::Arc};
56

@@ -69,13 +70,18 @@ impl RecomputeCertificatesHashCommand {
6970
let connection = dependencies_builder.get_sqlite_connection().await?;
7071
let migrator = CertificatesHashMigrator::new(
7172
CertificateRepository::new(connection.clone()),
72-
Arc::new(SignedEntityStoreAdapter::new(connection)),
73+
Arc::new(SignedEntityStoreAdapter::new(connection.clone())),
7374
);
7475

7576
migrator
7677
.migrate()
7778
.await
78-
.map_err(|err| format!("Certificate hash migrator error: {err}"))?;
79+
.map_err(|err| format!("recompute-certificates-hash: migration error: {err}"))?;
80+
81+
vacuum_database(connection)
82+
.await
83+
.map_err(|err| format!("recompute-certificates-hash: database vacuum error: {err}"))?;
84+
7985
Ok(())
8086
}
8187
}

mithril-aggregator/src/tools/certificates_hash_migrator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ impl CertificatesHashMigrator {
161161
self.certificate_repository
162162
.delete_certificates(&old_certificates.iter().collect::<Vec<_>>())
163163
.await?;
164+
164165
Ok(())
165166
}
166167
}

mithril-common/src/sqlite/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,34 @@ pub use entity::{HydrationError, SqLiteEntity};
1515
pub use projection::{Projection, ProjectionField};
1616
pub use provider::Provider;
1717
pub use source_alias::SourceAlias;
18+
19+
use sqlite::Connection;
20+
use std::sync::Arc;
21+
use tokio::sync::Mutex;
22+
23+
use crate::StdResult;
24+
25+
/// Do a [vacuum](https://www.sqlite.org/lang_vacuum.html) on the given connection, this will
26+
/// reconstruct the database file, repacking it into a minimal amount of disk space.
27+
pub async fn vacuum_database(connection: Arc<Mutex<Connection>>) -> StdResult<()> {
28+
let connection = &connection.lock().await;
29+
connection.execute("vacuum")?;
30+
Ok(())
31+
}
32+
33+
#[cfg(test)]
34+
mod test {
35+
use crate::sqlite::vacuum_database;
36+
use sqlite::Connection;
37+
use std::sync::Arc;
38+
use tokio::sync::Mutex;
39+
40+
#[tokio::test]
41+
async fn calling_vacuum_on_an_empty_in_memory_db_should_not_fail() {
42+
let connection = Arc::new(Mutex::new(Connection::open(":memory:").unwrap()));
43+
44+
vacuum_database(connection)
45+
.await
46+
.expect("Vacuum should not fail");
47+
}
48+
}

0 commit comments

Comments
 (0)