Skip to content
Advertisement

How to prevent alembic revision –autogenerate from making revision file if it has not detected any changes?

I have project where I’m using SQLAlchemy for models and I’m trying to integrate Alembic for making migrations. Everything works as expected when I change models and Alembic sees that models have changed -> it creates good migration file with command: alembic revision --autogenerate -m "model changed"

But when I have NOT changed anything in models and I use the same command:

alembic revision --autogenerate -m "should be no migration"

revision gives me ’empty’ revision file like this:

JavaScript

What is purpose of this file? Could I prevent creation of this ’empty file’ when alembic revision –autogenerate will not see any changes? To compare when I use Django and it’s internal migration when I type command:

python manage.py makemigrations

I get output something like:

No changes detected

and there is not migration file created. Is there a way to do the same with Alembic revision? Or is there other command that could check if there were changes in models and if there were then I could simply run alembic revision and upgrade?

Advertisement

Answer

Accepted answer does not answer the question. The correct answer is: Yes, you can call alembic revision --autogenerate and be sure that only if there are changes a revision file would be generated:

As per alembic’s documentation

Implemented in Flask-Migrate (exactly in this file), it’s just a change to env.py to account for the needed feature, namely to not autogenerate a revision if there are no changes to the models.

You would still run alembic revision --autogenerate -m "should be no migration" but the change you would make to the env.py is, in short:

JavaScript

Now, you can easily call alembic revision --autogenerate without risking the creation of a new empty revision.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement