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