948 B
raw
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous};
use sqlx::SqlitePool;
use std::path::Path;
use std::str::FromStr;
use std::time::Duration;
pub async fn init(data_dir: &Path) -> anyhow::Result<SqlitePool> {
let db_path = data_dir.join("db.sqlite3");
let url = format!("sqlite://{}", db_path.display());
if !db_path.exists() {
std::fs::File::create(&db_path)?;
}
let opts = SqliteConnectOptions::from_str(&url)?
.create_if_missing(true)
.journal_mode(SqliteJournalMode::Wal)
.synchronous(SqliteSynchronous::Normal)
.busy_timeout(Duration::from_secs(5))
.foreign_keys(true);
let pool = SqlitePoolOptions::new()
.max_connections(8)
.connect_with(opts)
.await?;
sqlx::migrate!("./migrations").run(&pool).await?;
Ok(pool)
}
pub fn now_ms() -> i64 {
chrono::Utc::now().timestamp_millis()
}