OXYGENHUB
# 1e23d0105921 oxygenhub / crates / hub / src / db / mod.rs
1.1 KiB · Rust · last changed Milestone 0.3: proposals and review 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 proposals;
6
pub mod repos;
7
8
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous};
9
use sqlx::SqlitePool;
10
use std::path::Path;
11
12
pub type Pool = SqlitePool;
13
14
/// Open (creating if needed) and migrate the database at `path`.
15
pub async fn open(path: &Path) -> anyhow::Result<Pool> {
16
    let options = SqliteConnectOptions::new()
17
        .filename(path)
18
        .create_if_missing(true)
19
        .journal_mode(SqliteJournalMode::Wal)
20
        .synchronous(SqliteSynchronous::Normal)
21
        .foreign_keys(true)
22
        .busy_timeout(std::time::Duration::from_secs(5));
23
    let pool = SqlitePoolOptions::new().max_connections(8).connect_with(options).await?;
24
    sqlx::migrate!("./migrations").run(&pool).await?;
25
    Ok(pool)
26
}
27
28
/// Milliseconds since the epoch, the hub's timestamp unit (as `oxy`'s).
29
pub fn now_ms() -> i64 {
30
    std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as i64).unwrap_or(0)
31
}