use drasi_lib::bootstrap::BootstrapProvider;
use drasi_plugin_sdk::prelude::*;
use utoipa::OpenApi;
use crate::{MySqlBootstrapConfig, MySqlBootstrapProvider, TableKeyConfig};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, utoipa::ToSchema)]
#[schema(as = bootstrap::mysql::MySqlTableKeyConfig)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct MySqlTableKeyConfigDto {
pub table: String,
pub key_columns: Vec<String>,
}
fn default_host() -> ConfigValue<String> {
ConfigValue::Static("localhost".to_string()) }
fn default_port() -> ConfigValue<u16> {
ConfigValue::Static(3306)
}
fn default_password() -> ConfigValue<String> {
ConfigValue::Static(String::new())
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, utoipa::ToSchema)]
#[schema(as = bootstrap::mysql::MySqlBootstrapConfig)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct MySqlBootstrapConfigDto {
#[serde(default = "default_host")]
#[schema(value_type = ConfigValueString)]
pub host: ConfigValue<String>,
#[serde(default = "default_port")]
#[schema(value_type = ConfigValueU16)]
pub port: ConfigValue<u16>,
#[schema(value_type = ConfigValueString)]
pub database: ConfigValue<String>,
#[schema(value_type = ConfigValueString)]
pub user: ConfigValue<String>,
#[serde(default = "default_password")]
#[schema(value_type = ConfigValueString)]
pub password: ConfigValue<String>,
#[serde(default)]
pub tables: Vec<String>,
#[serde(default)]
#[schema(value_type = Vec<bootstrap::mysql::MySqlTableKeyConfig>)]
pub table_keys: Vec<MySqlTableKeyConfigDto>,
}
#[derive(OpenApi)]
#[openapi(components(schemas(MySqlBootstrapConfigDto, MySqlTableKeyConfigDto,)))]
struct MySqlBootstrapSchemas;
pub struct MySqlBootstrapDescriptor;
#[async_trait]
impl BootstrapPluginDescriptor for MySqlBootstrapDescriptor {
fn kind(&self) -> &str {
"mysql"
}
fn config_version(&self) -> &str {
"1.0.0"
}
fn config_schema_name(&self) -> &str {
"bootstrap.mysql.MySqlBootstrapConfig"
}
fn config_schema_json(&self) -> String {
let api = MySqlBootstrapSchemas::openapi();
serde_json::to_string(
&api.components
.as_ref()
.expect("OpenAPI components missing")
.schemas,
)
.expect("Failed to serialize config schema")
}
async fn create_bootstrap_provider(
&self,
config_json: &serde_json::Value,
_source_config_json: &serde_json::Value,
) -> anyhow::Result<Box<dyn BootstrapProvider>> {
let dto: MySqlBootstrapConfigDto = serde_json::from_value(config_json.clone())?;
let mapper = DtoMapper::new();
let table_keys = dto
.table_keys
.into_iter()
.map(|tk| TableKeyConfig {
table: tk.table,
key_columns: tk.key_columns,
})
.collect();
let config = MySqlBootstrapConfig {
host: mapper.resolve_string(&dto.host).await?,
port: mapper.resolve_typed(&dto.port).await?,
database: mapper.resolve_string(&dto.database).await?,
user: mapper.resolve_string(&dto.user).await?,
password: mapper.resolve_string(&dto.password).await?,
tables: dto.tables,
table_keys,
};
let provider = MySqlBootstrapProvider::new(config);
Ok(Box::new(provider))
}
}