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