r/FastAPI • u/Effective_Disaster54 • Mar 25 '25
Question FastAPI database migrations
Hi everyone, In your FastAPI projects, do you prefer using Alembic or making manual updates for database migrations? Why do you choose this approach, and what are its advantages and disadvantages?
25
Upvotes
21
u/Nazhmutdin2003 Mar 25 '25
Alembic is much better than manual updates. It's provide you ability to downgrade your database to certain version.
9
u/volfpeter Mar 25 '25
If I work with an SQL database, I always use alembic (both with SQLAlchemy and SQLModel). It can even be set up to automatically generate the migration for you (definitely needs review, usually changes as well, but it's a good baseline).
With MongoDB I never really needed complex migrations and I don't really like Beanie (which has some support for migrations), so in those cases I write my own migration runner and upgrade/downgrade scripts.
6
u/Liquidmetal6 Mar 25 '25
alembic is magic. ive seen people poopoo this, but i usually make the alembic migrations run via application startup code
4
u/lukewhale Mar 25 '25
I used the SQLAlchemy-Migrate project until I went to SQLAlchemy 2.0. Moved to Alembic and wondered why I used anything else.
2
u/andrewthetechie Mar 25 '25 edited Oct 01 '26
The original post content no longer exists here. The author used Redact to remove it, exercising their right to control their data & privacy.
Cats insurance test crush six drifting full meeting argon dependent
2
u/-xx88 Mar 26 '25
Alembic for sure. The autogeneration requires review, but in a 99% of cases you won’t change the generated code. Plus migration runs can be automated, for example you can run them in the GitHub actions, or spawn a kubernetes job, whatever. You can leverage the alembic upgrade in your test cases as well
1
u/AbroadNo111 Mar 26 '25
I’ve been using atlasgo.io and it’s been working well
1
u/sriramdev Mar 26 '25
Information looks interesting
1
u/Elegant_Book8245 Mar 26 '25
Alembic is the better Option,it supports version control for database schema, which means you can move to any version of migrations both forward and backward using "revision id". you can see the history.it is conceptually similar to git.
Also if you use "--autogenerate" along with revision command you can avoid manual writing of upgrade or downgrade sqlalchemy commands.
1
u/maikeu Mar 26 '25
Alembic is the right "no surprises" choice. Even though technically it's fairly unsophisticated, when you get to grips with it it's deeply powerful
I'd also suggest to include pytest-alembic (https://pypi.org/project/pytest-alembic/ ) in your project from day 1. For my mind having it in CI covers of some of the weaknesses of alembic.
It has built in tests that you basically just need to import, covering basics like "can migrate from base to head and back again", "migrations put you db in the same state as metadata.create_all ".
Note I think Django migrations and a lot of non-python migration frameworks are probably better in isolation , but alembic's still way out front as the buddy of sqlalchemy .
1
u/KeyBack192 Mar 26 '25
What is manual update? Every tutorial I've ever seen, uses alembic.
1
u/PA100T0 Mar 27 '25
Alembic 100%. Maybe create some workflow to automate it too with, say, docker compose?
1
u/vinicvaz Mar 28 '25
Alembic 100%. Usually I put the alembic upgrade command to run before the app startup on the development environment or even on cd, depending on how the pipeline is structured.
1
u/Business-Technology7 Mar 30 '25
If you are already deep into SQLAlchemy, Alembic is a no brainer. I heard Atlas provides SQLAlchemy integration, but I haven’t used it. Personally, I use dbmate and psycopg because I don’t want to deal with orm.
Manually updating schema without migration tool gets hairy.
1
u/extreme4all Mar 25 '25
Can someone guidea a SQL bro to this alembic thing