PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 13, 2022

[FIXED] How to prevent alembic revision --autogenerate from making revision file if it has not detected any changes?

 April 13, 2022     alembic, migration, python, revision     No comments   

Issue

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:

"""next

Revision ID: d06d2a8fed5d
Revises: 4461d5328f57
Create Date: 2021-12-02 18:09:42.208607

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'd06d2a8fed5d'
down_revision = '4461d5328f57'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    pass
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    pass
    # ### end Alembic commands ###

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?


Solution

To answer this question we need to understand how the alembic works. Basically when you run alembic revision it creates a revision and holds revisionid and down_revision then line them like a queue... And if you check your database you see the latest revision id in the alembic_version table. Each revision needs a file to handle the migrations so you cant have a revision without it's related file... Because how then alembic can auto migrate to the latest revision from base when rebuilding the project ? it's like a missing piece of puzzle.

So long story short No you can't have a revision without a file related to it.

P.S. About Django and other frameworks, they have a pretty powerful migration script so it handles the migrations. In this case the framework's migrator inspect your models file and see there are no changes so it creates no revisions! That's why you don't see any change not in migrations folder and not in alembic_version table of your database.



Answered By - Mehrdad Khojastefar
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing