<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Guguweb - bash</title><link href="https://www.guguweb.com/" rel="alternate"></link><link href="https://www.guguweb.com/feeds/bash.atom.xml" rel="self"></link><id>https://www.guguweb.com/</id><updated>2022-01-04T17:58:44+01:00</updated><subtitle>Freelance developer and sysadmin</subtitle><entry><title>A simple script to update a DB migration in Django 1.7+</title><link href="https://www.guguweb.com/2015/10/22/a-simple-script-to-update-a-db-migration-in-django-1-7/" rel="alternate"></link><published>2015-10-22T10:41:37+00:00</published><updated>2022-01-04T17:58:44+01:00</updated><author><name>augusto</name></author><id>tag:www.guguweb.com,2015-10-22:/2015/10/22/a-simple-script-to-update-a-db-migration-in-django-1-7/</id><summary type="html">&lt;hr&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;South had the &lt;em&gt;update&lt;/em&gt; flag for the …&lt;/p&gt;</summary><content type="html">&lt;hr&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;South had the &lt;em&gt;update&lt;/em&gt; flag for the &lt;a href="http://south.readthedocs.org/en/latest/commands.html#schemamigration" target="_blank" rel="noopener noreferrer"&gt;schemamigration&lt;/a&gt; command, but I didn&amp;#8217;t find a similar functionality in the Django builtin &lt;a href="https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-makemigrations" target="_blank" rel="noopener noreferrer"&gt;makemigrations&lt;/a&gt; command introduced in Django 1.7.&lt;/p&gt;
&lt;p&gt;So I put togheter a simple bash script to automate the process.&lt;/p&gt;
&lt;!--more--&gt;

&lt;div class="gist"&gt;
    &lt;script src='https://gist.github.com/4b5ceb2eee14fe776c34.js'&gt;&lt;/script&gt;
    &lt;noscript&gt;
        &lt;pre&gt;&lt;code&gt;#!/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
&lt;/code&gt;&lt;/pre&gt;
    &lt;/noscript&gt;
&lt;/div&gt;
&lt;p&gt;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.&lt;/p&gt;</content><category term="snippets"></category><category term="bash"></category><category term="django"></category></entry></feed>