use chrono::{DateTime, Utc};
use foundation_db::core::storage_provider::AsyncQueryStore;
use crate::core::error::{AppError, AppResult};
pub mod ciphers;
pub mod devices;
pub mod events;
pub mod folders;
pub mod orgs;
pub mod sends;
pub mod two_factor;
pub mod users;
pub const SCHEMA: &str = include_str!("../../../migrations/0001_initial.sql");
pub async fn apply_schema(db: &dyn AsyncQueryStore) -> AppResult<()> {
let sql: String = SCHEMA
.lines()
.map(|line| match line.find("--") {
Some(idx) => &line[..idx],
None => line,
})
.collect::<Vec<_>>()
.join("\n");
for statement in sql.split(';') {
let statement = statement.trim();
if statement.is_empty() {
continue;
}
db.execute_async(statement, &[]).await.map_err(store_err)?;
}
Ok(())
}
pub(crate) fn store_err(e: foundation_db::StorageError) -> AppError {
AppError::Internal(e.to_string())
}
pub(crate) fn parse_ts(s: &str) -> Result<DateTime<Utc>, AppError> {
DateTime::parse_from_rfc3339(s)
.map(|d| d.with_timezone(&Utc))
.map_err(|e| AppError::Internal(format!("invalid timestamp {s:?}: {e}")))
}