| 1 |
use anyhow::Result; |
| 2 |
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; |
| 3 |
use sqlx::SqlitePool; |
| 4 |
use std::path::Path; |
| 5 |
use std::str::FromStr; |
| 6 |
|
| 7 |
pub async fn connect(path: &Path) -> Result<SqlitePool> { |
| 8 |
let url = format!("sqlite://{}?mode=rwc", path.display()); |
| 9 |
let opts = SqliteConnectOptions::from_str(&url)? |
| 10 |
.create_if_missing(true) |
| 11 |
.foreign_keys(true); |
| 12 |
let pool = SqlitePoolOptions::new().max_connections(4).connect_with(opts).await?; |
| 13 |
Ok(pool) |
| 14 |
} |
| 15 |
|
| 16 |
pub async fn migrate(pool: &SqlitePool) -> Result<()> { |
| 17 |
sqlx::migrate!("./migrations").run(pool).await?; |
| 18 |
Ok(()) |
| 19 |
} |
| 20 |
|