My personal-knowledge-system, with deeply integrated task tracking and long term goal planning capabilities.
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: remove old db in dto

+21 -104
-85
crates/dto/src/db.rs
··· 1 - use std::{ops::Deref, path::PathBuf}; 2 - 3 - use migration::{Migrator, MigratorTrait as _}; 4 - use sea_orm::{Database, DatabaseConnection}; 5 - use tracing::debug; 6 - 7 - use thiserror::Error; 8 - 9 - /// Database struct 10 - #[derive(Debug)] 11 - pub struct Db { 12 - conn: DatabaseConnection, 13 - } 14 - 15 - pub type DbResult<T> = Result<T, DbError>; 16 - 17 - #[derive(Debug, Error)] 18 - pub enum DbError { 19 - #[error("database file not found, tried looking at {not_found_at}")] 20 - NotFound { not_found_at: String }, 21 - #[error("Seaorm Error")] 22 - SeaOrm(#[from] sea_orm::error::DbErr), 23 - } 24 - 25 - impl AsRef<DatabaseConnection> for Db { 26 - fn as_ref(&self) -> &DatabaseConnection { 27 - &self.conn 28 - } 29 - } 30 - 31 - impl Deref for Db { 32 - type Target = DatabaseConnection; 33 - fn deref(&self) -> &Self::Target { 34 - &self.conn 35 - } 36 - } 37 - 38 - impl Db { 39 - /// Connects to the database to the database at `path`. 40 - /// 41 - /// # Errors 42 - /// Will error if `path` is not a valid file. 43 - pub async fn connect(path: impl Into<PathBuf>) -> DbResult<Self> { 44 - let path = path.into(); 45 - 46 - let connection_string = format!( 47 - "sqlite://{}", 48 - path.canonicalize() 49 - .map_err(|_| DbError::NotFound { 50 - not_found_at: path.to_string_lossy().to_string() 51 - })? 52 - .to_string_lossy() 53 - ); 54 - 55 - debug!("connecting to {connection_string}"); 56 - 57 - let conn = Database::connect(connection_string).await?; 58 - 59 - // run all migrations on connection 60 - Migrator::up(&conn, None).await?; 61 - 62 - Ok(Self { conn }) 63 - } 64 - } 65 - 66 - #[cfg(test)] 67 - mod tests { 68 - use std::{ 69 - fs::{File, create_dir_all}, 70 - path::PathBuf, 71 - }; 72 - 73 - use crate::Db; 74 - 75 - #[tokio::test] 76 - async fn test_connect() { 77 - let path = PathBuf::new(); 78 - let _ = Db::connect(&path).await.expect_err("not found"); 79 - 80 - let path = PathBuf::from("/tmp/filaments/test_db.sqlite"); 81 - create_dir_all(path.parent().unwrap()).unwrap(); 82 - let _ = File::create(&path).unwrap(); 83 - let _db = Db::connect(&path).await.unwrap(); 84 - } 85 - }
-4
crates/dto/src/lib.rs
··· 1 1 //! The DTO's (Data Transfer Objects) used to interact with 2 2 //! the Database. There is also a simple database struct in here. 3 3 4 - /// Database and its Errors 5 - mod db; 6 - pub use db::*; 7 - 8 4 /// For database stuff 9 5 pub use migration::{Migrator, MigratorTrait}; 10 6 pub use sea_orm::{Database, DatabaseConnection};
+10 -4
crates/dto/tests/common/mod.rs
··· 3 3 path::PathBuf, 4 4 }; 5 5 6 - use dto::Db; 7 6 use rand::RngExt; 7 + use sea_orm::{Database, DatabaseConnection}; 8 8 9 - pub async fn fresh_test_db() -> Db { 9 + pub async fn fresh_test_db() -> DatabaseConnection { 10 10 let rand_id = { 11 11 let mut rng = rand::rng(); 12 12 let mut rand_id = [0_u8; 4]; ··· 15 15 String::from_utf8(rand_id.to_vec()).unwrap() 16 16 }; 17 17 18 - let path = PathBuf::from(format!("/tmp/filaments/test_db_{rand_id}")); 18 + let path = PathBuf::from(format!("/tmp/filaments/test_db_{rand_id}.db")); 19 19 20 20 create_dir_all(path.parent().unwrap()).unwrap(); 21 21 22 22 let _ = File::create(&path).unwrap(); 23 - Db::connect(&path).await.unwrap() 23 + 24 + let db_conn_string = format!( 25 + "sqlite://{}", 26 + path.clone().canonicalize().unwrap().to_string_lossy() 27 + ); 28 + 29 + Database::connect(db_conn_string).await.unwrap() 24 30 }
+6 -6
crates/dto/tests/task.rs
··· 16 16 file_path: Set("/voo/doo".to_owned()), 17 17 ..Default::default() 18 18 } 19 - .insert(db.as_ref()) 19 + .insert(&db) 20 20 .await 21 21 .unwrap(); 22 22 ··· 26 26 zettel_id: Set(group_zettel.nano_id.clone()), 27 27 ..Default::default() 28 28 } 29 - .insert(db.as_ref()) 29 + .insert(&db) 30 30 .await 31 31 .unwrap(); 32 32 ··· 36 36 file_path: Set("/voo/doo".to_owned()), 37 37 ..Default::default() 38 38 } 39 - .insert(db.as_ref()) 39 + .insert(&db) 40 40 .await 41 41 .unwrap(); 42 42 ··· 46 46 zettel_id: Set(task_zettel.nano_id.clone()), 47 47 ..Default::default() 48 48 } 49 - .insert(db.as_ref()) 49 + .insert(&db) 50 50 .await 51 51 .unwrap(); 52 52 ··· 54 54 .filter_by_nano_id(task.nano_id.clone()) 55 55 .with(GroupEntity) 56 56 .with(ZettelEntity) 57 - .one(db.as_ref()) 57 + .one(&db) 58 58 .await 59 59 .unwrap() 60 60 .unwrap(); ··· 63 63 .filter_by_nano_id(group.nano_id.clone()) 64 64 .with(TaskEntity) 65 65 .with(ZettelEntity) 66 - .one(db.as_ref()) 66 + .one(&db) 67 67 .await 68 68 .unwrap() 69 69 .unwrap();
+5 -5
crates/dto/tests/zettel.rs
··· 15 15 color: Set(ColorDTO::new(255, 0, 0)), 16 16 ..Default::default() 17 17 } 18 - .insert(&*db) 18 + .insert(&db) 19 19 .await 20 20 .unwrap(); 21 21 ··· 25 25 file_path: Set("/voo/doo".to_owned()), 26 26 ..Default::default() 27 27 } 28 - .insert(db.as_ref()) 28 + .insert(&db) 29 29 .await 30 30 .unwrap(); 31 31 ··· 38 38 // .set_color("some color"), 39 39 // ) 40 40 .add_tag(tag.clone().into_active_model()) 41 - .insert(&*db) 41 + .insert(&db) 42 42 .await 43 43 .unwrap(); 44 44 ··· 49 49 file_path: Set("/voo/doo".to_owned()), 50 50 ..Default::default() 51 51 } 52 - .insert(db.as_ref()) 52 + .insert(&db) 53 53 .await 54 54 .unwrap(); 55 55 56 56 let zettels_for_tag = TagEntity::load() 57 57 .filter_by_nano_id(tag.nano_id.clone()) 58 58 .with(ZettelEntity) 59 - .all(db.as_ref()) 59 + .all(&db) 60 60 .await 61 61 .unwrap(); 62 62