feat: load and init db-sqlx-sqlite with testing environment setup

This commit is contained in:
Aravinth Manivannan 2022-04-13 12:20:26 +05:30
parent ec97f096de
commit efb5401691
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
3 changed files with 98 additions and 0 deletions

41
src/db.rs Normal file
View file

@ -0,0 +1,41 @@
/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
use crate::settings::Settings;
use db_core::prelude::*;
pub type BoxDB = Box<dyn SCDatabase>;
pub mod sqlite {
use super::*;
use db_sqlx_sqlite::{ConnectionOptions, Fresh};
use sqlx::sqlite::SqlitePoolOptions;
pub async fn get_data(settings: Option<Settings>) -> 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)
}
}

View file

@ -16,9 +16,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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;

54
src/tests.rs Normal file
View file

@ -0,0 +1,54 @@
/*
* ForgeFlux StarChart - A federated software forge spider
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
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<Data>) {
// 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<Data>) {
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)
}
}