OXYGENHUB
# 18e381ea88c2 oxygenhub / crates / hub / src / db / mod.rs
1.2 KiB · Rust · last changed Milestone 0.4: issues and notifications 1 month ago source blame history raw
1
//! SQLite through `sqlx` (hub spec §3.1, §17.2): WAL, foreign keys on,
2
//! migrations embedded and applied at start. One module per table group.
3
4
pub mod accounts;
5
pub mod issues;
6
pub mod notify;
7
pub mod proposals;
8
pub mod repos;
9
10
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous};
11
use sqlx::SqlitePool;
12
use std::path::Path;
13
14
pub type Pool = SqlitePool;
15
16
/// Open (creating if needed) and migrate the database at `path`.
17
pub async fn open(path: &Path) -> anyhow::Result<Pool> {
18
    let options = SqliteConnectOptions::new()
19
        .filename(path)
20
        .create_if_missing(true)
21
        .journal_mode(SqliteJournalMode::Wal)
22
        .synchronous(SqliteSynchronous::Normal)
23
        .foreign_keys(true)
24
        .busy_timeout(std::time::Duration::from_secs(5));
25
    let pool = SqlitePoolOptions::new().max_connections(8).connect_with(options).await?;
26
    sqlx::migrate!("./migrations").run(&pool).await?;
27
    Ok(pool)
28
}
29
30
/// Milliseconds since the epoch, the hub's timestamp unit (as `oxy`'s).
31
pub fn now_ms() -> i64 {
32
    std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as i64).unwrap_or(0)
33
}