Add container simulation with PostgreSQL and SQLite support
This commit is contained in:
59
apps/container/src/db.rs
Normal file
59
apps/container/src/db.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
pub mod config;
|
||||
pub mod postgresql;
|
||||
pub mod sqlite;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use shared::db_type::DBType;
|
||||
use std::future::Future;
|
||||
use std::{pin::Pin, sync::Arc};
|
||||
use url::Host;
|
||||
|
||||
use testcontainers::{ContainerAsync, GenericImage, TestcontainersError};
|
||||
|
||||
use crate::{ConfigInfoType, WithContainer, WithoutContainer};
|
||||
|
||||
pub type UnStartedContainer =
|
||||
Pin<Box<dyn Future<Output = Result<ContainerAsync<GenericImage>, TestcontainersError>> + Send>>;
|
||||
|
||||
pub type DBConfigInfoType = ConfigInfoType<ContainerizedDBInfo, PreExistingDBInfo>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PreExistingDBInfo {
|
||||
pub db_type: DBType,
|
||||
pub url: String,
|
||||
pub on_delete: Arc<dyn Fn() + Send + Sync>,
|
||||
}
|
||||
|
||||
impl WithoutContainer for PreExistingDBInfo {
|
||||
fn on_delete(&self) {
|
||||
(self.on_delete)();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ContainerizedDBInfo {
|
||||
pub db_type: DBType,
|
||||
pub container: Arc<ContainerAsync<GenericImage>>,
|
||||
pub container_name: String,
|
||||
pub database_name: String,
|
||||
pub host: Host,
|
||||
pub port: u16,
|
||||
pub url: String,
|
||||
pub user: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
impl WithContainer for ContainerizedDBInfo {
|
||||
fn container(&self) -> &Arc<ContainerAsync<GenericImage>> {
|
||||
&self.container
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait DBInfo<T> {
|
||||
async fn new(config: Option<T>) -> Self
|
||||
where
|
||||
Self: Sized;
|
||||
async fn get_db_container_config_info(&self) -> DBConfigInfoType;
|
||||
fn get_unstarted_container(&self) -> Result<UnStartedContainer, ()>;
|
||||
}
|
||||
Reference in New Issue
Block a user