Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I delete the django migration files inside migrations directory

I personally like django for its MVC ideals. But while i am running Django migrations in version 1.7 each and every migrations i do in it is stored inside the migrations directory. If i delete those file it is throwing an error while migration.

I Tested like this. I created a new Django project and initiated a git repo . I ran some 3-4 migrations in Django which resulted in 3-4 migration files under the migrations directory. I tried deleting the very older migration files i.e (1st and 2nd migration files) and tried to run

python manage.py makemigrations

which does cause some error like "migration files not found". Later i did a git stash which restored the deleted files. Now i tried to run the same command again and it was working fine.

What my question is if a person runs some 50 changes in db during development all the migration files are stored in migrations directory. Is it possible to delete those files and do changes to db again without any interruption?

like image 461
sathya Narrayanan Avatar asked Feb 09 '15 07:02

sathya Narrayanan


People also ask

Can you delete migrations folder 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.

Where are Django migration stored?

Short answer: the migrations originate from Django apps and third party apps you installed in INSTALLED_APPS . Not the ones you defined yourself. Migrations are generated per app, and are stored in some_app/migrations .


2 Answers

The answer is "it depends".

If you are working against a production DB, or some DB that can't periodically blow away for whatever reason, then you absolutely want to keep around the migration files that you've applied to your DB. They should be checked into source control with the rest of your code.

Now, for a situation like yours, the easiest way to discard your 50 migrations would be to just blow away the db (and it's 50 migrations) and start from scratch given your current models. It's oftentimes a good idea to do this periodically as you evolve your models during development.

Its ok to blow away your models when you blow away your DB because syncdb will build a blank db using your current models. It'll then optionally populate the db using any initial fixtures. Conceptually, there is no longer anything that you've migrated from at such a point, so you don't need to keep around your old migrations for your old db. They are no longer relevant.

It's not usually good to delete migration files that have been applied to your DB unless you are either 1) blowing away the DB altogether, or 2) reverting the migrations first.

You might also appreciate knowing that when you apply migrations to a db it's also recording those migrations in a special table in the db itself. That's why things go haywire when you just delete the migration files. They have to stay in sync with the migration table

like image 118
Sean Azlin Avatar answered Oct 11 '22 05:10

Sean Azlin


If models match database it is safe to delete migration files.

Currently, with Django 3 I can safely remove the migrations directory, then run python manage.py makemigrations myapp and python manage.py migrate. After that I have 0001_initial.py migration file and my production database is intact. This works when models already match database.

like image 31
MartinsM Avatar answered Oct 11 '22 05:10

MartinsM