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