use ignis_db::{migrate::SchemaMigrationManager, table, IgnisObject};
use std::collections::HashMap;
table! {
users (id) {
id -> Int4,
email -> Varchar,
name -> Text,
age -> Int4,
created_at -> Timestamptz,
active -> Bool,
tags -> Array,
}
}
table! {
posts (id) {
id -> Int4,
user_id -> Int4,
title -> Varchar,
content -> Text,
published -> Bool,
created_at -> Timestamptz,
updated_at -> Timestamptz,
}
}
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");
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!();
println!("Column Information:");
for col in &users_table.columns {
println!(" {}: {:?}", col.name, col.column_type);
}
println!();
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!();
println!("Generated DDL:");
println!("{}", users_table.generate_ddl());
println!();
println!("Testing Migration System with Enhanced Tables...");
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);
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());
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);
}
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(())
}