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