[Answered ]-Migrate Database of Django 1.4 application to Django 1.8 version

2👍

Required pre-reading

Django migrations:
https://docs.djangoproject.com/en/1.8/ref/django-admin/#makemigrations-app-label

Assuming you were using South:
https://docs.djangoproject.com/en/1.8/topics/migrations/#upgrading-from-south

Getting Started

Firstly dump your local database I prefer using mysql/postgres/whatever docs for this rather than using ./manage.py dumpdata.

You will also want to dump your production database as well just for safekeeping.

Next in your local environment I would dump the database and create a new database.

Then I would test that all your migrations actually work on a blank database.
These are the instructions for django 1.8

./manage.py makemirgrations

./manage.py migrate

That will help show up if any migrations are in an inconsistent state operating from a blank slate. If you encounter any errors they should be fixed first.

Given that works, now I would test that your migrations actually work against your production data.

So drop your local database, create a fresh one then load the production dump in.

If the tables are already configured correctly (i.e. they are in an up to date state in your production database) then you will need to fake all migrations.

./manage.py migrate --fake <appname>

However given that you have changed some models since upgrading to 1.8 in your local environment, then you might need to fake only some of the migrations. This could be the tricky part depending on the timing of when you upgraded and when you created the migrations.

Because django 1.7 will just create an initial migration for each app, you may need to actually break up the migration for some apps. That is, instead of 0001_initial you might need to manually break up that migration into 2 components:
1. a migration to match the current state of your production database
2. a migration to match any additional changes you have made to your model since then.

One way to do this is to checkout your first commit once you had django 1.8 working correctly locally then run

./manage.py makemigrations

then commit that

then go forward to your latest commit then run

./manage.py makemigrations

Now you should have 2+ migrations in each app that you have modified since upgrading to django 1.8.

Then you can fake initial on those apps that have 2+ new migrations for django 1.8

./manage.py migrate --fake-initial app1 app2

and the rest just

./manage.py migrate app3 app4

Now run your tests to confirm everything is working locally.

If you have changed migrations you will again want to test locally against a blank database to test that they work smoothly

Once that is working, record the ‘migrate’ commands that you used – then deploy your app to production and run just those migration commands once you have upgraded to django 1.8 on your server.

After Successful Completion

  1. Take new dumps of your local and production databases
  2. Uninstall South (assuming you had it installed before) from local and production environments

I’m sure there are a couple of holes in the above but hopefully that gives you the gist of what you need to do.

Leave a comment