ignis-db 0.2.0

A comprehensive PostgreSQL ORM with macro-driven schema management
Documentation
//! Example demonstrating the migration system with table! macro

use ignis_db::{migrate::SchemaMigrationManager, table, IgnisObject};
use std::collections::HashMap;

// Initial schema version
table! {
    users_v1 (id) {
        id -> Int4,
        name -> Text,
    }
}

// Updated schema version with new columns
table! {
    users_v2 (id) {
        id -> Int4,
        name -> Text,
        email -> Varchar,
        created_at -> Timestamptz,
        active -> Bool,
    }
}

// Another table to demonstrate multi-table migrations
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");

    // Create old schema state
    let mut old_schema = HashMap::new();
    old_schema.insert("users".to_string(), users_v1::table());

    // Create new schema state
    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!();

    // Generate migration
    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);
    }

    // Test the migration by applying it to a test database
    println!("\nTesting Migration with PostgreSQL...");

    // This would normally connect to a real database
    // For this example, we'll just verify the SQL syntax
    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(())
}