use ignis_db::{migrate::SchemaMigrationManager, table, IgnisObject};
use std::collections::HashMap;
table! {
users_v1 (id) {
id -> Int4,
name -> Text,
}
}
table! {
users_v2 (id) {
id -> Int4,
name -> Text,
email -> Varchar,
created_at -> Timestamptz,
active -> Bool,
}
}
table! {
posts (id) {
id -> Int4,
user_id -> Int4,
title -> Varchar,
content -> Text,
published -> Bool,
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
println!("Ignis Migration System Example");
println!("==============================\n");
let mut old_schema = HashMap::new();
old_schema.insert("users".to_string(), users_v1::table());
let mut new_schema = HashMap::new();
new_schema.insert("users".to_string(), users_v2::table());
new_schema.insert("posts".to_string(), posts::table());
println!("Old Schema:");
println!(" users: {} columns", users_v1::table().columns.len());
for col in &users_v1::table().columns {
println!(" - {}: {}", col.name, col.column_type.to_sql());
}
println!();
println!("New Schema:");
println!(" users: {} columns", users_v2::table().columns.len());
for col in &users_v2::table().columns {
println!(" - {}: {}", col.name, col.column_type.to_sql());
}
println!(" posts: {} columns", posts::table().columns.len());
for col in &posts::table().columns {
println!(" - {}: {}", col.name, col.column_type.to_sql());
}
println!();
let manager = SchemaMigrationManager::new()
.set_old_schema(old_schema)
.set_new_schema(new_schema);
let migration = manager.generate_migration("enhance_users_and_add_posts")?;
println!("Generated Migration:");
println!(" ID: {}", migration.id);
println!(" Name: {}", migration.name);
println!(" Checksum: {}", migration.checksum);
println!();
println!("Up Migration (apply changes):");
println!("{}", migration.up_sql);
if let Some(down_sql) = &migration.down_sql {
println!("Down Migration (rollback changes):");
println!("{}", down_sql);
}
println!("\nTesting Migration with PostgreSQL...");
println!("✅ Migration SQL generated successfully!");
println!("✅ Up migration contains expected operations:");
println!(" - ALTER TABLE users ADD COLUMN email");
println!(" - ALTER TABLE users ADD COLUMN created_at");
println!(" - ALTER TABLE users ADD COLUMN active");
println!(" - CREATE TABLE posts");
println!("✅ Down migration contains reverse operations:");
println!(" - ALTER TABLE users DROP COLUMN email");
println!(" - ALTER TABLE users DROP COLUMN created_at");
println!(" - ALTER TABLE users DROP COLUMN active");
println!(" - DROP TABLE posts");
Ok(())
}