Skip to main content

Pure Hylang database migration tool for SQLite - schema versioning and management in Lisp

Project description

Pure Hylang Migration Tool

A 100% pure Hylang v1.1.0 schema migration tool for SQLite with SQLObject integration. No Python code - everything is written in idiomatic Hylang using modern features like hyrule macros.

Features

  • โœจ Pure Hylang - Entire codebase in Hylang v1.1.0, no Python files
  • ๐ŸŽฏ Modern Hylang idioms - Uses hyrule macros, let bindings, and functional patterns
  • ๐Ÿ“ฆ Pip installable - Works as a standard Python package despite being pure Hylang
  • ๐Ÿ”„ Full migration lifecycle - Create, apply, rollback, validate migrations
  • ๐Ÿ—ƒ๏ธ SQLObject integration - Seamless ORM support
  • ๐ŸŽจ Colored CLI output - Beautiful terminal interface using colorama
  • ๐Ÿ›ก๏ธ Transaction safety - All migrations run in transactions
  • โœ… Validation & checksums - Ensure migration integrity
  • ๐Ÿ” Dry-run mode - Preview changes before applying

Installation

Using UV (Recommended)

UV is a fast Python package manager that provides excellent environment handling:

# Clone the repository
git clone https://github.com/yourusername/hylang-migrations.git
cd hylang-migrations

# Create virtual environment and install
uv venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
uv pip install -e .

# The tool is now available
hylang-migrate --help

Using pip

# Clone the repository
git clone https://github.com/yourusername/hylang-migrations.git
cd hylang-migrations

# Install in development mode
pip install -e .

# Or build and install
pip install build
python -m build
pip install dist/hylang_migrations-*.whl

From PyPI (when published)

pip install hylang-migrations

Quick Start

1. Initialize Your Project

cd your-project
hylang-migrate init

This creates:

  • migrations/ directory for migration files
  • .migrations configuration file (in Hylang format)
  • Initial project structure

2. Create a Migration

hylang-migrate create create_users_table

This generates a timestamped Hylang migration file:

;;; Migration: create_users_table
;;; Version: 20240101120000

(defclass CreateUsersTable []
  (defn up [self connection]
    "Apply migration"
    (connection.execute
      "CREATE TABLE users (
         id INTEGER PRIMARY KEY,
         username VARCHAR(255) UNIQUE NOT NULL,
         email VARCHAR(255) UNIQUE NOT NULL,
         created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
       )"))
  
  (defn down [self connection]
    "Rollback migration"
    (connection.execute "DROP TABLE IF EXISTS users")))

(setv migration (CreateUsersTable))

3. Run Migrations

# Apply all pending migrations
hylang-migrate migrate

# Preview changes (dry run)
hylang-migrate migrate --dry-run

# Migrate to specific version
hylang-migrate migrate --target 20240101120000

4. Check Status

hylang-migrate status

Output:

๐Ÿ“Š Migration Status
  Database: database.db
  Migrations: migrations/

โœ… Applied Migrations:
Version          Name                    Applied At
20240101120000   create_users_table     2024-01-01 12:00:00

โณ Pending Migrations:
Version          Name
20240101130000   add_user_profiles

5. Rollback

# Rollback last migration
hylang-migrate rollback

# Rollback multiple migrations
hylang-migrate rollback --steps 3

# Rollback to specific version
hylang-migrate rollback --to 20240101120000

Command Reference

Global Options

  • --config PATH - Configuration file path (default: .migrations)
  • --db PATH - Database file path (default: database.db)
  • --migrations-dir PATH - Migrations directory (default: migrations)
  • --verbose - Verbose output

Commands

Command Description Options
init Initialize migration system -
create NAME Create new migration -
migrate Run pending migrations --target VERSION, --dry-run
rollback Rollback migrations --steps N, --to VERSION, --dry-run
status Show migration status -
list List all migrations --pending, --applied
show VERSION Show migration details -
validate Validate migration files -

Configuration

Hylang Format (.migrations)

{
  :database {
    :path "database.db"
    :type "sqlite"
  }
  :migrations {
    :directory "migrations"
    :table-name "migration_history"
    :auto-transaction true
    :verify-checksums true
  }
  :sqlobject {
    :debug false
    :cache true
    :lazy-update true
  }
  :logging {
    :level "INFO"
    :file "migrations.log"
  }
}

Environment Variables

export DB_PATH=production.db
export MIGRATIONS_DIR=db/migrations
export SQLOBJECT_DEBUG=true

Writing Migrations

Basic Migration Structure

(require hyrule [-> ->> as->])
(import sqlite3)

(defclass MigrationName []
  (defn __init__ [self]
    (setv self.version "20240101120000")
    (setv self.name "migration_name"))
  
  (defn up [self connection]
    "Apply migration"
    ;; Your forward migration logic
    )
  
  (defn down [self connection]
    "Rollback migration"
    ;; Your rollback logic
    )
  
  (defn validate [self connection]
    "Optional validation"
    True)
  
  (defn get-checksum [self]
    "Calculate checksum"
    (import hashlib)
    (-> (hashlib.sha256)
        (.update (.encode (+ self.version self.name) "utf-8"))
        (.hexdigest))))

(setv migration (MigrationName))

Using SQLObject Models

(import sqlobject [SQLObject StringCol IntCol DateTimeCol BoolCol])
(import datetime [datetime])

(defclass User [SQLObject]
  (setv _table "users")
  (setv username (StringCol :unique True :notNone True))
  (setv email (StringCol :unique True :notNone True))
  (setv is-active (BoolCol :default True))
  (setv created-at (DateTimeCol :default datetime.now)))

Pure Hylang Implementation

This tool is written entirely in Hylang v1.1.0 with modern idioms:

Key Files (all .hy):

  • cli.hy - Command-line interface with argparse and colorama
  • migrations.hy - Core migration engine
  • config.hy - Configuration management
  • utils.hy - Utility functions
  • templates.hy - Migration and model generators

Hylang v1.1.0 Features Used:

  • require hyrule for modern macros
  • let bindings for local scope
  • lfor list comprehensions
  • #** for keyword arguments
  • f-strings for formatting
  • Pattern matching with cond

Development

Setup Development Environment

# Clone repo
git clone https://github.com/yourusername/hylang-migrations.git
cd hylang-migrations

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install in development mode
pip install -e ".[dev]"

Running Tests

# Run all tests
pytest tests/

# Run specific test
pytest tests/test_migrations.hy

# With coverage
pytest --cov=hylang_migrations tests/

Building Package

# Build distribution
python -m build

# Upload to TestPyPI
python -m twine upload --repository testpypi dist/*

# Upload to PyPI
python -m twine upload dist/*

Project Structure

hylang-migrations/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ hylang_migrations/
โ”‚       โ”œโ”€โ”€ __init__.hy          # Package initialization
โ”‚       โ”œโ”€โ”€ __main__.hy          # Module entry point
โ”‚       โ”œโ”€โ”€ cli.hy               # CLI implementation
โ”‚       โ”œโ”€โ”€ migrations.hy        # Core engine
โ”‚       โ”œโ”€โ”€ config.hy            # Configuration
โ”‚       โ”œโ”€โ”€ utils.hy             # Utilities
โ”‚       โ””โ”€โ”€ templates.hy         # Generators
โ”œโ”€โ”€ migrations/                   # User migrations directory
โ”‚   โ””โ”€โ”€ *.hy                     # Migration files
โ”œโ”€โ”€ tests/                       # Test suite
โ”‚   โ””โ”€โ”€ test_*.hy               # Test files
โ”œโ”€โ”€ pyproject.toml              # Package configuration
โ”œโ”€โ”€ setup.hy                    # Hylang setup script
โ”œโ”€โ”€ README.md                   # This file
โ””โ”€โ”€ LICENSE                     # MIT License

Why Pure Hylang?

This project demonstrates that complex tools can be written entirely in Hylang without any Python code:

  1. Language Purity - Shows Hylang's completeness as a language
  2. Lisp Power - Leverages macros and functional programming
  3. Python Ecosystem - Still integrates seamlessly with pip/PyPI
  4. Modern Hylang - Uses latest v1.1.0 features and idioms
  5. Real-World Tool - Not just a toy, but a production-ready tool

Claude Code Integration

This package includes Claude Code subagents to help you work with Hylang migrations more effectively!

Installing Claude Agents

# Install the Hylang migrations assistant
hylang-migrate install-claude-agent

# The agents are now available in Claude Code!

Available Agents

  1. hylang-migrate-assistant - Expert help with:

    • Creating and managing migrations
    • Debugging migration issues
    • Schema design best practices
    • Hylang v1.1.0 migration syntax
  2. hyrule-expert - Hylang/Hyrule language expert for:

    • Conditionals (if, when, cond)
    • Loops and comprehensions
    • String formatting (.format vs f-strings)
    • Hyrule macros and utilities

To use these agents in Claude Code, type /agents and select the appropriate assistant.

Contributing

Contributions must be in pure Hylang! We welcome:

  • Bug fixes
  • New features
  • Documentation improvements
  • Test coverage
  • Performance optimizations

Please ensure all code follows Hylang v1.1.0 idioms and includes tests.

License

MIT License - See LICENSE file for details.

Acknowledgments

  • Hylang community for the amazing Lisp-on-Python language
  • SQLObject for the ORM functionality
  • All contributors to the Python ecosystem

Remember: This entire tool is written in pure Hylang - no Python files! ๐ŸŽ‰

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

hylang_migrations-0.1.0.tar.gz (38.5 kB view details)

Uploaded Source

Built Distribution

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

hylang_migrations-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file hylang_migrations-0.1.0.tar.gz.

File metadata

  • Download URL: hylang_migrations-0.1.0.tar.gz
  • Upload date:
  • Size: 38.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for hylang_migrations-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fa7f402a40d32339f9c08ecace6e9604cb9dfc2db6c2ad42204c08ca814d1e9a
MD5 b351af794bbbf033b3f3fc222e90d628
BLAKE2b-256 5b946e4ce7f268cafc44990def486801a1d07ce8e5f3c57fac705d0625c41669

See more details on using hashes here.

File details

Details for the file hylang_migrations-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hylang_migrations-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e546520a3f3adadea0c4600049b1c9d2629233066616f667f86d843c273e045c
MD5 c47d7954045d7d190f87c7a8f362a23a
BLAKE2b-256 640053717d614f8ed14c8e51728d49e3ba27b0cda1535a0c31a93aeb6b62417e

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