Implement integrity check

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/5/head
trivernis 3 years ago
parent 272b0bf7dc
commit 37322b13a7
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -38,8 +38,11 @@ pub enum RepoError {
#[error("failed to decode data {0}")] #[error("failed to decode data {0}")]
Decode(#[from] data_encoding::DecodeError), Decode(#[from] data_encoding::DecodeError),
#[error("Failed to read repo.toml configuration file {0}")] #[error("failed to read repo.toml configuration file {0}")]
Config(#[from] config::ConfigError), Config(#[from] config::ConfigError),
#[error("the database file is corrupted {0}")]
Corrupted(String),
} }
#[derive(Error, Debug)] #[derive(Error, Debug)]

@ -9,6 +9,7 @@ use sea_orm::ActiveValue::Set;
use sea_orm::ConnectionTrait; use sea_orm::ConnectionTrait;
impl JobDao { impl JobDao {
#[tracing::instrument(level = "debug", skip(self))]
pub async fn migrate_content_descriptors(&self) -> RepoResult<()> { pub async fn migrate_content_descriptors(&self) -> RepoResult<()> {
let cds: Vec<content_descriptor::Model> = let cds: Vec<content_descriptor::Model> =
content_descriptor::Entity::find().all(&self.ctx.db).await?; content_descriptor::Entity::find().all(&self.ctx.db).await?;

@ -1,4 +1,5 @@
pub mod migrate_content_descriptors; pub mod migrate_content_descriptors;
pub mod sqlite_operations;
use crate::dao::{DaoContext, DaoProvider}; use crate::dao::{DaoContext, DaoProvider};

@ -0,0 +1,44 @@
use crate::dao::job::JobDao;
use mediarepo_core::error::RepoError::Corrupted;
use mediarepo_core::error::RepoResult;
use sea_orm::DatabaseBackend::Sqlite;
use sea_orm::{ConnectionTrait, FromQueryResult, Statement};
#[derive(Debug, FromQueryResult)]
struct IntegrityCheckResult {
integrity_check: String,
}
impl JobDao {
#[tracing::instrument(level = "debug", skip(self))]
pub async fn check_integrity(&self) -> RepoResult<()> {
let check_result: Option<IntegrityCheckResult> = IntegrityCheckResult::find_by_statement(
Statement::from_string(Sqlite, String::from("PRAGMA integrity_check;")),
)
.one(&self.ctx.db)
.await?;
tracing::debug!("check result = {:?}", check_result);
check_result
.ok_or_else(|| Corrupted(String::from("no check result")))
.and_then(map_check_result)
}
#[tracing::instrument(level = "debug", skip(self))]
pub async fn vacuum(&self) -> RepoResult<()> {
self.ctx
.db
.execute(Statement::from_string(Sqlite, String::from("VACUUM;")))
.await?;
Ok(())
}
}
fn map_check_result(result: IntegrityCheckResult) -> RepoResult<()> {
if result.integrity_check == "ok" {
Ok(())
} else {
Err(Corrupted(result.integrity_check))
}
}

@ -30,7 +30,7 @@ impl JobsNamespace {
match run_request.job_type { match run_request.job_type {
JobType::MigrateContentDescriptors => job_dao.migrate_content_descriptors().await?, JobType::MigrateContentDescriptors => job_dao.migrate_content_descriptors().await?,
JobType::CalculateSizes => calculate_all_sizes(ctx).await?, JobType::CalculateSizes => calculate_all_sizes(ctx).await?,
JobType::CheckIntegrity => {} JobType::CheckIntegrity => job_dao.check_integrity().await?,
} }
ctx.emit_to(Self::name(), "run_job", ()).await?; ctx.emit_to(Self::name(), "run_job", ()).await?;

Loading…
Cancel
Save