1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::cli::parser::Commands;
use crate::config::Config;
use crate::db::migrate::run_pending_migrations;
use crate::db::pool::DbPool;
use crate::db::stats;
use crate::errors::AppResult;
use crate::ui::messages::{error, info, success};
pub fn handle(cmd: &Commands, cfg: &Config) -> AppResult<()> {
if let Commands::Db {
migrate,
check,
vacuum,
info: show_info,
} = cmd
{
// Unica istanza condivisa
let mut pool: Option<DbPool> = None;
// Helper per ottenere il DbPool
fn get_pool<'a>(pool: &'a mut Option<DbPool>, db_path: &str) -> AppResult<&'a mut DbPool> {
if pool.is_none() {
*pool = Some(DbPool::new(db_path)?);
}
Ok(pool.as_mut().unwrap())
}
// ------------------------------------------------------------
// 1) MIGRATION
// ------------------------------------------------------------
if *migrate {
let pool = get_pool(&mut pool, &cfg.database)?;
info("Running database migrations…");
run_pending_migrations(&pool.conn)?;
success("Database migration completed successfully.\n");
}
// ------------------------------------------------------------
// 2) SHOW INFO
// ------------------------------------------------------------
if *show_info {
let pool = get_pool(&mut pool, &cfg.database)?;
info("Database information:");
stats::print_db_info(pool, &cfg.database)?;
}
// ------------------------------------------------------------
// 3) INTEGRITY CHECK
// ------------------------------------------------------------
if *check {
let pool = get_pool(&mut pool, &cfg.database)?;
info("Running database integrity check…");
let integrity: String = pool
.conn
.query_row("PRAGMA integrity_check;", [], |row| row.get(0))?;
if integrity == "ok" {
success("Integrity check passed.\n");
} else {
error(format!("Integrity check failed:\n{}", integrity));
}
}
// ------------------------------------------------------------
// 4) VACUUM
// ------------------------------------------------------------
if *vacuum {
let pool = get_pool(&mut pool, &cfg.database)?;
info("Running VACUUM…");
pool.conn.execute_batch("VACUUM;")?;
success("VACUUM completed successfully.\n");
}
}
Ok(())
}