Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there SQLAlchemy automigration tool like South for Django?

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?

like image 259
Zelid Avatar asked Feb 20 '11 16:02

Zelid


People also ask

What is SQLAlchemy alembic?

¶ Alembic is a lightweight database migration tool for usage with the SQLAlchemy Database Toolkit for Python. Front Matter. Project Homepage. Installation.


1 Answers

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:

  • Table additions and removals
  • Column additions and removals
  • Change of nullable status on columns
  • Basic changes in indexes, explicitly-named unique constraints, and foreign keys

(see also: https://alembic.sqlalchemy.org/en/latest/autogenerate.html)

Install alembic

pip install alembic 

or (depending on the version of Python you are using):

pip3 install alembic 

Configure 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)

Generate the migration

Simply execute the following command line:

alembic revision --autogenerate -m "Message for this migration" 

Or even (not recommended):

alembic revision --autogenerate 

Upgrade the database

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/)

like image 142
Mathieu Rodic Avatar answered Sep 19 '22 17:09

Mathieu Rodic