Is there SQLAlchemy automigration tool like South for Django?
I looked to sqlalchemy-migrate but it doesn't seem to generate sql update scripts automatically or upgrade downgrade DB
Looks like with sqlalchemy-migrate you need to a) manually copy your old model to a new file b) crate new model in application and copy it to a new file c) write manually create/drop/alter tables in python sqlalchemy extended dialect d) generate sql alter script e) run command to execute alter sql script
As for me it doesn't solve the problem and only adds overhead, as I can simply do d) manually and it will be much faster then do a), b), c) manually just to d) that you can do in one step.
Is there any auto migration libraries for SQLAlchemy like South for Django, or many RoR-like migration tools?
What I need is to change SQLAlchemy model in python app, run tool and it will compare current DB schema to new DB schema that new model should use, and create Alter scripts that I can adjust manually and execute.
Is there any solution like this in Python?
¶ Alembic is a lightweight database migration tool for usage with the SQLAlchemy Database Toolkit for Python. Front Matter. Project Homepage. Installation.
You can perform automatic migrations with Alembic. I use it in two large-scale projects I am currently working on. The automatic migration generator can recognize:
(see also: https://alembic.sqlalchemy.org/en/latest/autogenerate.html)
pip install alembic
or (depending on the version of Python you are using):
pip3 install alembic
Execute the following command in your project:
alembic init alembic
This will set up alembic for your project, in a folder called alembic
.
You will then need to edit the generated alembic.ini
configuration file.
In the file env.py
, tell Alembic where to find SQLAlchemy's metadata
object in your project.
(see also: https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file)
Simply execute the following command line:
alembic revision --autogenerate -m "Message for this migration"
Or even (not recommended):
alembic revision --autogenerate
After this, I upgrade the database with this simple command from the folder containing the alembic.ini
configuration file:
alembic upgrade head
(see also: http://rodic.fr/blog/automatic-migrations-sqlalchemy-alembic/)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With