Home > Mobile >  How to rollback non linear migrations in django?
How to rollback non linear migrations in django?

Time:02-25

Suppose I have following migrations in Django.

    X
  /   \
 A      B      [A and B can represent any number of linear migrations.] 
  \   /
    Y (merge migration).

Doing python manage.py migrate app X will unapply all of A, B and Y. How can I unapply only B and Y?. And keep A as it is, i.e applied.

CodePudding user response:

    X(*)      [* represent migration has been applied.]
  /   \
 A(*)   B(*)  [A and B can represent any number of linear migrations.] 
  \   /
    Y(*)      (merge migration) 

Steps to unapply only one path i.e B and Y

  1. Unapply only Y. By doing python manage.py migrate app B (or A; both works).

       X(*)     
     /    \
    A(*)   B(*) 
     \    /
       Y()   
    
  2. Remove the migration files A and Y for time being from the project location.

      X(*)     
       \
        B(*) 
    
  3. Now unapply B, by doing python manage.py migrate app X.

      X(*)     
       \
        B() 
    
  4. Bring the migration files A and Y to the original location. You can now safely delete the unapplied migrations B and Y, if you wish to.

      X(*)     
     /   \
    A(*)  B() 
     \   /
       Y()   
    

The gist is, django can only rollback migrations if the files are present in the location. If you do not want to rollback a migration path (i.e A here), remove it from the project location while performing rollback.

  • Related