From bcd70488ae96ad7dc79d5f9fb20caa1659e6cb3b Mon Sep 17 00:00:00 2001 From: pedrocarlo Date: Fri, 29 Aug 2025 12:22:50 -0300 Subject: [PATCH] add sqlite integrity check back --- simulator/main.rs | 33 ++++++++++++++++++++++++++++++++- simulator/runner/watch.rs | 13 ++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/simulator/main.rs b/simulator/main.rs index 0aaaf8be7..ccf8977ae 100644 --- a/simulator/main.rs +++ b/simulator/main.rs @@ -15,6 +15,7 @@ use std::any::Any; use std::backtrace::Backtrace; use std::fs::OpenOptions; use std::io::{IsTerminal, Write}; +use std::path::Path; use std::sync::{mpsc, Arc, Mutex}; use tracing_subscriber::field::MakeExt; use tracing_subscriber::fmt::format; @@ -598,13 +599,23 @@ fn run_simulation_default( }) .collect::>(); - let result = execute_plans(env.clone(), plans, &mut states, last_execution); + let mut result = execute_plans(env.clone(), plans, &mut states, last_execution); let env = env.lock().unwrap(); env.io.print_stats(); tracing::info!("Simulation completed"); + if result.error.is_none() { + let ic = integrity_check(&env.get_db_path()); + if let Err(err) = ic { + tracing::error!("integrity check failed: {}", err); + result.error = Some(turso_core::LimboError::InternalError(err.to_string())); + } else { + tracing::info!("integrity check passed"); + } + } + result } @@ -667,3 +678,23 @@ const BANNER: &str = r#" \____________________________/ "#; + +fn integrity_check(db_path: &Path) -> anyhow::Result<()> { + let conn = rusqlite::Connection::open(db_path)?; + let mut stmt = conn.prepare("SELECT * FROM pragma_integrity_check;")?; + let mut rows = stmt.query(())?; + let mut result: Vec = Vec::new(); + + while let Some(row) = rows.next()? { + result.push(row.get(0)?); + } + if result.is_empty() { + anyhow::bail!("simulation failed: integrity_check should return `ok` or a list of problems") + } + if !result[0].eq_ignore_ascii_case("ok") { + // Build a list of problems + result.iter_mut().for_each(|row| *row = format!("- {row}")); + anyhow::bail!("simulation failed: {}", result.join("\n")) + } + Ok(()) +} diff --git a/simulator/runner/watch.rs b/simulator/runner/watch.rs index feab80af1..95d65ad64 100644 --- a/simulator/runner/watch.rs +++ b/simulator/runner/watch.rs @@ -4,6 +4,7 @@ use sql_generation::generation::pick_index; use crate::{ generation::plan::{Interaction, InteractionPlanState}, + integrity_check, runner::execution::ExecutionContinuation, }; @@ -25,13 +26,23 @@ pub(crate) fn run_simulation( secondary_pointer: 0, }) .collect::>(); - let result = execute_plans(env.clone(), plans, &mut states, last_execution); + let mut result = execute_plans(env.clone(), plans, &mut states, last_execution); let env = env.lock().unwrap(); env.io.print_stats(); tracing::info!("Simulation completed"); + if result.error.is_none() { + let ic = integrity_check(&env.get_db_path()); + if let Err(err) = ic { + tracing::error!("integrity check failed: {}", err); + result.error = Some(turso_core::LimboError::InternalError(err.to_string())); + } else { + tracing::info!("integrity check passed"); + } + } + result }