mascarpone/src/db.rs

36 lines
912 B
Rust
Raw Normal View History

2025-11-27 13:45:21 -06:00
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?;
2026-01-23 21:20:27 -06:00
sqlx::migrate!("./migrations/each_user/").run(&pool).await?;
2026-01-19 21:38:23 -06:00
if user.username == "demo" {
sqlx::query_file!("./migrations/demo.sql")
.execute(&pool)
.await?;
2025-11-27 13:45:21 -06:00
};
Ok(Self { pool })
}
}