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:
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?
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.
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.
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.
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:
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.
Remove the .pyc
files under /migrations/_pycache_/ that you have unapplied.
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.
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.
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.More to read at: https://docs.djangoproject.com/en/2.2/topics/migrations/#migration-squashing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With