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
- 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
- Validate your schemas:
migracraft --validate
- Generate your first migration:
migracraft --name "initial_setup" --full
- Make changes to schemas and generate differential migrations:
migracraft --name "add_user_profiles"
- 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
sqlplpgsqlcpythonperltcl
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
- Maintainability: Each module has a single responsibility
- Testability: Smaller, focused modules are easier to test
- Extensibility: Easy to add new features or modify existing ones
- Readability: Clear separation of concerns
- 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:
-
Using the installed command (recommended):
migracraft --help migracraft --validate migracraft --full --name "initial_setup"
-
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
048942685715256398adde41d7c4119d5f9b0e4d2280bb65c0d380f7d763adfe
|
|
| MD5 |
1a42dd5cb714027e06701ff4517d42d5
|
|
| BLAKE2b-256 |
71061b2b00804fa8682e5aeb729bad912405bbaf2583f4f7ff510e44d08fd39c
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8630d72bd61425246d9204196fb1ae2962d7038ee7b3a29829d1f85546fbe44
|
|
| MD5 |
529d0a6995248727f42490b8f8ae89a3
|
|
| BLAKE2b-256 |
5caa103d03a3627cf57a23d4d4640bba9d1f3fa0693f28064027b317d816ec1d
|