1pub mod connection;
2pub mod migrations;
3pub mod queries;
4pub mod advanced_queries;
5pub mod pool;
6
7use anyhow::Result;
8use std::path::Path;
9use std::sync::OnceLock;
10
11pub use connection::Database;
12pub use pool::{DatabasePool, PoolConfig, PooledConnectionGuard};
13
14static DB_POOL: OnceLock<DatabasePool> = OnceLock::new();
16
17pub fn initialize_database(db_path: &Path) -> Result<Database> {
18 println!("Initializing database at: {:?}", db_path);
19 let db = Database::new(db_path)?;
20 println!("Database connection created");
21
22 println!("Running migrations...");
24 migrations::run_migrations(&db.connection)?;
25 println!("Migrations completed");
26
27 Ok(db)
28}
29
30pub fn get_database_path() -> Result<std::path::PathBuf> {
31 let data_dir = dirs::data_dir()
32 .or_else(|| dirs::home_dir())
33 .ok_or_else(|| anyhow::anyhow!("Could not determine data directory"))?;
34
35 let tempo_dir = data_dir.join(".tempo");
36 std::fs::create_dir_all(&tempo_dir)?;
37
38 Ok(tempo_dir.join("data.db"))
39}
40
41pub fn initialize_pool() -> Result<()> {
43 let db_path = get_database_path()?;
44 let pool = DatabasePool::new_with_defaults(db_path)?;
45
46 DB_POOL.set(pool)
47 .map_err(|_| anyhow::anyhow!("Database pool already initialized"))?;
48
49 Ok(())
50}
51
52pub fn initialize_pool_with_config(config: PoolConfig) -> Result<()> {
54 let db_path = get_database_path()?;
55 let pool = DatabasePool::new(db_path, config)?;
56
57 DB_POOL.set(pool)
58 .map_err(|_| anyhow::anyhow!("Database pool already initialized"))?;
59
60 Ok(())
61}
62
63pub async fn get_connection() -> Result<PooledConnectionGuard> {
65 let pool = DB_POOL.get()
66 .ok_or_else(|| anyhow::anyhow!("Database pool not initialized. Call initialize_pool() first."))?;
67
68 pool.get_connection().await
69}
70
71pub fn get_pool_stats() -> Result<pool::PoolStats> {
73 let pool = DB_POOL.get()
74 .ok_or_else(|| anyhow::anyhow!("Database pool not initialized"))?;
75
76 pool.stats()
77}
78
79pub fn close_pool() -> Result<()> {
81 if let Some(pool) = DB_POOL.get() {
82 pool.close()?;
83 }
84 Ok(())
85}