Files
YANPM/apps/container/src/db.rs
GW_MC 6d1888e6c3
Some checks failed
Test / verify-generated-code (pull_request) Successful in 8m9s
Test / test (pull_request) Successful in 7m54s
Test / lint (pull_request) Failing after 3m21s
Fix clippy warnings
2025-11-19 19:27:43 +08:00

61 lines
1.5 KiB
Rust

pub mod config;
pub mod postgresql;
pub mod sqlite;
use async_trait::async_trait;
use shared::db_type::DBType;
use std::error::Error;
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, Box<dyn Error>>;
}