Data migrations in Django

At this point, our SQLite database consists of a small number of languages. However, we started with the goal to allow users to create snippets for a wide variety of languages. As things stand, other than manually creating Language objects one by one, we have no better way of populating these records.

Furthermore, in the deployment, if we choose to go with a more robust database like PostgreSQL then we have to again enter languages one by one.

What we need is a way to automatically load “system data” so that our application can run successfully no matter whether we are in development or production.

Data Migrations

Data migration is similar to ordinary migration we learned in lesson Basics of Migrations, but instead of altering database schema it changes the data in the database.

The following are two common uses cases of Data migration:

  1. Load essential data so that your application can operate correctly (this is what we need).
  2. When data needs to be updated, after changes in the models.

If you try to create a new migration via makemigrations command, you will get “No changes detected”.

This is because we haven’t made any changes since the last time we have run makemigrations command.

So how do we create data migration?

We can force Django to create an empty migration file using the --empty option followed by the name of the app.

This will create a time-stamped empty migration in the migrations subdirectory of the djangobin app.

The migration file we just created is of special significance to us. However, the name of the migration file gives a false impression that it is an ordinary migration. Let’s rename the file to reflect what it does.

Open the migration file and it should look like this:

djangobin/django_project/djangobin/migrations/language_data.py

As we discussed in Migrations in Django chapter, the operations list is where the meat of the migration lies but currently it is empty. Django comes with lots of built-in operations. We have already seen some of them in Migrations in Django chapter. The operation we are interested in is called RunPython.

The RunPython operation allows us to run arbitrary Python code from the migration file. It takes two functions: a forward function and a backward function. The forward function is executed when the migration is applied and the backward function is executed when the migration is rolled back.

Let’s define RunPython operation. Open language_data.py and modify it as follows:

djangobin/django_project/djangobin/migrations/language_data.py

Note: The code is truncated to save space. Remember, you can always see can view the full source code in the Github repo.

The forward and backward functions takes two arguments, app registry (which is an instance of django.apps.registry.Apps) and SchemaEditor.

The app registry contains the historical versions of all your models loaded into it to match where in your history the migration sits. And the SchemaEditor is what Django uses to communicate with the database.

Inside the data migration, you should always use the historical version of the model because the current version of the model might have changed in the interim. Django builds this historical model using the migration files. To load the historical version of the model we use get_model() method which takes app and model name as arguments.

Django uses historical models all the time but this is the first time we need to understand how it works.

Whenever your run makemigrations command Django compares the current version of the model with the historical version of the model stored in the migration files to figure out what’s needs to be added, updated or removed from the database and then creates a migration file based on the changes it encounters.

Our data migration is ready. To apply it execute the following command:

The output will be something like this:

Leave a Comment