Is there any library or tool to perform database migrations in python without SQLAlchemy? I am using a PostgreSQL database and the queries are raw SQL. I use psycopg2 for the database connection.
3 Answers
There are several tools that can be used to version your database with migrations in Python, outside of SQLAlchemy. I can't speak for any of them in particular, so I looked for ones that seem to have some adoption as well as active maintainence.
(as of Apr 14, 2023)
| Tool | GitHub | Stars | Maintained | Notes |
|---|---|---|---|---|
| pgmigrate | yandex/pgmigrate | 562 | Feb 3 2023 | Postgres only |
| dbmate | amacneil/dbmate | 3.3k | Mar 2023 | |
| Yoyo | coobas/yoyo-migrations | 0 | Mar 2020 | |
| Migrate | golang/migrate | 11.2k | Apr 2023 | go-lang library & CLI tool |
Other resources I've found seem to be tied to one framework or another. Searching the package index turns up a lot of older resources.
If one finds pgmigrate or dbmate to be undesirable, then from there it might be worth asking whether the migration tool really needs to be Python-specific. Typically migrations are a part of the build process and (in my opinion) ideally should require as little coding and configuration as possible.
Juan D. Vega's post, Language and framework agnostic database migrations, highlights the golang/migrate CLI tool, which attempts to offer a way to manage your migrations irrespective of the particulars of the application that the database supports.
4 Comments
If you can bring yourself to do your db migrations outside of the CLR, I'd strongly recommend considering FlyWay. It has been around forever, it's supported by RedGate, and the community version is free and open source. It has built-in support for all the major db platforms and installs like any other utility you'd use. For a Python app you'd do all of your db migrations via shell scripts or process calls instead of directly in the code.
My team settled on this tech because:
- Yoyo is pretty immature and buggy for us
- We don't use SQLAlchemy's OR framework
- Alembic's syntax and framework seems pretty heavyweight for us
- Under FlyWay's model we just write SQL scripts and check them into a directory. It's really lightweight.
- It's been proven to work nicely in clustered environments
Give it a look.
Comments
There is flask-migratepg for this purpose, where SQL files are simply placed under a database/migrations directory in alphanumerical order and migrations executed with flask migrate execute. It is based on Psycopg 3.
flask-migrate?alembicgenerates the migration scripts for you, it hides the enormous amount of complexity of updating a schema for an existing table in the database. Updating schema of existing tables in the database is a very complex operation and requires careful planning for tables with large number of columns and imagine if you have to do such thing frequently it is even cumbersome.alembicin combination withflask-migratemakes this task very easy.