Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reset django migrations on an application deployed on heroku?

Tags:

django

heroku

I have deployed a django application to heroku but I need to reset those migrations. Is that possible without deleting the entire project and redeploying it? I have some test data in that database that I would prefer not to have to enter all over again.

I'd like to delete all migration files and create new ones. Thing is, I deleted all migration files from my local machine and created new ones so now the migration files on my local machine are all 001. Pushing that to heroku says there were no changes because the 001 migration files already exist on there. Only deleting them would work. Basically something similar to

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete

but for heroku.

like image 324
Nelson King Avatar asked Jan 29 '26 10:01

Nelson King


2 Answers

Pushing to Heroku is part of the solution, but not all of it. This updates the migration files, but not the django_migrations table in your database.

  1. If you still have the old code running on Heroku:

    1. Back your database up

    2. Reverse all of your migrations:

      heroku run python manage.py migrate appname zero
      
    3. Then deploy your new code:

      git push heroku master
      
    4. Then run the new migrations:

      heroku run python manage.py migrate
      
  2. If you've already deployed the new code to Heroku:

    1. Back your database up

    2. Manually delete your app's tables from your database, as well as rows from the django_migrations table where the app matches your app name e.g. via heroku pg:psql (this may get tricky if you have other apps installed and inter-app dependencies)

    3. Run your new migrations:

      heroku run python migrate
      

Another, probably safer, option if you've already deployed the new code is to rollback to a version that reflects your database schema, then use option 1 above. In this case you can rollback again to the new version instead of doing a regular release.

like image 67
Chris Avatar answered Jan 31 '26 02:01

Chris


Yes it can be done by two ways.

  • You can generate fixture for your current database schema if no such column is deleted or altered.
  • You can dump your database than just remove database from your db server and migrate again! Than load dumped data into fresh db, this will keep your previous data safe.
like image 34
Devang Padhiyar Avatar answered Jan 31 '26 02:01

Devang Padhiyar