Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Safely Remove Old Migrations?

I've got a Django app with a lot of out-of-date migrations. I'd like to remove the old migrations and start fresh.

The app has 14 different "migrations" folders.

Here is what a few of them look like:

enter image description here

enter image description here

enter image description here

Is it safe to remove all the contents from each of these folders? Or, do I have to make sure to only remove some of the files -- and if so which files?

like image 383
VikR Avatar asked Sep 18 '19 21:09

VikR


People also ask

How do I remove a specific migration in Django?

There is a solution: run migrations 2 and 3 backwards with ./manage.py migrate my_app 0001 , then delete migration files. If you can't migrate back (e.g. you messed up with your database manually) then you can fake migrate back with ./manage.py migrate my_app 0001 --fake and set up database as it should be manually.

Can I delete migrations folder in Django?

Django will remove any prior migrations that were applied to the specified app (myApp). It searches for Python files (migration files) in the migrations folder of each project app, with the exception of init.py, and deletes them.

Can we delete migration file?

This can be easily done by deleting your Migrations folder and dropping your database; at that point you can create a new initial migration, which will contain your entire current schema. It's also possible to reset all migrations and create a single one without losing your data.


3 Answers

You should never just delete migrations before unapplying them, or it will be a nightmare when you want to apply new migrations.

To unapply migrations you should do the following:

  1. Use the python manage.py migrate your_app_name XXXX in case you want to unapply migrations after the XXXX migration. Otherwise use python manage.py migrate your_app_name zero to completely unapply all migrations.

  2. Remove the .pyc files under /migrations/_pycache_/ that you have unapplied.

  3. Remove the .py files under migrations/ that you have unapplied.

Now you can create new migrations without any headaches.

If what you're looking for is to squash all the migrations into one, do the steps above removing all migrations and then run python manage.py makemigrations your_app_name to create a single migration file. After that just run python manage.py migrate your_app_name and you're done.

like image 75
Jordan Mora Avatar answered Oct 10 '22 00:10

Jordan Mora


That depends. If you have a production database (or any database you cannot simply drop and recreate), then the answer is no, you cannot safely remove migrations.

If you do not have any permanent databases, then yes, you can remove all migrations, run python manage.py makemigrations --initial and it will create fresh migrations based on your current models.

Also, you should check if any of the migrations are custom data migrations written by hand. If there are any, you might want to keep those.

The .pyc files are generally safe to remove, provided the related .py files are still there.

your first screenshot is not Django and looks like a JS project of some sort.

like image 24
Mad Wombat Avatar answered Oct 10 '22 00:10

Mad Wombat


  1. The json and js files are unrelated to the django migrations as well as __pycache__ folder. You can delete all off them.
  2. If you mean "previously applied and no longer needed as the project only needs the latest version of the migrations" you don't want to remove but squash them instead with squashmigrations which reduces the files you have to two, init file and the initial migration file, this way your project still works.
  3. If by remove you mean you no longer need them because you already changed the models so much that the previous migrations aren't even used other than being applied and unapplied without ever being used, doesn't matter, go to step 2 and do that instead of deleting the files manually. When you create migrations on your applications one by one, you also create migration dependency tree, well, django does. And it is really hard to keep track of after some point, if you try to delete everything thinking you can create new migration files with ease, trust me as someone who experienced otherwise, it does not work like that. It is way simpler to let django handle the migration squashing, it optimizes the migration meaning that it also deletes the unused ones in your final state.

More to read at: https://docs.djangoproject.com/en/2.2/topics/migrations/#migration-squashing

like image 31
Işık Kaplan Avatar answered Oct 10 '22 01:10

Işık Kaplan