3

I'm using Django and Postgresql to develop a web service.

Suppose we've 3~4 branch which for the different features or old-version bugfix purpose.

Then, I met a problem, when I was in branch A and change django model, and run migrate to change database in my local test desktop.

When I switch to another branch which has no migration file, database will inconsistent and cannot work when I try to run django, I've to delete the database and recreate it.

In general, what's the best/common way to deal with this kind demands for developer environment?

codebrew
  • 329
  • 2
  • 13

4 Answers4

3

I understand your situation well and have been in same shoe several times.

Here is what I prefer(/do):

  1. I am in branch bug-fix/surname_degrade I changed the user data model [which generated user_migration_005] and then migrated the DB. Then my boss came and pointed out that the user is not able to login due to login degrade.

  2. So I have to switch branch and fix that first.

  3. I can rollback the migration[user_migration_005] which I have done few moments back. With something like this python manage.py migrate user_migration_004

  4. Switched branch and started working on hot-fix/login_degrade

  5. When I switch back to my previous task , I can just do migration and proceed.

With this procedure I don't need to delete my all tables or restore old database or anything like that.

I am a newbie, will be extremely happy to hear your thoughts.

Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34
1

The major issue here is that, you database will change everytime You migrate,so either you mantain you database consistency among different branches, or You can do One thing, while using/testing (after declaring all the models)
1) Delete all database tables ( If you have a backup or dummy data )
2) Delete all existing migration files in you branch
3) Create new migrations
4) Migrate to new migrations
The above steps can also be done if the models are re modified, after modification just repeat the steps.

toddler95
  • 19
  • 3
  • I agree to this answer. I would see problem more wide. It is custom practic to use Git-Flow strategy (keep separate branch for every feature and merge them to `master` periodically). Look at `trunk based` develop strategy https://trunkbaseddevelopment.com/ You will reduce coherence problem between branches – Pavel Minenkov Jul 26 '18 at 07:55
  • 2
    Don't do this. Deleting migrations is usually a bad idea. – Daniel Roseman Jul 26 '18 at 08:09
  • 1
    This may encounter another problem in my case, when other developers try to pull the migrations file which I have done the above steps, they'll conflict with their migrations, and have to delete database again. If changing model is very frequent, is this a good way, too? What bothers me currently is that I need to delete the database from time to time. – codebrew Jul 26 '18 at 08:09
1

Run a different test database in each branch.

When you fork the design, fork the database

Make a clone of the database and migrate that.

Jasen
  • 11,837
  • 2
  • 30
  • 48
0

Make sure when you push to git, you include your migrations, that wait when someone else pulls the branch and does a migrate django knows what changes were made to the database.

Oh Great One
  • 374
  • 2
  • 17