pub use drasi_mysql_common::{is_valid_identifier, TableKeyConfig};
#[derive(Clone, PartialEq)]
pub struct MySqlBootstrapConfig {
pub host: String,
pub port: u16,
pub database: String,
pub user: String,
pub password: String,
pub tables: Vec<String>,
pub table_keys: Vec<TableKeyConfig>,
}
impl std::fmt::Debug for MySqlBootstrapConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MySqlBootstrapConfig")
.field("host", &self.host)
.field("port", &self.port)
.field("database", &self.database)
.field("user", &self.user)
.field("password", &"[REDACTED]")
.field("tables", &self.tables)
.field("table_keys", &self.table_keys)
.finish()
}
}
impl MySqlBootstrapConfig {
pub fn validate(&self) -> anyhow::Result<()> {
if self.database.is_empty() {
return Err(anyhow::anyhow!(
"Validation error: database cannot be empty. \
Please specify the MySQL database name"
));
}
if self.user.is_empty() {
return Err(anyhow::anyhow!(
"Validation error: user cannot be empty. \
Please specify the MySQL user"
));
}
if self.port == 0 {
return Err(anyhow::anyhow!(
"Validation error: port cannot be 0. \
Please specify a valid port number (1-65535)"
));
}
if self.tables.is_empty() {
return Err(anyhow::anyhow!(
"Validation error: tables cannot be empty. \
Please configure at least one table to bootstrap"
));
}
for table in &self.tables {
if !is_valid_identifier(table) {
return Err(anyhow::anyhow!(
"Validation error: table '{table}' contains invalid characters. \
Only letters, numbers, and underscores are allowed"
));
}
}
Ok(())
}
}