pipedash_core/infrastructure/database/
mod.rs1use std::path::Path;
2
3mod metrics_repository;
4mod repository;
5mod schema;
6mod sqlite_backend;
7
8#[cfg(feature = "postgres")]
9mod postgres_backend;
10
11pub use metrics_repository::MetricsRepository;
12#[cfg(feature = "postgres")]
13pub use postgres_backend::PostgresConfigBackend;
14pub use repository::{
15 DatabasePool,
16 Repository,
17};
18pub use schema::init_database;
19#[cfg(feature = "postgres")]
20pub use schema::init_postgres_database;
21pub use sqlite_backend::SqliteConfigBackend;
22
23pub async fn has_encrypted_tokens(db_path: &Path) -> bool {
24 if !db_path.exists() {
25 return false;
26 }
27
28 let db_url = format!("sqlite:{}?mode=ro", db_path.display());
29
30 let pool = match sqlx::SqlitePool::connect(&db_url).await {
31 Ok(pool) => pool,
32 Err(e) => {
33 tracing::debug!(
34 "Could not connect to database to check encrypted tokens: {}",
35 e
36 );
37 return false;
38 }
39 };
40
41 let result: Result<i64, _> = sqlx::query_scalar(
42 "SELECT COUNT(*) FROM providers WHERE encrypted_token IS NOT NULL AND token_nonce IS NOT NULL"
43 )
44 .fetch_one(&pool)
45 .await;
46
47 let has_tokens = result.map(|count| count > 0).unwrap_or(false);
48
49 tracing::debug!(
50 "Checked for encrypted tokens in {}: {}",
51 db_path.display(),
52 if has_tokens { "found" } else { "none" }
53 );
54
55 has_tokens
56}