pipedash_core/infrastructure/database/
mod.rs1use std::path::Path;
2
3macro_rules! on_pool {
4 ($pool:expr, $p:ident => $body:expr) => {
5 match $pool {
6 DatabasePool::Sqlite($p) => $body,
7 DatabasePool::Postgres($p) => $body,
8 }
9 };
10}
11
12mod device_alert_repository;
13mod device_push_repository;
14mod device_repository;
15mod metrics_repository;
16mod pipeline_view_repository;
17mod push_repository;
18mod repository;
19mod schema;
20mod sqlite_backend;
21
22#[cfg(feature = "postgres")]
23mod postgres_backend;
24
25pub use device_alert_repository::DeviceAlertRepository;
26pub use device_push_repository::DevicePushRepository;
27pub use device_repository::DeviceRepository;
28pub use metrics_repository::{
29 MetricsRepository,
30 RecordMetrics,
31};
32pub use pipeline_view_repository::{
33 PipelineViewRepository,
34 StoredMember,
35 StoredPin,
36 StoredView,
37 ViewExport,
38};
39#[cfg(feature = "postgres")]
40pub use postgres_backend::PostgresConfigBackend;
41pub use push_repository::PushRepository;
42pub use repository::{
43 DatabasePool,
44 ProviderFetchStatus,
45 Repository,
46};
47pub(crate) use repository::{
48 FETCH_STATUS_ERROR,
49 FETCH_STATUS_NEVER,
50 FETCH_STATUS_SUCCESS,
51};
52pub use schema::init_database;
53#[cfg(feature = "postgres")]
54pub use schema::init_postgres_database;
55pub use sqlite_backend::SqliteConfigBackend;
56
57pub async fn has_encrypted_tokens(db_path: &Path) -> bool {
58 if !db_path.exists() {
59 return false;
60 }
61
62 let db_url = format!("sqlite:{}?mode=ro", db_path.display());
63
64 let pool = match sqlx::SqlitePool::connect(&db_url).await {
65 Ok(pool) => pool,
66 Err(e) => {
67 tracing::debug!(
68 "Could not connect to database to check encrypted tokens: {}",
69 e
70 );
71 return false;
72 }
73 };
74
75 let result: Result<i64, _> = sqlx::query_scalar(
76 "SELECT COUNT(*) FROM providers WHERE encrypted_token IS NOT NULL AND token_nonce IS NOT NULL"
77 )
78 .fetch_one(&pool)
79 .await;
80
81 let has_tokens = result.map(|count| count > 0).unwrap_or(false);
82
83 tracing::debug!(
84 "Checked for encrypted tokens in {}: {}",
85 db_path.display(),
86 if has_tokens { "found" } else { "none" }
87 );
88
89 has_tokens
90}