Backend Development with FastAPI
Back to Data Layer and Auth

Database Migrations with Alembic

Schema changes as code. Alembic generates migration scripts; running them migrates production safely. FIND_VIDEO: search 'alembic database migrations fastapi tutorial' — recommended channel: ArjanCodes / Pretty Printed. Aim for 10 min or under.

12 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.
Loading progress...

Key moments

  1. Benefits of Migration Tools — Explains why schema migration management is necessary for database synchronization and safe rollbacks.
  2. Installing Alembic — Covers adding Alembic as a project dependency using pip or Poetry.
  3. Initializing Environment — Demonstrates initializing the migration directory and configuration files via the CLI.
  4. Configuring Database Connection — Shows how to specify the database connection URL in configuration files.
  5. Creating Revision Files — Details creating revision files and implementing upgrade and downgrade methods.
  6. Executing and Rolling Back — Demonstrates applying migrations to head and rolling back schema changes with relative offsets.
  7. Parallel Development Conflicts — Simulates creating migrations on parallel Git branches that produce divergent revision heads.
  8. Resolving Divergent Heads — Shows how to merge conflicting migration heads into a single unified revision history.
PDF notes

Frequently asked questions

How does Alembic know which migrations have been applied?

Alembic maintains an alembic_version table inside the target database that stores the current revision identifier.

Why does alembic upgrade head fail after merging Git branches?

Parallel branches create multiple migration files with the same parent, producing divergent heads that require an explicit merge revision.

Can I configure database connection strings dynamically instead of in alembic.ini?

Yes, you can override and dynamically inject database connection settings by modifying the run_migrations logic within env.py.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.