|
| 1 | +# Database Migrations with Alembic |
| 2 | + |
| 3 | +Mitsuki provides optional, pre-configured support for database migrations using [Alembic](https://alembic.sqlalchemy.org/), the standard migration tool for SQLAlchemy. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Overview](#overview) |
| 8 | +- [Getting Started](#getting-started) |
| 9 | +- [Migration Workflow](#migration-workflow) |
| 10 | +- [How It Works](#how-it-works) |
| 11 | +- [Manual Setup](#manual-setup) |
| 12 | + |
| 13 | +## Overview |
| 14 | + |
| 15 | +When you enable Alembic support, Mitsuki's CLI tool automatically generates the necessary configuration files, allowing you to use standard Alembic commands to manage your database schema. |
| 16 | + |
| 17 | +**What you get:** |
| 18 | +- **Automatic Setup**: `mitsuki init` can create and configure Alembic for you. |
| 19 | +- **Standard Workflow**: Use familiar commands like `alembic revision`, `alembic upgrade`, and `alembic downgrade`. |
| 20 | +- **Pre-configured Environment**: The generated `env.py` is already set up to work with Mitsuki's configuration system and entity discovery. |
| 21 | + |
| 22 | +## Getting Started |
| 23 | + |
| 24 | +The easiest way to start using Alembic is to enable it when creating a new project with the `mitsuki init` command. |
| 25 | + |
| 26 | +1. **Run `mitsuki init`** |
| 27 | + |
| 28 | + ```bash |
| 29 | + mitsuki init |
| 30 | + ``` |
| 31 | + |
| 32 | +2. **Enable Alembic** |
| 33 | + |
| 34 | + When prompted, answer "yes" to setting up Alembic: |
| 35 | + |
| 36 | + ``` |
| 37 | + Setup Alembic for database migrations? [Y/n]: y |
| 38 | + ``` |
| 39 | + |
| 40 | +3. **Generated Files** |
| 41 | + |
| 42 | + The CLI will generate the following files in your project's root directory: |
| 43 | +
|
| 44 | + ``` |
| 45 | + my_app/ |
| 46 | + ├── alembic.ini # Alembic configuration |
| 47 | + ├── alembic/ # Migration scripts |
| 48 | + │ ├── env.py # Alembic runtime environment |
| 49 | + │ ├── script.py.mako # Migration template |
| 50 | + │ └── versions/ # Directory for migration files |
| 51 | + └── src/ |
| 52 | + └── my_app/ |
| 53 | + └── ... |
| 54 | + ``` |
| 55 | +
|
| 56 | +4. **Success Message** |
| 57 | +
|
| 58 | + The CLI will confirm that Alembic has been configured: |
| 59 | +
|
| 60 | + ``` |
| 61 | + ✓ Alembic configured |
| 62 | +
|
| 63 | + To create your first migration: |
| 64 | + cd my_app |
| 65 | + alembic revision --autogenerate -m "initial schema" |
| 66 | + alembic upgrade head |
| 67 | + ``` |
| 68 | +
|
| 69 | +## Migration Workflow |
| 70 | +
|
| 71 | +Once your project is set up, you can use the standard Alembic workflow to manage your database schema. |
| 72 | +
|
| 73 | +### 1. Import Your Entities |
| 74 | +
|
| 75 | +Before generating a migration, you need to make sure Alembic can see your `@Entity` classes. The generated `alembic/env.py` includes a wildcard import: |
| 76 | +
|
| 77 | +```python |
| 78 | +from my_app.src.domain import * |
| 79 | +``` |
| 80 | +
|
| 81 | +This automatically imports all entities in your `domain` package. If you place your entities in different packages, you'll need to update this import or add additional imports: |
| 82 | + |
| 83 | +```python |
| 84 | +# alembic/env.py |
| 85 | +
|
| 86 | +# ... (existing code) |
| 87 | +
|
| 88 | +# Import your entities so Mitsuki can discover them |
| 89 | +from my_app.src.domain import * |
| 90 | +from my_app.src.models import * # If you have entities in other packages |
| 91 | +
|
| 92 | +# ... (rest of the file) |
| 93 | +``` |
| 94 | + |
| 95 | +### 2. Generate a Migration |
| 96 | + |
| 97 | +Whenever you create a new entity or modify an existing one, you can generate a new migration script automatically: |
| 98 | + |
| 99 | +```bash |
| 100 | +alembic revision --autogenerate -m "Add Post entity" |
| 101 | +``` |
| 102 | + |
| 103 | +This will create a new file in the `alembic/versions/` directory containing the `upgrade` and `downgrade` functions for applying and reverting the schema changes. |
| 104 | + |
| 105 | +### 3. Apply the Migration |
| 106 | + |
| 107 | +To apply the migration to your database, run: |
| 108 | + |
| 109 | +```bash |
| 110 | +alembic upgrade head |
| 111 | +``` |
| 112 | + |
| 113 | +This will execute the `upgrade` function in the latest migration script, bringing your database schema up to date. |
| 114 | + |
| 115 | +### 4. Downgrade a Migration |
| 116 | + |
| 117 | +To revert the last migration, you can use: |
| 118 | + |
| 119 | +```bash |
| 120 | +alembic downgrade -1 |
| 121 | +``` |
| 122 | + |
| 123 | +## How It Works |
| 124 | + |
| 125 | +The integration between Mitsuki and Alembic is designed to be seamless and requires minimal configuration on your part. |
| 126 | + |
| 127 | +- **`alembic.ini`**: This is the main configuration file for Alembic. The generated file is pre-configured with the location of the migration scripts. |
| 128 | + |
| 129 | +- **`alembic/env.py`**: This is the key file for the integration. It's responsible for: |
| 130 | + - Reading your `application.yml` or `application-{profile}.yml` to get the correct database URL based on the `MITSUKI_PROFILE` environment variable. |
| 131 | + - Importing your entity classes so that Alembic's autogenerate feature can detect changes (via `from {{app_name}}.src.domain import *`). |
| 132 | + - Getting the SQLAlchemy metadata from Mitsuki using `get_sqlalchemy_metadata()`, which contains the schema information for all your entities. |
| 133 | +
|
| 134 | +- **`get_sqlalchemy_metadata()`**: This function from `mitsuki.data` automatically discovers all registered `@Entity` classes and builds SQLAlchemy metadata without requiring database initialization or async operations. |
| 135 | +
|
| 136 | +## Manual Setup |
| 137 | +
|
| 138 | +If you have an existing Mitsuki project and want to add Alembic support, you can follow these steps: |
| 139 | +
|
| 140 | +1. **Install Alembic**: |
| 141 | + ```bash |
| 142 | + pip install alembic |
| 143 | + ``` |
| 144 | + |
| 145 | +2. **Initialize Alembic**: |
| 146 | + ```bash |
| 147 | + alembic init alembic |
| 148 | + ``` |
| 149 | + |
| 150 | +3. **Configure `alembic/env.py`**: |
| 151 | + Replace the contents of `alembic/env.py` with the following, making sure to update the import paths to match your project structure: |
| 152 | + ```python |
| 153 | + import asyncio |
| 154 | + import os |
| 155 | + from logging.config import fileConfig |
| 156 | +
|
| 157 | + from sqlalchemy import pool |
| 158 | + from sqlalchemy.engine import Connection |
| 159 | + from sqlalchemy.ext.asyncio import async_engine_from_config |
| 160 | +
|
| 161 | + from alembic import context |
| 162 | +
|
| 163 | + config = context.config |
| 164 | +
|
| 165 | + if config.config_file_name is not None: |
| 166 | + fileConfig(config.config_file_name) |
| 167 | +
|
| 168 | + from my_app.src.domain import * |
| 169 | + from mitsuki.data import convert_to_async_url, get_sqlalchemy_metadata |
| 170 | +
|
| 171 | + target_metadata = get_sqlalchemy_metadata() |
| 172 | +
|
| 173 | +
|
| 174 | + def get_url(): |
| 175 | + """Get database URL from application.yml based on MITSUKI_PROFILE.""" |
| 176 | + import yaml |
| 177 | + profile = os.getenv("MITSUKI_PROFILE", "") |
| 178 | +
|
| 179 | + if profile: |
| 180 | + config_file = f"application-{profile}.yml" |
| 181 | + if not os.path.exists(config_file): |
| 182 | + raise FileNotFoundError( |
| 183 | + f"Configuration file '{config_file}' not found for MITSUKI_PROFILE='{profile}'. " |
| 184 | + f"Available profiles: dev, stg, prod (or unset MITSUKI_PROFILE to use application.yml)" |
| 185 | + ) |
| 186 | + else: |
| 187 | + config_file = "application.yml" |
| 188 | +
|
| 189 | + with open(config_file) as f: |
| 190 | + app_config = yaml.safe_load(f) |
| 191 | +
|
| 192 | + url = app_config["database"]["url"] |
| 193 | + return convert_to_async_url(url) |
| 194 | +
|
| 195 | +
|
| 196 | + def render_item(type_, obj, autogen_context): |
| 197 | + """Render custom types for migrations.""" |
| 198 | + if type_ == "type": |
| 199 | + if obj.__class__.__name__ == "GUID": |
| 200 | + autogen_context.imports.add("from mitsuki.data.adapters.sqlalchemy import GUID") |
| 201 | + return "GUID()" |
| 202 | + return False |
| 203 | +
|
| 204 | +
|
| 205 | + def run_migrations_offline() -> None: |
| 206 | + """Run migrations in 'offline' mode.""" |
| 207 | + url = get_url() |
| 208 | + context.configure( |
| 209 | + url=url, |
| 210 | + target_metadata=target_metadata, |
| 211 | + literal_binds=True, |
| 212 | + dialect_opts={"paramstyle": "named"}, |
| 213 | + render_item=render_item, |
| 214 | + ) |
| 215 | +
|
| 216 | + with context.begin_transaction(): |
| 217 | + context.run_migrations() |
| 218 | +
|
| 219 | +
|
| 220 | + def do_run_migrations(connection: Connection) -> None: |
| 221 | + context.configure( |
| 222 | + connection=connection, |
| 223 | + target_metadata=target_metadata, |
| 224 | + render_item=render_item, |
| 225 | + ) |
| 226 | + with context.begin_transaction(): |
| 227 | + context.run_migrations() |
| 228 | +
|
| 229 | +
|
| 230 | + async def run_async_migrations() -> None: |
| 231 | + """Run migrations in 'online' mode with async engine.""" |
| 232 | + configuration = config.get_section(config.config_ini_section, {}) |
| 233 | + configuration["sqlalchemy.url"] = get_url() |
| 234 | +
|
| 235 | + connectable = async_engine_from_config( |
| 236 | + configuration, |
| 237 | + prefix="sqlalchemy.", |
| 238 | + poolclass=pool.NullPool, |
| 239 | + ) |
| 240 | +
|
| 241 | + async with connectable.connect() as connection: |
| 242 | + await connection.run_sync(do_run_migrations) |
| 243 | +
|
| 244 | + await connectable.dispose() |
| 245 | +
|
| 246 | +
|
| 247 | + def run_migrations_online() -> None: |
| 248 | + """Run migrations in 'online' mode.""" |
| 249 | + asyncio.run(run_async_migrations()) |
| 250 | +
|
| 251 | +
|
| 252 | + if context.is_offline_mode(): |
| 253 | + run_migrations_offline() |
| 254 | + else: |
| 255 | + run_migrations_online() |
| 256 | + ``` |
0 commit comments