Django: Safely Remove Old Migrations? - django

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?

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.

The json and js files are unrelated to the django migrations as well as __pycache__ folder. You can delete all off them.
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.
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

Having marked one of the answers provided previously as being accepted, here is a summary of a few things I learned:
Deleting Django migrations is generally a bad idea.
Django keeps track of what's in your db through these migration files, as well as through a table it creates in your db, and if you delete any of this Django will start throwing errors on migrate that can be hard to fix.
I was getting some of those hard-to-fix errors. Here is what I did to fix it:
Ran migrate on the production server.
When I got an error, it would tell me how the db was out of sync with what Django expected. I corrected that manually by directly editing the db with an sql client.
E.g. If it said a key existed that wasn't supposed to exist, I deleted the relevant index from the indicated table.
Or if it said a table existed that wasn't supposed to exist, I backed up the table to a file, and deleted the table. Migrate then created the table, and then I repopulated it with data from the backup.
In the case of many-to-many tables, once Django had re-created them, I deleted all the new Django-created tables, and restored them from a backup created on my local dev system, which had already had all the latest migrations run on it.
Eventually I was able to complete all migrations successfully.
I have a feeling I lucked out and the above won't work in all cases! I've learned a lot about Django and migrations and will be much more careful about this in the future.

when you import from third app:
there are 2 step uninstall it
there are use the 'django_celery_beat' app for example.
step1: clean table
python .\manage.py migrate django_celery_beat zero
step2: remove app from INSTALLED_APPS
there are done!!!
this is django document on this.

How to Reset Migrations
if you are using linux/unix os then you can fire this command. delete all migration directory.
find . -path "/migrations/.py" -not -name "init.py" -delete
find . -path "/migrations/.pyc" -delete

Related

Deleted all my migrations including __init.py__

I am a beginner using Django. I have recently been working on a website that has a few different pages. I reference a couple of views from other apps in urls.py. One app I used to take inputs and make some calculations, so it had some forms defined. It was all working fine and I was able to import the views fine, but I changed one of my form field names. This caused some problems because I was storing these values in a database. That made sense to me because I had already saved some data with the previous naming convention and now I had changed it. So, I figured that deleting my database and migrations would allow me to start over and start a new database with my updated fields. I messed up though because I read that I should not have deleted my init.py file in my migrations folder.
I have tried re-doing my migrations with makemigrations and migrate but I keep getting an error saying that no changes are made and no new migrations show up in my folder. Also, now when I look in my urls.py file, the imported views and apps are showing up with a red underline and says "import package".
I have also tried reverting my code back and was able to get the migration files back, but the same error was shown in my urls.py file.
I have tried just about everything I could find online, but am now thinking that I may just have to recreate these apps.
Any help is appreciated!
First of all, to rename a form field, just change it in the models and then make and apply migrations. Django should detect the renaming of an existing field and create migrations.AlterField(...) statements (you can inspect the migration files that were created, and freely delete them if they are not yet applied/migrated).
First, please check if adding the empty file called __init__.py to the migration folder solves the problem. This is a python requirement, to let the interpreter know that the folder contains a python module.
Also if you have downloaded the project from somewhere, you need to have the existing migrations. Deleting them and recreating them yourself will likely fail.
After that just try running
python manage.py makemigrations
python manage.py migrate
If there are still no changes, check if there are migrations applied to your database by looking at the django_migrations table or by running this command:
python manage.py showmigrations
If this did not help, try to start with a clean project:
Backup all your code changes and revert to the starting point, the __init__.py file should be back and you should also delete the database (db.sqlite3)
run the migrate command
add all your code changes
check if your code changes have new migration files, they could cause problems. If you have not created or modified them manually, you can delete and let Django recreate them

Adding Migrations to Source Control and Merging Conflicting Migrations in Django

I took over a Django project and discovered that migrations for various apps were not being tracked by Git. That seemed a bit problematic since my understanding was that one should always track the migrations. Is that pretty much the consensus or are there reasons not to do it?
The second part of the question has to do with the fact that I have done some work involving tweaking some migrations locally. I would now like to push the changes to production. However, I am unsure as to what the best way to combine the possible conflicts would be.
For instance, in production, I have the following:
Untracked files:
(use "git add <file>..." to include in what will be committed)
mysite/aldryn_forms/migrations/0019_auto_20200730_1455.py
mysite/apps/common/migrations/0016_auto_20200624_2028.py
mysite/apps/common/migrations/0016_auto_20200625_1125.py
mysite/apps/common/migrations/0017_merge_20200625_1129.py
mysite/apps/common/migrations/0018_auto_20200720_1743.py
mysite/apps/payment/migrations/0005_auto_20200624_2028.py
mysite/apps/payment/migrations/0005_auto_20200625_1125.py
mysite/apps/payment/migrations/0006_merge_20200625_1129.py
mysite/apps/payment/migrations/0007_auto_20200720_1743.py
mysite/apps/payment/migrations/0008_paymentmodel_course.py
mysite/apps/payment/migrations/0009_paymentmodel_user.py
mysite/apps/plugins/migrations/0016_auto_20200624_2028.py
mysite/apps/plugins/migrations/0016_auto_20200625_1125.py
mysite/apps/plugins/migrations/0017_merge_20200625_1129.py
mysite/apps/xyz/migrations/0005_auto_20200730_1455.py
Locally, I have the following:
Untracked files:
(use "git add <file>..." to include in what will be committed)
mysite/aldryn_forms/migrations/0019_auto_20201108_1623.py
mysite/apps/common/migrations/0016_auto_20201108_1623.py
mysite/apps/common/migrations/0017_auto_20201108_1806.py
mysite/apps/payment/migrations/0005_auto_20201108_1623.py
mysite/apps/plugins/migrations/0016_auto_20201108_1623.py
mysite/apps/xyz/migrations/0005_auto_20201108_1623.py
These are the files with custom work:
mysite/apps/common/migrations/0016_auto_20201108_1623.py
mysite/apps/common/migrations/0017_auto_20201108_1806.py
It appears that all the migrations existing on the production server have been applied to the production database. Hence, I have concluded that they correctly describe the state of the production DB.
What should I do to sync my local work with that currently in-production?
First I would review code for conflicts in your new migrations and migrations aplied in production.
Then backup evrything (your site and production).
if you don't finde logic errors, try copy migrations files from production to yours site and go with :
python manage.py makemigrations –merge
python manage.py migrate
if it works then it will propably work in production.
There is article about it :
https://www.algotech.solutions/blog/python/django-migrations-and-how-to-manage-conflicts/#:~:text=So%2C%20in%20order%20to%20allow,manage.py%20makemigrations%20%E2%80%93merge)
once i had it too.. annoing is that you will have to do this -merge evry new migration.
At some point I reset migrations.
When you will have database in order. You can do:
backup db and remove insert from db table : migrations
delete all migration files from porject dir
qoute all paterns in urls.py
go with makemigrations and migrate
populete new db with old data (exept migration table)
oh there is even simpler method with --fake:
https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html
that will put you with initial migration.

Relation does not exist, in PostgreSQL, Django

So I've created a new model in Django, then executed both python manage.py makemigrations and python manage.py migrate in the right order. But then for some reason I accidentally dropped the table(relation) in PgAdmin (I know it sounds silly). So I tried deleting all the files in migrations folder but the init.py file. Then I ran the two commands above again, but could see no table in the PgAdmin. What should I do, aside from creating a table myself in PgAdmin?
Thanks in advance.
It won't work, because entry for all the migrations are already stored inside a table named django_migrations. So even if you run makemigrations after deleting all the migration files, it won't create a new one. So here is three ways you can fix it.
One: Drop DB(if data is not important)
Drop database and create a new one. Run makemigrations and migrate command.
Two: Create that table manually
If you already deleted all the migration files, you better restore them. You can use git for this: git checkout /path/to/migration/folder. Then you can manually create the table.
Three: Delete entries from django_migrations
I assumed you have deleted all the migration files. So this part covers the whole project. But #DenizKaplan has explained better way to do this.
If you are not using git, or no way to restore these files, then you can follow these steps:
Backup your database
Delete all entries from djang_migrations table
Run ./manage.py makemigrations to generate migration file
Run ./manage.py migrate <your lost app> to migrate your app(which you have lost in DB).
Run ./manage.py migrate --fake to fake the rest of apps.
There are some steps you need to check. If you have an empty output after makemigrations operations, you may need to check for django_migrations table to remove rows related to apps that you have working with. If this won't help at first place, you need check INSTALLED_APPS, maybe you may accidently delete apps. If all these not work, please give some detailed information about the error.

What is the correct way to deal with migrations of Django Database when pushing files to the remote git repository?

Actually we are group of 3 people working on the same project, and each one individually make changes in django database. After running migrations in individual machine, it creates migration file for each migration. When someone pushes updated code in remote git repository, it creates conflict with others' migrations of same name.
Because of this reason, I lost my whole data once. Kindly give suggestions what should I do with this migration thing?
here's what i do: whenever I want to fetch from remote i check if a duplicate migration will be fetched. (we have a script that checks for all migration directories if there are filenames which have the same starting number.) if that is the case, I 'merge' the migrations, usually like this:
Find the last migration before the duplicate, let's say it's migration 000X
Make sure you are on your local source version, before the duplicate is added.
migrate back to after migration n:
python manage migrate app 000X
pull the new version including the duplicates.
remove your duplicate migrations
run schemamigration
python manage schemamigration --auto
Now you should get a new migration adding your model changes on top of the changes that were made in the migration you pulled.

What is the recommended way to run South migrations before Django 1.7 migrations?

I have a few projects with lots of South migrations, including ones that contain a fair amount of custom SQL that need to be run in a specific order. After upgrading to Django 1.7, this is the recommendation on how to convert a project to use South (from the Django documentation):
If you already have pre-existing migrations created with South, then the upgrade process to use django.db.migrations is quite simple:
Ensure all installs are fully up-to-date with their migrations.
Remove 'south' from INSTALLED_APPS.
Delete all your (numbered) migration files, but not the directory or __init__.py - make sure you remove the .pyc files too.
Run python manage.py makemigrations. Django should see the empty migration directories and make new initial migrations in the new format.
Run python manage.py migrate. Django will see that the tables for the initial migrations already exist and mark them as applied without running them.
In short, "wipe your existing migrations and Django will take care of the rest".
What is not mentioned here is what to do when existing South migrations don't only consist of model changes, but instead contain direct SQL, data migrations, etc, that need to be run in order. In this case, the auto-generated Django migrations will miss a lot of things, since not all of these changes are obvious from introspecting a models file.
Ideally, one would be able to run the existing migrations using South, and then have Django migrations take over. What might be the best way to go about this? If this is not possible or very much not recommended, what is the best alternative?
Maybe this post can help you. Essentially you have to:
Change your current migration directory from 'migrations' to 'south_migrations'
Update your settings with this line
SOUTH_MIGRATION_MODULES = {
'your_app': 'your_project.your_app.south_migrations',
}