Skip to main content

The missing migration layer for SQLModel. Zero-config, auto-detecting schema migrations.

Project description

🧱 SQLMorph: Migrations & Schema Visualization for SQLModel and SQLAlchemy

SQLMorph — Zero-config migrations, schema inspection, and ER diagram generation for SQLModel and SQLAlchemy.
No boilerplate. No manual scripts. Just magic. ⚡

PyPI version License


✨ Why SQLMorph?

Feature Alembic SQLMorph
Auto-detects model changes
Zero configuration
Schema health check
ER diagram generator
Live migration watch
Works with SQLAlchemy + SQLModel
Interactive shell
JSON + Mermaid export

🚀 Features

SQLMorph is more than just a migration tool — it’s a complete database companion for your ORM projects.

  • 🚀 Zero-Config Migrations: Automatically detects schema changes and generates migration scripts.
  • 🩺 Schema Health Checks: Detects drift between your models and the database.
  • 📝 Safe Planning: Preview SQL changes before migrating.
  • 🔍 Advanced Inspection: View models and relationships as tables or JSON.
  • 🎨 Auto Diagrams: Generate Mermaid.js ERDs directly from your models.
  • 🌱 Data Seeding: Load seed data via JSON or YAML.
  • 👀 Live Watch Mode: Automatically detect model changes.
  • 🔧 Reverse Engineering: Generate ORM classes from existing DBs.
  • 🗄️ Interactive Shell: Native database shell integration.
  • 🧠 SQLAlchemy + SQLModel: Full compatibility with both ORMs.

📦 Installation

pip install -e .
# For shell command support on Debian/Ubuntu
sudo apt update && sudo apt install sqlite3

⚡ Quick Start

Define your models

# models.py
from typing import Optional
from sqlmorph import SQLModel, Field

class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str

Create your first migration

sqlmorph makemigration "create_user_table" --models models.py
sqlmorph migrate

Make changes and apply them

# models.py
class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    email: str  # <-- new field
sqlmorph plan
sqlmorph makemigration "add_email_to_user"
sqlmorph migrate

🧠 Works with SQLAlchemy Too

from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import declarative_base, relationship

Base = declarative_base()

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    name = Column(String)
    posts = relationship("Post", back_populates="user")

class Post(Base):
    __tablename__ = "posts"
    id = Column(Integer, primary_key=True)
    title = Column(String)
    user_id = Column(Integer, ForeignKey("users.id"))
    user = relationship("User", back_populates="posts")

Both ORM types work seamlessly.


🧰 Command Reference

Command Description
makemigration <name> Generate migration script for detected schema changes
migrate Apply pending migrations
rollback Undo the last migration
plan Preview upcoming SQL operations
doctor Detect drift between DB and models
inspect Show model tables and relationships
diagram Generate Mermaid.js ER diagrams
snapshot Save current schema state
explain <query> Analyze query performance
seed --file <path> Load seed data from JSON/YAML
watch Monitor models for changes
diffdb --url <db_url> Reverse engineer SQLModel from DB
shell Open database shell
help Show command reference

🧪 CI/CD Example

- name: Validate Schema
  run: |
    pip install sqlmorph
    sqlmorph doctor

🧩 How It Works

SQLMorph introspects your ORM metadata (SQLModel.metadata or Base.metadata), snapshots its structure, and generates safe migration plans.
All migrations are explicit, auditable, and reversible.


🛠️ Roadmap

  • Plugin system for lifecycle hooks
  • Cloud schema diff dashboard
  • FastAPI integration helpers
  • Visual web UI for migration planning

🧑‍💻 Contributing

Pull requests are welcome! Please run tests before submitting:

pytest

📜 License

MIT License © 2025 SQLMorph Contributors

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

sqlmorph-1.0.0.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

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

sqlmorph-1.0.0-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlmorph-1.0.0.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.3 Linux/5.10.16.3-microsoft-standard-WSL2

File hashes

Hashes for sqlmorph-1.0.0.tar.gz
Algorithm Hash digest
SHA256 26cc7132c4a3b9ee0b148b923fb1073f0fc52a96676d45af05bc91d7b2934d45
MD5 a1fa94ea0cb42f5282c7a832a7dc1821
BLAKE2b-256 c64873408a94299b967c70fc06866e9e32780358b414d6b9b69d75a001c2eed6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqlmorph-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 25.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.3 Linux/5.10.16.3-microsoft-standard-WSL2

File hashes

Hashes for sqlmorph-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2dfb6edeb53b9b5139294028acc7246cee99c6865a40c2b8311453f5f40a9225
MD5 9c7298b60f8f340284457c77919aefc9
BLAKE2b-256 d7c4468b751a9cd388f1f70fd708e35267fd696b6718e1c64e26f274a8eb9c9c

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