Skip to main content

Craft perfect PostgreSQL migrations from YAML schema definitions with multi-language entity generation

Project description

MigraCraft ๐Ÿ› ๏ธ

Craft perfect PostgreSQL migrations with precision and artistry.

A powerful and flexible PostgreSQL schema migration tool that generates SQL migrations from YAML schema definitions and creates entity classes for multiple programming languages.

โœจ Features

  • ๐ŸŽจ YAML Schema Definitions: Define your database schema in elegant, readable YAML files
  • โšก Differential Migrations: Automatically detect changes and generate incremental migrations
  • ๐Ÿš€ Full Schema Migrations: Generate complete schema migrations for initial setup
  • โ†ฉ๏ธ Rollback Support: Create rollback migrations to undo changes safely
  • โœ… Schema Validation: Comprehensive validation of YAML schema definitions
  • ๐Ÿ”— Foreign Key Support: Full support for foreign key constraints and relationships
  • ๐Ÿ“Š Index Management: Create and manage database indexes efficiently
  • โš™๏ธ PostgreSQL Functions: Support for stored procedures and functions
  • ๐Ÿ—๏ธ Entity Class Generation: Generate entity classes in 7+ programming languages
  • ๐Ÿงฉ Modular Architecture: Clean, maintainable codebase split into logical modules

๐Ÿš€ Quick Start

Installation

# Install MigraCraft
pip install migracraft

# Or install from source
git clone https://github.com/yourusername/migracraft.git
cd migracraft
pip install -e .

# Or using requirements.txt
pip install -r requirements.txt

Quick Commands

# Check version and see beautiful banner
migracraft --version
migracraft --banner

# Validate schemas
migracraft --validate

# Create first migration
migracraft --full --name "initial_setup"

# Generate TypeScript entities
migracraft --generate-entities typescript

Quick Start

  1. Create schema files in the schemas/ directory:
# schemas/users.yaml
tables:
  users:
    columns:
      id:
        type: SERIAL
        primary_key: true
      username:
        type: VARCHAR(50)
        not_null: true
        unique: true
      email:
        type: VARCHAR(255)
        not_null: true
        unique: true
      created_at:
        type: TIMESTAMP
        not_null: true
        default: CURRENT_TIMESTAMP
    indexes:
      - columns: [username]
        name: idx_users_username
        unique: true
  1. Validate your schemas:
migracraft --validate
  1. Generate your first migration:
migracraft --name "initial_setup" --full
  1. Make changes to schemas and generate differential migrations:
migracraft --name "add_user_profiles"
  1. Create rollback migrations when needed:
migracraft --rollback --name "undo_profiles"

Project Structure

migracraft-project/
โ”œโ”€โ”€ migrate.py                 # CLI entry point (also available as 'migracraft' command)
โ”œโ”€โ”€ migracraft/                # Core package
โ”‚   โ”œโ”€โ”€ __init__.py           # Package initialization
โ”‚   โ”œโ”€โ”€ config.py             # Configuration constants
โ”‚   โ”œโ”€โ”€ exceptions.py         # Custom exceptions
โ”‚   โ”œโ”€โ”€ validator.py          # Schema validation
โ”‚   โ”œโ”€โ”€ sql_generator.py      # SQL generation
โ”‚   โ”œโ”€โ”€ entity_generator.py   # Multi-language entity generation
โ”‚   โ”œโ”€โ”€ migration_manager.py  # Migration management
โ”‚   โ”œโ”€โ”€ migracraft.py         # Package entry point
โ”‚   โ””โ”€โ”€ main.py              # Main tool class
โ”œโ”€โ”€ schemas/                   # YAML schema definitions
โ”œโ”€โ”€ migrations/               # Generated SQL migrations
โ”œโ”€โ”€ entities/                 # Generated entity classes
โ”œโ”€โ”€ tests/                    # Test files
โ”œโ”€โ”€ setup.py                  # Package configuration
โ”œโ”€โ”€ requirements.txt          # Dependencies
โ”œโ”€โ”€ version.py               # Version management
โ”œโ”€โ”€ dev.py                   # Development helper
โ””โ”€โ”€ README.md

Usage

Command Line Options

migracraft [OPTIONS]

Options:
  --version                 Show version information
  --banner                  Display MigraCraft banner
  --schemas-dir DIR         Directory containing YAML schema files (default: schemas)
  --migrations-dir DIR      Directory to store migration files (default: migrations)
  --name NAME              Optional name for the migration
  --full                   Generate full migration instead of differential
  --validate               Only validate schemas without creating migrations
  --rollback               Create a rollback migration to undo the last migration
  --generate-entities LANG Generate entity classes for specified language
  --entities-dir DIR       Directory to store generated entity files
  -h, --help               Show help message

Schema Definition Format

Tables

tables:
  table_name:
    columns:
      column_name:
        type: DATA_TYPE
        primary_key: true/false    # Optional
        not_null: true/false       # Optional
        unique: true/false         # Optional
        default: "value"           # Optional
    indexes:                       # Optional
      - columns: [col1, col2]
        name: index_name           # Optional
        unique: true/false         # Optional
    foreign_keys:                  # Optional
      - columns: [local_column]
        references_table: other_table
        references_columns: [other_column]
        name: fk_name              # Optional
        on_delete: CASCADE         # Optional
        on_update: CASCADE         # Optional

Functions

functions:
  function_name:
    parameters:                    # Optional
      param_name: DATA_TYPE
    returns: RETURN_TYPE           # Optional, defaults to 'void'
    language: plpgsql             # Optional, defaults to 'sql'
    body: |
      -- SQL function body
      BEGIN
        -- Function logic here
      END;

Supported Data Types

  • Numeric: SERIAL, BIGSERIAL, INTEGER, BIGINT, DECIMAL, NUMERIC, REAL, DOUBLE PRECISION
  • Text: VARCHAR, CHAR, TEXT
  • Date/Time: TIMESTAMP, TIMESTAMPTZ, DATE, TIME, INTERVAL
  • Boolean: BOOLEAN, BOOL
  • Binary: BYTEA
  • Network: INET, CIDR, MACADDR
  • JSON: JSON, JSONB
  • And many more PostgreSQL types...

Supported Function Languages

  • sql
  • plpgsql
  • c
  • python
  • perl
  • tcl

Entity Generation

Generate entity classes from your schema definitions:

# Generate TypeScript interfaces and classes
migracraft --generate-entities typescript

# Generate Python dataclasses
migracraft --generate-entities python

# Generate Dart classes with JSON serialization
migracraft --generate-entities dart

# Generate Java POJOs with getters/setters
migracraft --generate-entities java

# Generate C++ classes with getters/setters
migracraft --generate-entities cpp

# Generate C# classes with properties
migracraft --generate-entities csharp

# Generate Go structs with JSON tags
migracraft --generate-entities go

# Specify custom output directory
migracraft --generate-entities typescript --entities-dir src/models

Supported Languages for Entity Generation

  • TypeScript: Generates interfaces and classes with proper typing
  • Python: Generates dataclasses with type hints
  • Dart: Generates classes with JSON serialization methods
  • Java: Generates POJOs with getters/setters and proper imports
  • C++: Generates header files with getters/setters
  • C#: Generates classes with properties and proper attributes
  • Go: Generates structs with JSON tags for serialization

Architecture

The tool is built with a modular architecture for maintainability:

  • Config: Centralized configuration and constants
  • Validator: Schema validation logic
  • SQL Generator: SQL generation for tables, functions, and modifications
  • Migration Manager: Migration file management and state tracking
  • Main Tool: High-level orchestration and user interface

Benefits of Refactoring

  1. Maintainability: Each module has a single responsibility
  2. Testability: Smaller, focused modules are easier to test
  3. Extensibility: Easy to add new features or modify existing ones
  4. Readability: Clear separation of concerns
  5. Reusability: Components can be used independently

Examples

See the schemas/ directory for complete examples including:

  • User management system
  • Product catalog
  • Order processing
  • System auditing and configuration

Running MigraCraft

You can run MigraCraft in two ways:

  1. Using the installed command (recommended):

    migracraft --help
    migracraft --validate
    migracraft --full --name "initial_setup"
    
  2. Using the Python script directly:

    python migrate.py --help
    python migrate.py --validate
    python migrate.py --full --name "initial_setup"
    

Both methods provide the same functionality. The migracraft command is available after installing the package with pip install -e .

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

migracraft-1.0.0.tar.gz (37.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

migracraft-1.0.0-py3-none-any.whl (38.6 kB view details)

Uploaded Python 3

File details

Details for the file migracraft-1.0.0.tar.gz.

File metadata

  • Download URL: migracraft-1.0.0.tar.gz
  • Upload date:
  • Size: 37.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for migracraft-1.0.0.tar.gz
Algorithm Hash digest
SHA256 048942685715256398adde41d7c4119d5f9b0e4d2280bb65c0d380f7d763adfe
MD5 1a42dd5cb714027e06701ff4517d42d5
BLAKE2b-256 71061b2b00804fa8682e5aeb729bad912405bbaf2583f4f7ff510e44d08fd39c

See more details on using hashes here.

File details

Details for the file migracraft-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: migracraft-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 38.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for migracraft-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f8630d72bd61425246d9204196fb1ae2962d7038ee7b3a29829d1f85546fbe44
MD5 529d0a6995248727f42490b8f8ae89a3
BLAKE2b-256 5caa103d03a3627cf57a23d4d4640bba9d1f3fa0693f28064027b317d816ec1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page