Skip to main content

shared/infrastructure/database/sqlite/
repository.rs

1use sqlx::{Executor, Pool, Sqlite, SqlitePool};
2
3use crate::error::Result;
4
5pub struct SQLite {
6    pub pool: Pool<Sqlite>,
7}
8impl SQLite {
9    pub async fn start(conn: &str) -> Result<Self> {
10        let pool = SqlitePool::connect(conn).await.inspect_err(|e| {
11            tracing::error!(
12                error_code = "InternalError::Database",
13                "Failed to connect to database - {e}"
14            );
15        })?;
16
17        pool.execute("PRAGMA foreign_keys = ON;").await?;
18
19        Ok(Self { pool })
20    }
21
22    pub async fn run_migrations(&self) -> Result<()> {
23        let path = std::path::Path::new("../../migrations/sqlite");
24        if path.exists() {
25            let migrator = sqlx::migrate::Migrator::new(path).await?;
26            migrator.run(&self.pool).await.expect("migrations to run");
27        }
28
29        Ok(())
30    }
31}