use ignis_db::introspection::DatabaseIntrospector;
use ignis_db::migrate::SchemaMigrationManager;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("🧪 Testing Schema Diff with New Object Types");
let pool =
sqlx::PgPool::connect("postgresql://postgres:postgres@localhost:5433/postgres").await?;
let introspector = DatabaseIntrospector::new(pool.clone());
println!("🔍 Introspecting current database state...");
let current_tables = introspector.introspect_tables().await?;
let current_views = introspector.introspect_views().await?;
let current_composite_types = introspector.introspect_composite_types().await?;
let current_indexes = introspector.introspect_indexes().await?;
let current_functions = introspector.introspect_functions().await?;
let current_procedures = introspector.introspect_procedures().await?;
let current_triggers = introspector.introspect_triggers().await?;
println!("📊 Current state:");
println!(" • Tables: {}", current_tables.len());
println!(" • Views: {}", current_views.len());
println!(" • Composite Types: {}", current_composite_types.len());
println!(" • Indexes: {}", current_indexes.len());
println!(" • Functions: {}", current_functions.len());
println!(" • Procedures: {}", current_procedures.len());
println!(" • Triggers: {}", current_triggers.len());
println!("\n🔍 Testing schema diff with identical states...");
let manager = SchemaMigrationManager::new()
.set_old_schema(
current_tables
.clone()
.into_iter()
.map(|t| (t.name.clone(), t))
.collect(),
)
.set_new_schema(
current_tables
.clone()
.into_iter()
.map(|t| (t.name.clone(), t))
.collect(),
)
.set_old_views(
current_views
.clone()
.into_iter()
.map(|v| (v.name.clone(), v))
.collect(),
)
.set_new_views(
current_views
.clone()
.into_iter()
.map(|v| (v.name.clone(), v))
.collect(),
)
.set_old_composite_types(
current_composite_types
.clone()
.into_iter()
.map(|ct| (ct.name.clone(), ct))
.collect(),
)
.set_new_composite_types(
current_composite_types
.clone()
.into_iter()
.map(|ct| (ct.name.clone(), ct))
.collect(),
)
.set_old_functions(
current_functions
.clone()
.into_iter()
.map(|f| (f.name.clone(), f))
.collect(),
)
.set_new_functions(
current_functions
.clone()
.into_iter()
.map(|f| (f.name.clone(), f))
.collect(),
)
.set_old_procedures(
current_procedures
.clone()
.into_iter()
.map(|p| (p.name.clone(), p))
.collect(),
)
.set_new_procedures(
current_procedures
.clone()
.into_iter()
.map(|p| (p.name.clone(), p))
.collect(),
)
.set_old_triggers(
current_triggers
.clone()
.into_iter()
.map(|t| (t.name.clone(), t))
.collect(),
)
.set_new_triggers(
current_triggers
.clone()
.into_iter()
.map(|t| (t.name.clone(), t))
.collect(),
);
match manager.generate_migration("test_no_changes") {
Ok(_) => println!("❌ Expected error for no changes, but got success"),
Err(e) => {
if e.to_string().contains("No schema changes detected") {
println!("✅ Correctly detected no schema changes");
} else {
println!("❌ Unexpected error: {}", e);
}
}
}
println!("\n🔍 Testing schema diff with modified function...");
let mut modified_functions = current_functions.clone();
if let Some(func) = modified_functions
.iter_mut()
.find(|f| f.name == "get_user_by_id")
{
*func = func
.clone()
.with_body("SELECT id, username, email FROM \"user\" WHERE id = $1");
}
let manager_with_changes = SchemaMigrationManager::new()
.set_old_schema(
current_tables
.clone()
.into_iter()
.map(|t| (t.name.clone(), t))
.collect(),
)
.set_new_schema(
current_tables
.clone()
.into_iter()
.map(|t| (t.name.clone(), t))
.collect(),
)
.set_old_views(
current_views
.clone()
.into_iter()
.map(|v| (v.name.clone(), v))
.collect(),
)
.set_new_views(
current_views
.clone()
.into_iter()
.map(|v| (v.name.clone(), v))
.collect(),
)
.set_old_composite_types(
current_composite_types
.clone()
.into_iter()
.map(|ct| (ct.name.clone(), ct))
.collect(),
)
.set_new_composite_types(
current_composite_types
.clone()
.into_iter()
.map(|ct| (ct.name.clone(), ct))
.collect(),
)
.set_old_functions(
current_functions
.clone()
.into_iter()
.map(|f| (f.name.clone(), f))
.collect(),
)
.set_new_functions(
modified_functions
.into_iter()
.map(|f| (f.name.clone(), f))
.collect(),
)
.set_old_procedures(
current_procedures
.clone()
.into_iter()
.map(|p| (p.name.clone(), p))
.collect(),
)
.set_new_procedures(
current_procedures
.clone()
.into_iter()
.map(|p| (p.name.clone(), p))
.collect(),
)
.set_old_triggers(
current_triggers
.clone()
.into_iter()
.map(|t| (t.name.clone(), t))
.collect(),
)
.set_new_triggers(
current_triggers
.clone()
.into_iter()
.map(|t| (t.name.clone(), t))
.collect(),
);
match manager_with_changes.generate_migration("test_function_changes") {
Ok(migration) => {
println!("✅ Correctly detected function changes!");
println!("📝 Generated migration: {}", migration.id);
println!("📄 UP SQL length: {} characters", migration.up_sql.len());
if let Some(down_sql) = &migration.down_sql {
println!("📄 DOWN SQL length: {} characters", down_sql.len());
}
}
Err(e) => {
println!("❌ Failed to detect function changes: {}", e);
}
}
println!("\n🔍 Testing schema diff with modified view...");
let mut modified_views = current_views.clone();
if let Some(view) = modified_views.iter_mut().find(|v| v.name == "user_summary") {
*view = view.clone().with_ddl("SELECT u.id, u.username, u.email, COUNT(p.id) as post_count FROM \"user\" u LEFT JOIN post p ON u.id = p.user_id GROUP BY u.id, u.username, u.email");
}
let manager_with_view_changes = SchemaMigrationManager::new()
.set_old_schema(
current_tables
.clone()
.into_iter()
.map(|t| (t.name.clone(), t))
.collect(),
)
.set_new_schema(
current_tables
.clone()
.into_iter()
.map(|t| (t.name.clone(), t))
.collect(),
)
.set_old_views(
current_views
.clone()
.into_iter()
.map(|v| (v.name.clone(), v))
.collect(),
)
.set_new_views(
modified_views
.into_iter()
.map(|v| (v.name.clone(), v))
.collect(),
)
.set_old_composite_types(
current_composite_types
.clone()
.into_iter()
.map(|ct| (ct.name.clone(), ct))
.collect(),
)
.set_new_composite_types(
current_composite_types
.clone()
.into_iter()
.map(|ct| (ct.name.clone(), ct))
.collect(),
)
.set_old_functions(
current_functions
.clone()
.into_iter()
.map(|f| (f.name.clone(), f))
.collect(),
)
.set_new_functions(
current_functions
.clone()
.into_iter()
.map(|f| (f.name.clone(), f))
.collect(),
)
.set_old_procedures(
current_procedures
.clone()
.into_iter()
.map(|p| (p.name.clone(), p))
.collect(),
)
.set_new_procedures(
current_procedures
.clone()
.into_iter()
.map(|p| (p.name.clone(), p))
.collect(),
)
.set_old_triggers(
current_triggers
.clone()
.into_iter()
.map(|t| (t.name.clone(), t))
.collect(),
)
.set_new_triggers(
current_triggers
.clone()
.into_iter()
.map(|t| (t.name.clone(), t))
.collect(),
);
match manager_with_view_changes.generate_migration("test_view_changes") {
Ok(migration) => {
println!("✅ Correctly detected view changes!");
println!("📝 Generated migration: {}", migration.id);
println!("📄 UP SQL length: {} characters", migration.up_sql.len());
if let Some(down_sql) = &migration.down_sql {
println!("📄 DOWN SQL length: {} characters", down_sql.len());
}
}
Err(e) => {
println!("❌ Failed to detect view changes: {}", e);
}
}
println!("\n🎉 Schema diff test completed!");
Ok(())
}