···11-use std::{ops::Deref, path::PathBuf};
22-33-use migration::{Migrator, MigratorTrait as _};
44-use sea_orm::{Database, DatabaseConnection};
55-use tracing::debug;
66-77-use thiserror::Error;
88-99-/// Database struct
1010-#[derive(Debug)]
1111-pub struct Db {
1212- conn: DatabaseConnection,
1313-}
1414-1515-pub type DbResult<T> = Result<T, DbError>;
1616-1717-#[derive(Debug, Error)]
1818-pub enum DbError {
1919- #[error("database file not found, tried looking at {not_found_at}")]
2020- NotFound { not_found_at: String },
2121- #[error("Seaorm Error")]
2222- SeaOrm(#[from] sea_orm::error::DbErr),
2323-}
2424-2525-impl AsRef<DatabaseConnection> for Db {
2626- fn as_ref(&self) -> &DatabaseConnection {
2727- &self.conn
2828- }
2929-}
3030-3131-impl Deref for Db {
3232- type Target = DatabaseConnection;
3333- fn deref(&self) -> &Self::Target {
3434- &self.conn
3535- }
3636-}
3737-3838-impl Db {
3939- /// Connects to the database to the database at `path`.
4040- ///
4141- /// # Errors
4242- /// Will error if `path` is not a valid file.
4343- pub async fn connect(path: impl Into<PathBuf>) -> DbResult<Self> {
4444- let path = path.into();
4545-4646- let connection_string = format!(
4747- "sqlite://{}",
4848- path.canonicalize()
4949- .map_err(|_| DbError::NotFound {
5050- not_found_at: path.to_string_lossy().to_string()
5151- })?
5252- .to_string_lossy()
5353- );
5454-5555- debug!("connecting to {connection_string}");
5656-5757- let conn = Database::connect(connection_string).await?;
5858-5959- // run all migrations on connection
6060- Migrator::up(&conn, None).await?;
6161-6262- Ok(Self { conn })
6363- }
6464-}
6565-6666-#[cfg(test)]
6767-mod tests {
6868- use std::{
6969- fs::{File, create_dir_all},
7070- path::PathBuf,
7171- };
7272-7373- use crate::Db;
7474-7575- #[tokio::test]
7676- async fn test_connect() {
7777- let path = PathBuf::new();
7878- let _ = Db::connect(&path).await.expect_err("not found");
7979-8080- let path = PathBuf::from("/tmp/filaments/test_db.sqlite");
8181- create_dir_all(path.parent().unwrap()).unwrap();
8282- let _ = File::create(&path).unwrap();
8383- let _db = Db::connect(&path).await.unwrap();
8484- }
8585-}
-4
crates/dto/src/lib.rs
···11//! The DTO's (Data Transfer Objects) used to interact with
22//! the Database. There is also a simple database struct in here.
3344-/// Database and its Errors
55-mod db;
66-pub use db::*;
77-84/// For database stuff
95pub use migration::{Migrator, MigratorTrait};
106pub use sea_orm::{Database, DatabaseConnection};
+10-4
crates/dto/tests/common/mod.rs
···33 path::PathBuf,
44};
5566-use dto::Db;
76use rand::RngExt;
77+use sea_orm::{Database, DatabaseConnection};
8899-pub async fn fresh_test_db() -> Db {
99+pub async fn fresh_test_db() -> DatabaseConnection {
1010 let rand_id = {
1111 let mut rng = rand::rng();
1212 let mut rand_id = [0_u8; 4];
···1515 String::from_utf8(rand_id.to_vec()).unwrap()
1616 };
17171818- let path = PathBuf::from(format!("/tmp/filaments/test_db_{rand_id}"));
1818+ let path = PathBuf::from(format!("/tmp/filaments/test_db_{rand_id}.db"));
19192020 create_dir_all(path.parent().unwrap()).unwrap();
21212222 let _ = File::create(&path).unwrap();
2323- Db::connect(&path).await.unwrap()
2323+2424+ let db_conn_string = format!(
2525+ "sqlite://{}",
2626+ path.clone().canonicalize().unwrap().to_string_lossy()
2727+ );
2828+2929+ Database::connect(db_conn_string).await.unwrap()
2430}