hoylemd
hoylemd

Reputation: 456

Is there a way to roll back all django app migrations?

I'm bootstrapping a new django project, and my team usually makes a 'setup/reset' script that rebuilds containers as well as drops(unless it doesnt exist yet) and rebuilds the database from scratch.

I found the manage.py flush command for dropping the data, but I also want to revert all migrations. I could only find a command for rolling back the migrations of a single app at a time:

manage.py migrate <app_name> zero

Is there a way I can do that for every app, or would I have to list them out in the script? We do have the manage.py migrate command that migrates everything, so I basically just want the opposite of that.

If it's not easily possible, I'll probably just make the db container drop and rebuild the database

Thanks!

Upvotes: 10

Views: 10993

Answers (3)

gxmad
gxmad

Reputation: 2230

The good way is to use Django tools :

python manage.py migrate my_app zero

To reverse the migrations in the database Reversing migrations : Django doc

Then, manually delete the migrations you don't need in my_app/migrations

And finally, your are back to clean project :

python manage.py makemigrations my_app
python manage.py migrate my_app

Enjoy !!

Upvotes: 17

Michael Kim
Michael Kim

Reputation: 771

python manage.py showmigrations | grep -P "^[^ ]" | xargs -I {app} python manage.py migrate {app} zero

If it's not easily possible, I'll probably just make the db container drop and rebuild the database

Honestly removing the db is way easier than letting Django unapply. I'm just adding this answer in case anyone wants a one-liner for other purposes.

Upvotes: 3

yrekkehs
yrekkehs

Reputation: 797

If you have django-extensions, python manage.py reset_db sounds like what you're looking for.

Upvotes: 7

Related Questions