django data migrations best practices

django data migrations best practices

What is a django migration?

  • Django migration is a python module which alters the database when we apply it with command python manage.py migrate

What is a django data migration?

  • In most of the cases django migrations are used to alter the schema.
  • In some cases when we alter the database schema and add a new column in the database and update the data into the column by processing other tables or the same table with different columns.
  • For above case we deal with the data so we call this migration as data migration.
  • Django does not auto provide the data migration. We need to write the data migration using RunPython operation.

How to write a django data migration?

  • Let's create a empty migration with below command
    python manage.py makemigrations --empty <app name>
    
  • Djanog will create the empty migration with some randome name. Rename the migration file name as per the operations we want to perform.
  • The created empty migration looks like below
    # Generated by Django A.B on YYYY-MM-DD HH:MM
    from django.db import migrations
    
    class Migration(migrations.Migration):
    
        dependencies = [
            ('yourappname', '0001_initial'),
        ]
    
        operations = [
        ]
    
  • A data migration is a simple python function which takes the two parameters app and schema_editor and performs the operations as we defined. Let's see a simple example
    from django.db import migrations
    
    def combine_names(apps, schema_editor):
        Person = apps.get_model('yourappname', 'Person')
        for person in Person.objects.all():
            person.full_name = '%s %s' % (person.first_name, person.last_name)
            person.save()
    
    class Migration(migrations.Migration):
    
        dependencies = [
            ('yourappname', '0001_initial'),
        ]
    
        operations = [
            migrations.RunPython(combine_names),
        ]
    
  • Now, run the command python manage.py migrate to apply the migration.

Note: Django executes the migration operations in a single database transaction. So, do not perform database alter operations and data migration operations in the same migration. If you perform both operations in the same migration file then django will throw an error.