|
| 1 | +# Alembic Migration Guide for `mcpgateway` |
| 2 | + |
| 3 | +> Creating, applying, and managing schema migrations with Alembic. |
| 4 | +
|
| 5 | +--- |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | +1. [Why Alembic?](#why-alembic) |
| 10 | +2. [Prerequisites](#prerequisites) |
| 11 | +3. [Directory Layout](#directory-layout) |
| 12 | +4. [Everyday Workflow](#everyday-workflow) |
| 13 | +5. [Helpful Make Targets](#helpful-make-targets) |
| 14 | +6. [Troubleshooting](#troubleshooting) |
| 15 | +7. [Further Reading](#further-reading) |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Why Alembic? |
| 20 | + |
| 21 | +- **Versioned DDL** — Revisions are timestamped, diff-able, and reversible. |
| 22 | +- **Autogeneration** — Detects model vs. DB drift and writes `op.create_table`, `op.add_column`, etc. |
| 23 | +- **Multi-DB Support** — Works with SQLite, PostgreSQL, MySQL—anything SQLAlchemy supports. |
| 24 | +- **Zero Runtime Cost** — Only runs when you call it (dev, CI, deploy). |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## Prerequisites |
| 29 | + |
| 30 | +```bash |
| 31 | +# Activate your virtual environment first |
| 32 | +pip install --upgrade alembic |
| 33 | +``` |
| 34 | + |
| 35 | +You do not need to set up `alembic.ini`, `env.py`, or metadata wiring - they're already configured. |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## Directory Layout |
| 40 | + |
| 41 | +``` |
| 42 | +alembic.ini |
| 43 | +alembic/ |
| 44 | +├── env.py |
| 45 | +├── script.py.mako |
| 46 | +└── versions/ |
| 47 | + ├── 20250626235501_initial_schema.py |
| 48 | + └── ... |
| 49 | +``` |
| 50 | + |
| 51 | +* `alembic.ini`: Configuration file |
| 52 | +* `env.py`: Connects Alembic to your models and DB settings |
| 53 | +* `script.py.mako`: Template for new revisions (keep this!) |
| 54 | +* `versions/`: Contains all migration scripts |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## Everyday Workflow |
| 59 | + |
| 60 | +> **1 Edit → 2 Revision → 3 Upgrade** |
| 61 | +
|
| 62 | +| Step | What you do | |
| 63 | +| ------------------------ | ----------------------------------------------------------------------------- | |
| 64 | +| **1. Change models** | Modify SQLAlchemy models in `mcpgateway.db` or its submodules. | |
| 65 | +| **2. Generate revision** | Run: `MSG="add users table"` then `alembic revision --autogenerate -m "$MSG"` | |
| 66 | +| **3. Review** | Open the new file in `alembic/versions/`. Verify the operations are correct. | |
| 67 | +| **4. Upgrade DB** | Run: `alembic upgrade head` | |
| 68 | +| **5. Commit** | Run: `git add alembic/versions/*.py` | |
| 69 | + |
| 70 | +### Other Common Commands |
| 71 | + |
| 72 | +```bash |
| 73 | +alembic current # Show current DB revision |
| 74 | +alembic history --verbose # Show all migrations and their order |
| 75 | +alembic downgrade -1 # Roll back one revision |
| 76 | +alembic downgrade <rev> # Roll back to a specific revision hash |
| 77 | +``` |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## ✅ Make Targets: Alembic Migration Commands |
| 82 | + |
| 83 | +These targets help you manage database schema migrations using Alembic. |
| 84 | + |
| 85 | +> You must have a valid `alembic/` setup and a working SQLAlchemy model base (`Base.metadata`). |
| 86 | +
|
| 87 | +--- |
| 88 | + |
| 89 | +### 💡 List all available targets (with help) |
| 90 | + |
| 91 | +```bash |
| 92 | +make help |
| 93 | +``` |
| 94 | + |
| 95 | +This will include the Alembic section: |
| 96 | + |
| 97 | +``` |
| 98 | +# 🛢️ Alembic tasks |
| 99 | +db-new Autogenerate revision (MSG="title") |
| 100 | +db-up Upgrade DB to head |
| 101 | +db-down Downgrade one step (REV=-1 or hash) |
| 102 | +db-current Show current DB revision |
| 103 | +db-history List the migration graph |
| 104 | +``` |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +### 🔨 Commands |
| 109 | + |
| 110 | +| Command | Description | |
| 111 | +| -------------------------- | ------------------------------------------------------ | |
| 112 | +| `make db-new MSG="..."` | Generate a new migration based on model changes. | |
| 113 | +| `make db-up` | Apply all unapplied migrations. | |
| 114 | +| `make db-down` | Roll back the latest migration (`REV=-1` by default). | |
| 115 | +| `make db-down REV=abc1234` | Roll back to a specific revision by hash. | |
| 116 | +| `make db-current` | Print the current revision ID applied to the database. | |
| 117 | +| `make db-history` | Show the full migration history and graph. | |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +### 📌 Examples |
| 122 | + |
| 123 | +```bash |
| 124 | +# Create a new migration with a custom message |
| 125 | +make db-new MSG="add users table" |
| 126 | + |
| 127 | +# Apply it to the database |
| 128 | +make db-up |
| 129 | + |
| 130 | +# Downgrade the last migration |
| 131 | +make db-down |
| 132 | + |
| 133 | +# Downgrade to a specific revision |
| 134 | +make db-down REV=cf1283d7fa92 |
| 135 | + |
| 136 | +# Show the current applied revision |
| 137 | +make db-current |
| 138 | + |
| 139 | +# Show all migration history |
| 140 | +make db-history |
| 141 | +``` |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +### 🛑 Notes |
| 146 | + |
| 147 | +* You must **edit models first** before `make db-new` generates anything useful. |
| 148 | +* Always **review generated migration files** before committing. |
| 149 | +* Don't forget to run `make db-up` on CI or deploy if using migrations to manage schema. |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +## Troubleshooting |
| 154 | + |
| 155 | +| Symptom | Cause / Fix | |
| 156 | +| ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 157 | +| **Empty migration (`pass`)** | Alembic couldn't detect models. Make sure all model classes are imported before `Base.metadata` is used (already handled in your `env.py`). | |
| 158 | +| **`Can't locate revision ...`** | You deleted or renamed a revision file that the DB is pointing to. Either restore it or run `alembic stamp base` and recreate the revision. | |
| 159 | +| **`script.py.mako` missing** | This file is required. Run `alembic init alembic` in a temp folder and copy the missing template into your project. | |
| 160 | +| **SQLite foreign key limitations** | SQLite doesn't allow dropping constraints. Use `create table → copy → drop` flow manually, or plan around it. | |
| 161 | +| **DB not updating** | Did you forget to run `alembic upgrade head`? Check with `alembic current`. | |
| 162 | +| **Wrong DB URL or config errors** | Confirm `settings.database_url` is valid. Check `env.py` and your `.env`/config settings. Alembic ignores `alembic.ini` for URLs in your setup. | |
| 163 | +| **Model changes not detected** | Alembic only picks up declarative models in `Base.metadata`. Ensure all models are imported and not behind `if TYPE_CHECKING:` or other lazy imports. | |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +## Further Reading |
| 168 | + |
| 169 | +* Official docs: [https://alembic.sqlalchemy.org](https://alembic.sqlalchemy.org) |
| 170 | +* Autogenerate docs: [https://alembic.sqlalchemy.org/en/latest/autogenerate.html](https://alembic.sqlalchemy.org/en/latest/autogenerate.html) |
| 171 | + |
| 172 | +--- |
0 commit comments