Overview
VidaPlus API uses Alembic for database migrations, providing version control for your database schema. Alembic works seamlessly with SQLAlchemy to track and apply schema changes over time.Alembic Configuration
The project uses Alembic with the following configuration:Configuration File
Thealembic.ini file is located in the project root and contains:
The
sqlalchemy.url in alembic.ini is a placeholder. The actual database URL is set dynamically in migrations/env.py from your environment variables.Environment Configuration
Themigrations/env.py file configures Alembic to use your application’s settings:
- Database URL is loaded from
Settings().DATABASE_URLenvironment variable - Metadata is imported from
table_registrywhich contains all SQLAlchemy models - Supports both offline and online migration modes
Environment Variables
Ensure your.env file contains the required database URL:
Creating Migrations
Auto-generating Migrations
Alembic can automatically detect changes to your SQLAlchemy models and generate migration scripts:Modify Your Models
Make changes to your models in
vidaplus/models/models.py. For example, adding a new field:Generate Migration Script
Use the Alembic This creates a new migration file in
revision command with --autogenerate:migrations/versions/ with a name like:Manual Migrations
For complex schema changes, create an empty migration and write the upgrade/downgrade logic manually:Running Migrations
Upgrade to Latest Version
Apply all pending migrations to bring your database to the latest schema:Upgrade to Specific Revision
Upgrade to a specific migration revision:Upgrade by Relative Steps
Upgrade by a specific number of revisions:Downgrade Migrations
Downgrade to a previous revision:Migration Examples from VidaPlus
Creating Tables
Here’s a real example from the VidaPlus codebase that creates theestoques and leitos tables:
Checking Migration Status
Current Revision
Check which migration revision your database is currently at:Migration History
View the migration history:Show Pending Migrations
See which migrations haven’t been applied yet:Best Practices
Always Use Version Control
Commit migration files to version control (Git) along with your model changes. This ensures migrations and models stay in sync across your team.
Test Migrations Locally
Before applying migrations to production, test them on a local database or staging environment:
Running Migrations in Docker
When using Docker, run migrations inside the container:Troubleshooting
”Can’t locate revision identified by…”
This usually means your migration history is out of sync. Check:- All migration files are present in
migrations/versions/ - The
alembic_versiontable in your database matches a valid revision
”Target database is not up to date”
Runalembic upgrade head to apply pending migrations.
Environment Variable Not Found
Ensure your.env file contains DATABASE_URL and is in the project root:
Migration Conflicts
If multiple developers create migrations simultaneously, you may have conflicts. Resolve by:- Identifying conflicting revisions
- Using
alembic mergeto create a merge revision - Reordering migrations if necessary
Next Steps
Testing
Learn how to test your application with pytest
Database Models
Explore the database models and schema