A simple script to update a DB migration in Django 1.7+


Thu 22 October 2015


During the development of a Django model on your local machine is it often necessary to refine the most recent migration to cope with updates to the model, without polluting the migrations of the app with a new migration for each local update.

South had the update flag for the schemamigration command, but I didn’t find a similar functionality in the Django builtin makemigrations command introduced in Django 1.7.

So I put togheter a simple bash script to automate the process.

#!/bin/bash
if [ -z $1 ]; then
echo "Usage: $0 app_label"
exit 1
fi
app=$1 # this is the name of the app where we want to update the last migration
# get the list of known migrations for the app
migrations=(`./manage.py showmigrations $app | awk '{print $2}' | tail -2`)
if [ ${#migrations[@]} == 1 ]; then
# there is just one migration in the list
# here we are updating the initial migration
previous_migration=zero
current_migration=${migrations[0]} # should be 0001_initial
else
# there is more than one migration in the list
# get the previous one to go back to
# and the current one to update
previous_migration=${migrations[0]}
current_migration=${migrations[1]}
fi
# go back to the previous migration
./manage.py migrate $app $previous_migration
# remove the current, outdated migration
rm $app/migrations/${current_migration}.*
# create a new migration
./manage.py makemigrations $app
# migrate the DB to the new migration
./manage.py migrate $app

Basically the script unapplies the last migration for a given app, update the migration and reapplies it. I find it a little time saver and it feels good to know that the script is doing the right things in the right order.


Share:

Type Comment Here (at least 3 chars)