Home > OS >  Django REST committing migration files to production
Django REST committing migration files to production

Time:01-08

I have a django REST project and a PostgreSQL database deployed to DigitalOcean. When I develop locally, I have a separate dockerized REST server and a separate PostgreSQL database to test backend features without touching production data.

My question arises when I'm adding/modifying model fields that require me to make migrations using python [manage.py](https://manage.py) makemigrations and python [manage.py](https://manage.py) migrate command. Here is my current situation so far:

What I was supposed to do

IN LOCAL ENV, to create the migration files,

python manage.py makemigrations 
python manage.py migrate

Now commit these newly created files, something like below.

git add app/migrations/...
git commit -m 'add migration files' app/migrations/...

IN PRODUCTION ENV, run only the below command.

python manage.py migrate

What I did so far

IN LOCAL ENV, created the migration files,

python manage.py makemigrations 
python manage.py migrate

I committed & pushed the changes to production WITHOUT the created migration file

IN PRODUCTION ENV, ran BOTH commands.

python manage.py makemigrations
python manage.py migrate

The production server successfully added the isActive field to the database and is working fine, but I still have a 0011_user_isActive.py migration file in my local changes that hasn't been staged/committed/pushed to github repo.

And because I ran makemigrations command in production env, it probably created the same migration file that I haven't pushed from local env.

My questions are:

  1. What happens if I push the local migration file to production? Wouldn't it create a conflict when I run migration command on digitalocean console in the future?
  2. How should I fix this situation?

I am just scared I'm going to corrupt/conflict my production database as I'm very inexperienced in databases and have too much to risk at the moment. Would appreciate any tips on best practices when dealing with such situations!

CodePudding user response:

As docs says:

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.

So it's best practice is to push your migration files and make your local and production migration files sync.

And if you got conflict when pushing migraions files and pulling them, the makemigrations --merge command is for solving that.

Also docs says:

Because migrations are stored in version control, you’ll occasionally come across situations where you and another developer have both committed a migration to the same app at the same time, resulting in two migrations with the same number. Don’t worry - the numbers are just there for developers’ reference, Django just cares that each migration has a different name. Migrations specify which other migrations they depend on - including earlier migrations in the same app - in the file, so it’s possible to detect when there’s two new migrations for the same app that aren’t ordered. When this happens, Django will prompt you and give you some options. If it thinks it’s safe enough, it will offer to automatically linearize the two migrations for you. If not, you’ll have to go in and modify the migrations yourself - don’t worry, this isn’t difficult, and is explained more in Migration files below.

Also be aware that in case of updating existed data in production, you can use RunPython in migration file. Read about it here.

  • Related