ignis-db 0.2.0

A comprehensive PostgreSQL ORM with macro-driven schema management
Documentation
# Ignis-DB

A PostgreSQL ORM and database management tool for Rust.

## Overview

Ignis-DB provides:

- Database schema introspection and migration management
- Rust code generation from existing databases
- Schema synchronization between Rust models and database state
- Command-line interface for database operations

## Features

### Core Functionality

- **Database Introspection**: Analyze existing PostgreSQL schemas
- **Migration System**: Generate and apply database migrations
- **Code Generation**: Create Rust code from database schemas
- **Schema Sync**: Compare Rust models with database state
- **Macro System**: Comprehensive declarative schema definition macros

### Supported PostgreSQL Objects

- Tables and columns
- Views (standard and materialized)
- Composite types
- Functions and procedures
- Triggers
- Indexes and constraints
- Events and scheduled jobs
- Roles and permissions
- Grants and security

### Macro System

Ignis-DB provides a comprehensive set of declarative macros for defining database schemas:

#### Schema Definition Macros

- **`table!`** - Define tables with columns, constraints, and indexes
- **`view!`** - Create standard and materialized views
- **`composite!`** - Define PostgreSQL composite types
- **`index!`** - Create database indexes with various types and options

#### Database Object Macros

- **`trigger!`** - Define database triggers with timing and events
- **`procedure!`** - Create stored procedures with parameters
- **`function!`** - Define database functions
- **`event!`** - Schedule database events and jobs

#### Security and Access Control

- **`role!`** - Define database roles and users
- **`grant!`** - Manage permissions and access control

#### ORM Integration Macros

- **`impl_ignis_model!`** - Implement IgnisModel trait for structs
- **`impl_insertable!`** - Add insert operations to models
- **`impl_updatable!`** - Add update operations to models
- **`impl_deletable!`** - Add delete operations to models

### CLI Commands

- `init`: Initialize project from existing database
- `introspect`: Analyze database schema
- `migrate`: Manage database migrations
- `scaffold`: Generate code for specific tables
- `sync`: Synchronize Rust models with database

## Installation

```bash
cargo install --path .
```

## Usage

### Initialize from Existing Database

```bash
ignis-cli init --database-url "postgresql://user:pass@localhost/db" --output ./my_project
```

### Introspect Database Schema

```bash
ignis-cli introspect --database-url "postgresql://user:pass@localhost/db"
```

### Generate Migrations

```bash
ignis-cli sync --database-url "postgresql://user:pass@localhost/db" --src ./src/models
```

## Development

### Prerequisites

- Rust 1.70+
- PostgreSQL database
- Docker (for testing)

### Setup

```bash
git clone <repository>
cd ignis-db
cargo build
```

### Testing

```bash
# Start test database
docker-compose up -d

# Run tests
cargo test
```

## Project Structure

```
src/
├── bin/           # CLI application
├── schema/        # Database schema definitions
├── migrate/       # Migration system
├── introspection/ # Database introspection
├── codegen/       # Code generation
├── code_parsing/  # Rust code parsing
└── macros/        # Declarative schema definition macros
```

## Examples

See the `examples/` directory for usage examples covering:

- Basic table operations
- View management
- Migration workflows
- Schema synchronization

### Macro Usage Examples

```rust
use ignis_db::*;

// Define a table with columns and constraints
table! {
    users {
        id -> Int4,
        username -> Varchar,
        email -> Varchar,
        created_at -> Timestamptz,

        // Constraints
        primary_key(id),
        unique(username),
        unique(email),
        not_null(username),
        not_null(email),
    }
}

// Create a view
view! {
    active_users {
        SELECT id, username, email
        FROM users
        WHERE created_at > NOW() - INTERVAL '30 days'
    }
}

// Define a composite type
composite! {
    address {
        street -> Varchar,
        city -> Varchar,
        zip_code -> Varchar,
        country -> Varchar,
    }
}

// Create a trigger
trigger! {
    users_audit ON users
    AFTER INSERT OR UPDATE OR DELETE
    FOR EACH ROW
    EXECUTE FUNCTION audit_users_changes()
}

// Define a stored procedure
procedure! {
    create_user(username: Varchar, email: Varchar) -> Int4 {
        INSERT INTO users (username, email, created_at)
        VALUES ($1, $2, NOW())
        RETURNING id
    }
}
```

## License

MIT OR Apache-2.0