ignis-db 0.2.0

A comprehensive PostgreSQL ORM with macro-driven schema management
Documentation
//! Enhanced table! macro example demonstrating all features

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

// Enhanced users table
table! {
    users (id) {
        id -> Int4,
        email -> Varchar,
        name -> Text,
        age -> Int4,
        created_at -> Timestamptz,
        active -> Bool,
        tags -> Array,
    }
}

// Posts table with foreign key reference
table! {
    posts (id) {
        id -> Int4,
        user_id -> Int4,
        title -> Varchar,
        content -> Text,
        published -> Bool,
        created_at -> Timestamptz,
        updated_at -> Timestamptz,
    }
}

// Categories table for posts
table! {
    categories (id) {
        id -> Int4,
        name -> Varchar,
        description -> Text,
        parent_id -> Int4,
        created_at -> Timestamptz,
    }
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    println!("Enhanced Table! Macro Example");
    println!("=============================\n");

    // Test schema support
    println!("Schema Support:");
    let users_table = users::table();
    println!("  users table schema: {:?}", users_table.schema);
    println!("  posts table schema: {:?}", posts::table().schema);
    println!(
        "  categories table schema: {:?}",
        categories::table().schema
    );
    println!();

    // Test column information
    println!("Column Information:");
    for col in &users_table.columns {
        println!("  {}: {:?}", col.name, col.column_type);
    }
    println!();

    // Test advanced column types
    println!("Advanced Column Types:");
    let tags_col = users_table
        .columns
        .iter()
        .find(|c| c.name == "tags")
        .unwrap();
    println!("  tags column type: {:?}", tags_col.column_type);
    println!();

    // Generate DDL to see the enhanced output
    println!("Generated DDL:");
    println!("{}", users_table.generate_ddl());
    println!();

    // Test migration system with enhanced tables
    println!("Testing Migration System with Enhanced Tables...");

    // Create old schema (basic users table)
    let mut old_schema = HashMap::new();
    let basic_users = ignis_db::schema::Table::new("users")
        .add_column(ignis_db::schema::Column::new(
            "id",
            ignis_db::schema::ColumnType::Int4,
        ))
        .add_column(ignis_db::schema::Column::new(
            "name",
            ignis_db::schema::ColumnType::Text,
        ))
        .set_primary_key("id");
    old_schema.insert("users".to_string(), basic_users);

    // New schema (enhanced users table)
    let mut new_schema = HashMap::new();
    new_schema.insert("users".to_string(), users_table);
    new_schema.insert("posts".to_string(), posts::table());
    new_schema.insert("categories".to_string(), categories::table());

    // Generate migration
    let manager = SchemaMigrationManager::new()
        .set_old_schema(old_schema)
        .set_new_schema(new_schema);

    let migration = manager.generate_migration("enhance_users_add_posts_categories")?;

    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 inventory registration
    println!("\nInventory Registration:");
    let tables: Vec<_> = ignis_db::get_registered_tables().collect();
    for table in tables {
        println!("  - {} (schema: {:?})", table.0.name, table.0.schema);
        println!("    DDL: {}", table.0.ddl);
    }

    println!("\n✅ Enhanced table! macro working perfectly!");
    println!("✅ Advanced column types supported");
    println!("✅ Migration system enhanced");

    Ok(())
}