|
| 1 | +# Database Migrations (Alembic) |
| 2 | + |
| 3 | +This directory contains database migration scripts managed by Alembic. These scripts track and apply changes to the application's database schema over time. |
| 4 | + |
| 5 | +The migration history for this project lives in the `alembic/versions/` directory. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Common Workflow |
| 10 | + |
| 11 | +The standard workflow involves creating, reviewing, and applying migrations. |
| 12 | + |
| 13 | +- **To create a new migration:** `alembic revision --autogenerate -m "Your description"` |
| 14 | +- **To apply migrations:** `alembic upgrade head` |
| 15 | +- **To see migration history:** `alembic history` |
| 16 | +- **To see the current version:** `alembic current` |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +### 1. Creating a New Migration |
| 21 | + |
| 22 | +When you change a SQLAlchemy model (e.g., in `db.py`), follow these steps to generate a migration script: |
| 23 | + |
| 24 | +**Step 1: Make your model changes.** |
| 25 | +Add, remove, or alter columns and tables in your SQLAlchemy model definitions. |
| 26 | + |
| 27 | +**Step 2: Autogenerate the script.** |
| 28 | +Run the following command from the project's root directory. Use a short but descriptive message. |
| 29 | + |
| 30 | +```bash |
| 31 | +alembic revision --autogenerate -m "Add slug and url to gateways table" |
| 32 | +``` |
| 33 | +**Step 3: Review and Edit the Script (CRITICAL STEP).** |
| 34 | +A new file will be created in alembic/versions/. Always open and review this file. |
| 35 | + |
| 36 | +Autogenerate is a starting point, not a final answer. It is good at detecting new columns and tables. |
| 37 | + |
| 38 | +It often requires significant manual editing for complex changes like: |
| 39 | + |
| 40 | +- Data migrations (populating new columns). |
| 41 | + |
| 42 | +- Renaming columns or tables. |
| 43 | + |
| 44 | +- Changes that require multi-stage operations (adding a column as nullable, populating it, then making it not-nullable). |
| 45 | + |
| 46 | +- Ensure the upgrade() and downgrade() functions are correct and logical. |
| 47 | + |
| 48 | +### 2. Applying Migrations |
| 49 | + |
| 50 | +To upgrade your database to the latest version: |
| 51 | +This command applies all pending migrations. This is the command used by developers locally and by the CI/CD pipeline during deployment. |
| 52 | +```bash |
| 53 | +alembic upgrade head |
| 54 | +``` |
| 55 | + |
| 56 | +To test your downgrade path (local development only): |
| 57 | +It's good practice to ensure your migrations are reversible. |
| 58 | +#### Revert the very last migration |
| 59 | +```bash |
| 60 | +alembic downgrade -1 |
| 61 | +``` |
| 62 | +#### You can then re-apply it |
| 63 | +```bash |
| 64 | +alembic upgrade +1 |
| 65 | +``` |
| 66 | + |
| 67 | +### 3. Deployment Notes |
| 68 | + |
| 69 | +**CI/CD:** During deployment, the alembic upgrade head command is run automatically to synchronize the database schema with the new application code before the server starts. |
| 70 | + |
| 71 | +**Configuration:** The sqlalchemy.url in alembic.ini is replaced by the value set for DATABASE_URL environment variable, which env.py is configured to read. |
0 commit comments