use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ColumnDefinition {
pub name: String,
pub data_type: String,
pub nullable: bool,
pub default_value: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConstraintDefinition {
pub name: Option<String>,
pub constraint_type: String,
pub columns: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableSnapshot {
pub table_name: String,
pub columns: Vec<ColumnDefinition>,
pub constraints: Vec<ConstraintDefinition>,
}
impl TableSnapshot {
pub fn new(table_name: &str) -> Self {
Self {
table_name: table_name.to_string(),
columns: Vec::new(),
constraints: Vec::new(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigrationMetadata {
pub version: u32,
pub name: String,
pub up_sql: String,
pub down_sql: Option<String>,
pub table_snapshots: HashMap<String, TableSnapshot>,
}
impl MigrationMetadata {
pub fn new(version: u32, name: String, up_sql: String) -> Self {
Self {
version,
name,
up_sql,
down_sql: None,
table_snapshots: HashMap::new(),
}
}
pub fn add_table_snapshot(&mut self, snapshot: TableSnapshot) {
self.table_snapshots.insert(snapshot.table_name.clone(), snapshot);
}
pub fn get_table_snapshot(&self, table_name: &str) -> Option<&TableSnapshot> {
self.table_snapshots.get(table_name)
}
}
#[cfg(test)]
mod tests {
use super::super::{MigrationMetadata, TableSnapshot};
#[test]
fn test_create_metadata() {
let metadata = MigrationMetadata::new(
1,
"test_migration".to_string(),
"CREATE TABLE test (id INT);".to_string(),
);
assert_eq!(metadata.version, 1);
assert_eq!(metadata.name, "test_migration");
assert!(metadata.table_snapshots.is_empty());
}
#[test]
fn test_add_table_snapshot() {
let mut metadata = MigrationMetadata::new(1, "test".to_string(), "CREATE TABLE test (id INT);".to_string());
let snapshot = TableSnapshot::new("test");
metadata.add_table_snapshot(snapshot);
assert_eq!(metadata.table_snapshots.len(), 1);
assert!(metadata.table_snapshots.contains_key("test"));
}
#[test]
fn test_get_nonexistent_table() {
let metadata = MigrationMetadata::new(1, "test".to_string(), "CREATE TABLE test (id INT);".to_string());
assert!(metadata.get_table_snapshot("nonexistent").is_none());
}
}