Django Data Migrations: Getting Them Right on a Live Database
Schema migrations, adding a column, creating a table are the ones everyone thinks of first. Data migrations are the quieter, riskier cousin: transforming or backfilling actual data, on a database that's serving real traffic while you do it. Get a schema migration wrong and you usually find out immediately. Get a data migration wrong on a table with tens of thousands of rows, and you might not…
Schema migrations like adding a column or creating a table are straightforward. However, data migrations, which transform or backfill actual data while a database serves real traffic, are often more complex and riskier. A data migration gone wrong on a large table may not be detected until a user reports incorrect data. The key to getting data migrations right on a live database involves a different approach than schema migrations.
When creating a data migration in Django, it follows the same process as any migration but uses RunPython instead of schema operations. This is where the real considerations come in. Always provide a reverse function for the migration, as it becomes crucial if the migration goes wrong in production. Instead of importing models directly, use apps.get_model to ensure the migration runs against the correct version of the model at the time of execution.
Instead of loading an entire large queryset into memory, use .iterator() to stream results from the database, reducing memory usage significantly. Also, batch large updates to avoid holding locks on the table for extended periods, affecting other production traffic. Finally, make migrations idempotent to ensure that re-running the migration won't cause double-processing of rows that have already been successfully updated.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.