Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Django migrations live in source control?

Tags:

As the title says... I'm not sure if Django migrations should live in source control.

For:

  • If they get accidentally deleted from my local machine, it's going to cause me issues next time I want to run a migration... right? So it would be useful for me to have them.

Against:

  • Devs setting up the project for the first time shouldn't need to run them, they can just work straight from the models file.
  • They seem like machine-specific cruft.
  • Could they potentially reveal things I don't want about the database?
like image 621
Richard Avatar asked May 12 '15 15:05

Richard


People also ask

Should we commit migrations Django?

You should be making them once on your development machine and then running the same migrations on your colleagues' machines, your staging machines, and eventually your production machines. If you follow this process, you shouldn't be getting any merge conflicts in the migration files.

Where are Django migrations 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 .

Should I delete migrations Django?

If you've not created some custom migrations for like loading data, then yes. It should be safe to remove all of the migrations in the migrations folder and run makemigratons command.


1 Answers

Yes, absolutely!!

From the docs:

The migration files for each app live in a “migrations” directory inside of that app, and are designed to be committed to, and distributed as part of, its codebase. You should be making them once on your development machine and then running the same migrations on your colleagues’ machines, your staging machines, and eventually your production machines.

One big point is that migrations should always be tested before you deploy them in production. You should never create migrations on production, only apply them.

You also want to synchronise the state of the models in source control with the state of the database. If someone pulls your branch, has to find a bug, and goes back in the source control's history, he'd need the migration files to change the state of the database to match that point in time. If he has to create his own migration files, they won't include the intermediate state, and he runs into a problem where his models are out-of-sync with the database.

like image 178
knbk Avatar answered Mar 13 '23 23:03

knbk