mascarpone/src/db.rs
Robert Perce 69e23fd9bb
Some checks failed
/ integration-test--firefox (push) Failing after 3m5s
fix: broken contact query
2026-01-26 17:56:00 -06:00

37 lines
990 B
Rust

use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions};
use std::str::FromStr;
use crate::models::user::User;
pub struct Database {
pub pool: SqlitePool,
}
pub type DbId = i64;
impl Database {
pub async fn for_user(user: &User) -> Result<Self, anyhow::Error> {
let file = if user.ephemeral {
":memory:".to_string()
} else {
format!("./dbs/{}.db", user.username)
};
let db_options = SqliteConnectOptions::from_str(&file)?
.create_if_missing(true)
.to_owned();
let pool = SqlitePoolOptions::new().connect_with(db_options).await?;
tracing::debug!("migrating...");
sqlx::migrate!("./migrations/each_user/").run(&pool).await?;
if user.username == "demo" {
sqlx::query_file!("./migrations/demo.sql")
.execute(&pool)
.await?;
};
tracing::debug!("...done.");
Ok(Self { pool })
}
}