-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use better connection management * Use clippy * Only print error that id3v2 tag is not supported. * Updated frankenstein
- Loading branch information
1 parent
9c6fdc7
commit 2707e20
Showing
60 changed files
with
831 additions
and
1,149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::process::exit; | ||
use std::sync::OnceLock; | ||
use std::time::Duration; | ||
use diesel::Connection; | ||
use diesel::r2d2::ConnectionManager; | ||
use r2d2::Pool; | ||
use crate::adapters::persistence::dbconfig::DBType; | ||
use crate::constants::inner_constants::ENVIRONMENT_SERVICE; | ||
use crate::DbPool; | ||
|
||
#[derive(Debug)] | ||
pub struct ConnectionOptions { | ||
pub enable_wal: bool, | ||
pub enable_foreign_keys: bool, | ||
pub busy_timeout: Option<Duration>, | ||
} | ||
|
||
impl r2d2::CustomizeConnection<DBType, diesel::r2d2::Error> for ConnectionOptions { | ||
fn on_acquire(&self, conn: &mut DBType) -> Result<(), diesel::r2d2::Error> { | ||
use diesel::connection::SimpleConnection; | ||
(|| { | ||
if self.enable_wal { | ||
conn.batch_execute("PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL;")?; | ||
} | ||
if self.enable_foreign_keys { | ||
conn.batch_execute("PRAGMA foreign_keys = ON;")?; | ||
} | ||
if let Some(d) = self.busy_timeout { | ||
conn.batch_execute(&format!("PRAGMA busy_timeout = {};", d.as_millis()))?; | ||
} | ||
Ok(()) | ||
})() | ||
.map_err(diesel::r2d2::Error::QueryError) | ||
} | ||
} | ||
|
||
pub fn establish_connection() -> DBType { | ||
let database_url = &ENVIRONMENT_SERVICE.get().unwrap().database_url; | ||
DBType::establish(database_url).unwrap_or_else(|e| { | ||
log::error!("Error connecting to {} with reason {}", database_url, e); | ||
exit(1) | ||
}) | ||
} | ||
|
||
static POOL: OnceLock<DbPool> = OnceLock::new(); | ||
|
||
|
||
pub fn get_connection() -> r2d2::PooledConnection<ConnectionManager<DBType>> { | ||
POOL.get_or_init(init_pool).get().unwrap() | ||
} | ||
|
||
fn init_pool() -> DbPool { | ||
let conn = establish_connection(); | ||
match conn { | ||
DBType::Postgresql(_) => { | ||
init_postgres_db_pool(&ENVIRONMENT_SERVICE.get().unwrap().database_url) | ||
.expect("Failed to connect to database") | ||
} | ||
DBType::Sqlite(_) => { | ||
init_sqlite_db_pool(&ENVIRONMENT_SERVICE.get().unwrap().database_url) | ||
.expect("Failed to connect to database") | ||
} | ||
} | ||
} | ||
|
||
|
||
fn init_postgres_db_pool( | ||
database_url: &str, | ||
) -> Result<Pool<ConnectionManager<DBType>>, String> { | ||
let env_service = ENVIRONMENT_SERVICE.get().unwrap(); | ||
let db_connections = env_service.conn_number; | ||
let manager = ConnectionManager::<DBType>::new(database_url); | ||
let pool = Pool::builder() | ||
.max_size(db_connections as u32) | ||
.build(manager) | ||
.expect("Failed to create pool."); | ||
Ok(pool) | ||
} | ||
|
||
fn init_sqlite_db_pool( | ||
database_url: &str, | ||
) -> Result<Pool<ConnectionManager<DBType>>, String> { | ||
let manager = ConnectionManager::<DBType>::new(database_url); | ||
let pool = Pool::builder() | ||
.max_size(16) | ||
.connection_customizer(Box::new(ConnectionOptions { | ||
enable_wal: true, | ||
enable_foreign_keys: true, | ||
busy_timeout: Some(Duration::from_secs(120)), | ||
})) | ||
.build(manager) | ||
.unwrap(); | ||
Ok(pool) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#[path = "schemas/sqlite/schema.rs"] | ||
pub mod schema; | ||
pub mod db; | ||
|
||
use diesel::QueryResult; | ||
|
||
#[derive(diesel::MultiConnection)] | ||
pub enum DBType { | ||
Postgresql(diesel::PgConnection), | ||
Sqlite(diesel::SqliteConnection), | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! import_database_config { | ||
() => { | ||
pub const SQLITE_MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations/sqlite"); | ||
|
||
pub const POSTGRES_MIGRATIONS: EmbeddedMigrations = | ||
embed_migrations!("./migrations/postgres"); | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! execute_with_conn { | ||
($diesel_func:expr) => { | ||
{ | ||
use $crate::get_connection; | ||
use std::ops::DerefMut; | ||
|
||
let mut conn = get_connection(); | ||
let _ = match conn.deref_mut() { | ||
$crate::adapters::persistence::dbconfig::DBType::Sqlite(conn) => return $diesel_func | ||
(conn), | ||
$crate::adapters::persistence::dbconfig::DBType::Postgresql(conn) => return $diesel_func(conn), | ||
}; | ||
} | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! insert_with_conn { | ||
($diesel_func:expr) => { | ||
{ | ||
|
||
use $crate::get_connection; | ||
use std::ops::DerefMut; | ||
let mut conn = get_connection(); | ||
let _ = match conn.deref_mut() { | ||
$crate::adapters::persistence::dbconfig::DBType::Sqlite(conn) => $diesel_func(conn), | ||
$crate::adapters::persistence::dbconfig::DBType::Postgresql(conn) => $diesel_func(conn), | ||
}; | ||
} | ||
}; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod repositories; | ||
pub mod model; | ||
pub mod model; | ||
pub mod dbconfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 11 additions & 10 deletions
21
src/adapters/persistence/repositories/device/device_repository.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,41 @@ | ||
use diesel::RunQueryDsl; | ||
use crate::adapters::persistence::model::device::device_entity::DeviceEntity; | ||
use crate::application::repositories::device_repository::DeviceRepository; | ||
use crate::dbconfig::DBType; | ||
use crate::domain::models::device::model::Device; | ||
use crate::execute_with_conn; | ||
use crate::utils::error::{map_db_error, CustomError}; | ||
|
||
pub struct DeviceRepositoryImpl; | ||
|
||
use diesel::QueryDsl; | ||
use crate::dbconfig::schema::devices::dsl::*; | ||
use crate::adapters::persistence::dbconfig::schema::devices::dsl::*; | ||
use diesel::ExpressionMethods; | ||
use DBType as DbConnection; | ||
use crate::adapters::persistence::dbconfig::db::get_connection; | ||
|
||
impl DeviceRepository for DeviceRepositoryImpl { | ||
fn create(device: Device, conn: &mut DBType) -> Result<Device, CustomError> { | ||
fn create(device: Device) -> Result<Device, CustomError> { | ||
let device_entity: DeviceEntity = device.into(); | ||
execute_with_conn!(conn, |conn| diesel::insert_into(devices) | ||
execute_with_conn!(|conn| diesel::insert_into(devices) | ||
.values(device_entity) | ||
.get_result(conn) | ||
.map_err(map_db_error) | ||
.map(|device_entity: DeviceEntity| device_entity.into())) | ||
|
||
} | ||
|
||
fn get_devices_of_user(username_to_find: String, conn: &mut DBType) -> Result<Vec<Device>, | ||
fn get_devices_of_user(username_to_find: String) -> Result<Vec<Device>, | ||
CustomError> { | ||
devices | ||
.filter(username.eq(username_to_find)) | ||
.load::<DeviceEntity>(conn) | ||
.load::<DeviceEntity>(&mut get_connection()) | ||
.map(|device_entity| device_entity.into_iter().map(|device_entity| device_entity.into()).collect()) | ||
.map_err(map_db_error) | ||
} | ||
|
||
fn delete_by_username(username1: &str, conn: &mut DBType) -> Result<(), CustomError> { | ||
use crate::dbconfig::schema::devices::dsl::*; | ||
diesel::delete(devices.filter(username.eq(username1))).execute(conn).map(|_|()).map_err(map_db_error) | ||
fn delete_by_username(username1: &str) -> Result<(), CustomError> { | ||
use crate::adapters::persistence::dbconfig::schema::devices::dsl::*; | ||
diesel::delete(devices.filter(username.eq(username1))).execute(&mut get_connection()).map(|_|()) | ||
.map_err | ||
(map_db_error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
use crate::dbconfig::DBType; | ||
use crate::domain::models::device::model::Device; | ||
use crate::utils::error::CustomError; | ||
|
||
pub trait DeviceRepository { | ||
fn create(device: Device, conn: &mut DBType) -> Result<Device, CustomError>; | ||
fn get_devices_of_user(username: String, conn: &mut DBType) -> Result<Vec<Device>, CustomError>; | ||
fn delete_by_username(username: &str, conn: &mut DBType) -> Result<(), CustomError>; | ||
fn create(device: Device) -> Result<Device, CustomError>; | ||
fn get_devices_of_user(username: String) -> Result<Vec<Device>, CustomError>; | ||
fn delete_by_username(username: &str) -> Result<(), CustomError>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
use crate::DbPool; | ||
use crate::domain::models::device::model::Device; | ||
use crate::utils::error::CustomError; | ||
|
||
pub trait CreateUseCase { | ||
fn create(device_to_safe: Device, conn: &DbPool) -> Result<Device, CustomError>; | ||
fn create(device_to_safe: Device) -> Result<Device, CustomError>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
use crate::DbPool; | ||
use crate::utils::error::CustomError; | ||
|
||
pub trait EditUseCase { | ||
fn delete_by_username(username: &str, conn: &DbPool) -> Result<(), CustomError>; | ||
fn delete_by_username(username: &str) -> Result<(), CustomError>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
use crate::DbPool; | ||
use crate::domain::models::device::model::Device; | ||
use crate::utils::error::CustomError; | ||
|
||
pub trait QueryUseCase { | ||
fn query_by_username(username: String, pool: &DbPool) -> Result<Vec<Device>, CustomError>; | ||
fn query_by_username(username: String) -> Result<Vec<Device>, CustomError>; | ||
} |
Oops, something went wrong.