Files
Aether/crates/aether-data/src/config.rs
T

120 lines
3.6 KiB
Rust
Raw Normal View History

2026-05-05 18:27:36 +08:00
use crate::database::SqlDatabaseConfig;
use crate::driver::postgres::PostgresPoolConfig;
use crate::DataLayerError;
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct DataLayerConfig {
2026-05-05 18:27:36 +08:00
pub database: Option<SqlDatabaseConfig>,
pub postgres: Option<PostgresPoolConfig>,
}
impl DataLayerConfig {
2026-05-05 18:27:36 +08:00
pub fn from_database(database: SqlDatabaseConfig) -> Self {
Self {
database: Some(database),
postgres: None,
}
}
pub fn from_postgres(postgres: PostgresPoolConfig) -> Self {
Self {
database: Some(SqlDatabaseConfig::from_postgres_config(postgres)),
postgres: None,
}
}
pub fn effective_database(&self) -> Option<SqlDatabaseConfig> {
self.database.clone().or_else(|| {
self.postgres
.clone()
.map(SqlDatabaseConfig::from_postgres_config)
})
}
pub fn validate(&self) -> Result<(), DataLayerError> {
2026-05-05 18:27:36 +08:00
if let Some(database) = &self.database {
database.validate()?;
}
if let Some(postgres) = &self.postgres {
postgres.validate()?;
}
Ok(())
}
pub fn has_persistent_backends(&self) -> bool {
2026-05-08 00:18:12 +08:00
self.effective_database().is_some()
}
}
#[cfg(test)]
mod tests {
use super::DataLayerConfig;
2026-05-05 18:27:36 +08:00
use crate::database::{DatabaseDriver, SqlDatabaseConfig, SqlPoolConfig};
use crate::driver::postgres::PostgresPoolConfig;
#[test]
fn validates_nested_backend_configs() {
let config = DataLayerConfig {
2026-05-05 18:27:36 +08:00
database: None,
postgres: Some(PostgresPoolConfig {
database_url: "postgres://localhost/aether".to_string(),
min_connections: 2,
max_connections: 8,
acquire_timeout_ms: 1_500,
idle_timeout_ms: 5_000,
max_lifetime_ms: 30_000,
statement_cache_capacity: 64,
require_ssl: false,
}),
};
assert!(config.validate().is_ok());
assert!(config.has_persistent_backends());
}
#[test]
fn rejects_invalid_nested_backend_configs() {
let config = DataLayerConfig {
2026-05-05 18:27:36 +08:00
database: None,
postgres: Some(PostgresPoolConfig {
database_url: String::new(),
min_connections: 4,
max_connections: 2,
acquire_timeout_ms: 1_500,
idle_timeout_ms: 5_000,
max_lifetime_ms: 30_000,
statement_cache_capacity: 64,
require_ssl: false,
}),
};
assert!(config.validate().is_err());
}
2026-05-05 18:27:36 +08:00
#[test]
fn new_database_config_takes_priority_over_legacy_postgres_config() {
let config = DataLayerConfig {
database: Some(SqlDatabaseConfig {
driver: DatabaseDriver::Sqlite,
url: "sqlite://./data/aether.db".to_string(),
pool: SqlPoolConfig::default(),
}),
postgres: Some(PostgresPoolConfig {
database_url: "postgres://localhost/aether".to_string(),
min_connections: 1,
max_connections: 4,
acquire_timeout_ms: 1_000,
idle_timeout_ms: 5_000,
max_lifetime_ms: 30_000,
statement_cache_capacity: 64,
require_ssl: false,
}),
};
let effective = config
.effective_database()
.expect("database config should exist");
assert_eq!(effective.driver, DatabaseDriver::Sqlite);
}
}