django migrations and git branches while working on multiple features simultaneously

Viewed 368

In Django, when working on multiple new features simultaneously, what is the best way to handle database migrations with git?
(as in python manage.py makemigrations , and python manage.py migrate which are used to update the database after the models.py file has been changed)

I’m in middle of working on feature1, on its own branch. Now I need to create feature2 and push it to production.

Should I:

  1. fork new feature2 branch off of production, create feature2, merge back to prod, migrate.

  2. fork new feature2 branch off of production, create feature2, migrate, merge back to prod.

Or some other way?

And when I go back to feature1, what do i do to insure everything will be up to date?

1 Answers

If you use a database migration tool by command line in conjunction with git you can take advantage of up and downs SQL scripts.

Let's say you want to add some changes to your DB, through the migration tool you can define a namescript1-up.sql and namescript1-down.sql and you can checkout to a specific database version from the command line (after a git checkout).

For example, I use golang-migrate for my go apps (I think it can be used for any language when used by command line).

Anyway, I would say this is an improper use of a migration tool, which is more suitable to apply the scripts regarding only the schema variations and not regarding ordinary INSERT or UPDATE sql statements.

I would suggest working on a containerized version of your DB, so you can destroy and recreate anything on flight.

Related