use super::{FieldType, TableConfig};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SchemaColumn {
pub name: String,
pub data_type: String,
pub column_type: String,
pub nullable: bool,
pub max_length: Option<u64>,
}
impl SchemaColumn {
pub fn new(
name: impl Into<String>,
data_type: impl Into<String>,
column_type: impl Into<String>,
nullable: bool,
max_length: Option<u64>,
) -> Self {
Self {
name: name.into(),
data_type: data_type.into().to_ascii_lowercase(),
column_type: column_type.into().to_ascii_lowercase(),
nullable,
max_length,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SchemaIssueKind {
MissingColumn,
IncompatibleType,
NullabilityMismatch,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SchemaIssue {
pub field: String,
pub kind: SchemaIssueKind,
pub expected: String,
pub actual: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SchemaValidationReport {
pub table: String,
pub issues: Vec<SchemaIssue>,
}
impl SchemaValidationReport {
pub fn is_compatible(&self) -> bool {
self.issues.is_empty()
}
}
impl TableConfig {
pub fn validate_schema(&self, columns: &[SchemaColumn]) -> SchemaValidationReport {
let mut fields: Vec<_> = self.fields.values().collect();
fields.sort_by(|left, right| left.name.cmp(&right.name));
let mut issues = Vec::new();
for field in fields {
let Some(column) = columns.iter().find(|column| column.name == field.name) else {
issues.push(SchemaIssue {
field: field.name.clone(),
kind: SchemaIssueKind::MissingColumn,
expected: field.field_type.display_name().to_string(),
actual: None,
});
continue;
};
if !is_type_compatible(&field.field_type, column) {
issues.push(SchemaIssue {
field: field.name.clone(),
kind: SchemaIssueKind::IncompatibleType,
expected: field.field_type.display_name().to_string(),
actual: Some(column.column_type.clone()),
});
}
if field.required && column.nullable {
issues.push(SchemaIssue {
field: field.name.clone(),
kind: SchemaIssueKind::NullabilityMismatch,
expected: "NOT NULL".to_string(),
actual: Some("NULL".to_string()),
});
}
}
SchemaValidationReport {
table: self.table_name.clone(),
issues,
}
}
}
fn is_type_compatible(field_type: &FieldType, column: &SchemaColumn) -> bool {
let data_type = column.data_type.as_str();
match field_type {
FieldType::String { max_length } => {
matches!(data_type, "varchar" | "char")
&& column
.max_length
.is_some_and(|actual| actual >= *max_length as u64)
}
FieldType::Integer => matches!(
data_type,
"tinyint" | "smallint" | "mediumint" | "int" | "integer" | "bigint"
),
FieldType::BigInt => data_type == "bigint",
FieldType::Float => matches!(data_type, "float" | "double" | "decimal"),
FieldType::Double => matches!(data_type, "double" | "decimal"),
FieldType::Boolean => {
matches!(data_type, "bool" | "boolean")
|| (data_type == "tinyint" && column.column_type.starts_with("tinyint(1)"))
}
FieldType::Date => data_type == "date",
FieldType::DateTime => matches!(data_type, "datetime" | "timestamp"),
FieldType::Timestamp => data_type == "bigint",
FieldType::Json => data_type == "json",
FieldType::Text => matches!(data_type, "tinytext" | "text" | "mediumtext" | "longtext"),
FieldType::Enum { .. } => matches!(data_type, "enum" | "varchar" | "char"),
FieldType::ForeignKey { .. } => true,
}
}