liveBook · Manning

Reversible migration

As explained in Django in Action

A reversible migration is a migration that defines operations for both applying and undoing its changes.

A reversible migration defines both forward and backward operations, so a database change can be applied and undone. Django’s RunPython operation supplies one function for each direction. section 10.2

A practical data migration follows this sequence:

  • Add the replacement fields and create the schema migration. section 10.2

Renaming Promoter name data

The forward function copies first_name and last_name into full_name and common_name. The backward function reconstructs first_name and last_name from those replacement fields. section 10.2

How Django in Action covers it

How deep it goes
Implements paired RunPython functions, historical model access, row iteration, dependencies, and migration creation commands.
Examples
Transforms Promoter first_name and last_name fields into full_name and common_name fields.
What you can do after
Build it from scratch using an empty migration, paired Python functions, and historical models to transform existing data.
How it connects
Connects Django migrations with RunPython, data migration, model fields, and historical models.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.