From efb54016913f840ee46afe9270ae40048b8e9041 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 13 Apr 2022 12:20:26 +0530 Subject: [PATCH] feat: load and init db-sqlx-sqlite with testing environment setup --- src/db.rs | 41 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 +++ src/tests.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 src/db.rs create mode 100644 src/tests.rs diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..cd6722d --- /dev/null +++ b/src/db.rs @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2022 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +use crate::settings::Settings; +use db_core::prelude::*; + +pub type BoxDB = Box; + +pub mod sqlite { + use super::*; + use db_sqlx_sqlite::{ConnectionOptions, Fresh}; + use sqlx::sqlite::SqlitePoolOptions; + + pub async fn get_data(settings: Option) -> BoxDB { + let settings = settings.unwrap_or_else(|| Settings::new().unwrap()); + + let pool = settings.database.pool; + let pool_options = SqlitePoolOptions::new().max_connections(pool); + let connection_options = ConnectionOptions::Fresh(Fresh { + pool_options, + url: settings.database.url, + }); + + let db = connection_options.connect().await.unwrap(); + db.migrate().await.unwrap(); + Box::new(db) + } +} diff --git a/src/main.rs b/src/main.rs index 4318a41..a378895 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,9 +16,12 @@ * along with this program. If not, see . */ pub mod data; +pub mod db; pub mod gitea; pub mod settings; pub mod spider; +#[cfg(test)] +mod tests; pub mod utils; pub mod verify; diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..f7c851e --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,54 @@ +/* + * ForgeFlux StarChart - A federated software forge spider + * Copyright (C) 2022 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +use std::env; +pub use std::sync::Arc; + +use serde::Serialize; + +use crate::data::Data; +pub use crate::db::BoxDB; +use crate::settings::{DBType, Settings}; + +//pub mod sqlx_postgres { +// use super::*; +// +// pub async fn get_data() -> (BoxDB, Arc) { +// let url = env::var("POSTGRES_DATABASE_URL").unwrap(); +// let mut settings = Settings::new().unwrap(); +// settings.database.url = url.clone(); +// settings.database.database_type = DBType::Postgres; +// let db = pg::get_data(Some(settings.clone())).await; +// (db, Data::new(Some(settings))) +// } +//} + +pub mod sqlx_sqlite { + use super::*; + use crate::db::sqlite; + + pub async fn get_data() -> (BoxDB, Arc) { + let url = env::var("SQLITE_DATABASE_URL").unwrap(); + println!("found db url: {url}"); + let mut settings = Settings::new().unwrap(); + settings.database.url = url.clone(); + settings.database.database_type = DBType::Sqlite; + let db = sqlite::get_data(Some(settings.clone())).await; + (db, Data::new(settings).await) + } +}