use crate::models::Field;
use anyhow::Result;
#[derive(Debug, Clone)]
pub struct DatabaseVersion {
pub major: u32,
pub minor: u32,
pub patch: u32,
}
impl DatabaseVersion {
pub fn new(major: u32, minor: u32, patch: u32) -> Self {
Self { major, minor, patch }
}
pub fn parse(version_str: &str) -> Result<Self> {
let parts: Vec<&str> = version_str.split('.').collect();
if parts.len() < 1 {
return Err(anyhow::anyhow!("Invalid version string: {}", version_str));
}
let major = parts[0].parse()?;
let minor = if parts.len() > 1 { parts[1].parse()? } else { 0 };
let patch = if parts.len() > 2 { parts[2].parse()? } else { 0 };
Ok(Self::new(major, minor, patch))
}
pub fn compare(&self, other: &Self) -> std::cmp::Ordering {
if self.major != other.major {
self.major.cmp(&other.major)
} else if self.minor != other.minor {
self.minor.cmp(&other.minor)
} else {
self.patch.cmp(&other.patch)
}
}
pub fn is_gte(&self, other: &Self) -> bool {
self.compare(other) != std::cmp::Ordering::Less
}
pub fn is_lt(&self, other: &Self) -> bool {
self.compare(other) == std::cmp::Ordering::Less
}
}
pub trait DatabaseCompatibility {
fn get_version(&self) -> Result<DatabaseVersion>;
fn convert_field_type(&self, field: &Field, target_version: &DatabaseVersion) -> Result<Field>;
fn generate_compatible_sql(&self, sql: &str, target_version: &DatabaseVersion) -> Result<String>;
}
pub struct MySqlCompatibility;
impl DatabaseCompatibility for MySqlCompatibility {
fn get_version(&self) -> Result<DatabaseVersion> {
todo!()
}
fn convert_field_type(&self, field: &Field, _target_version: &DatabaseVersion) -> Result<Field> {
Ok(field.clone())
}
fn generate_compatible_sql(&self, sql: &str, _target_version: &DatabaseVersion) -> Result<String> {
Ok(sql.to_string())
}
}
pub struct PostgresCompatibility;
impl DatabaseCompatibility for PostgresCompatibility {
fn get_version(&self) -> Result<DatabaseVersion> {
todo!()
}
fn convert_field_type(&self, field: &Field, _target_version: &DatabaseVersion) -> Result<Field> {
Ok(field.clone())
}
fn generate_compatible_sql(&self, sql: &str, _target_version: &DatabaseVersion) -> Result<String> {
Ok(sql.to_string())
}
}
pub struct SqliteCompatibility;
impl DatabaseCompatibility for SqliteCompatibility {
fn get_version(&self) -> Result<DatabaseVersion> {
todo!()
}
fn convert_field_type(&self, field: &Field, _target_version: &DatabaseVersion) -> Result<Field> {
Ok(field.clone())
}
fn generate_compatible_sql(&self, sql: &str, _target_version: &DatabaseVersion) -> Result<String> {
Ok(sql.to_string())
}
}