Skip to content

Instantly share code, notes, and snippets.

Django Migrations without Downtime

Note that in the below instructions, migrations are all run manually and are not an automatic part of the deployment process.

Adding Fields or Tables

Adding a (nullable) field or a new table

  1. Make the model or column addition in your code.
  2. Generate the model-change migrations locally.
  3. Make two separate pull requests, one containing the model change, one containing the newly-generated migrationst. Note that the model-change migration should only contain the model change code, not any supplemental code using the newly-structured model.
@majackson
majackson / migrations.md
Last active March 7, 2024 04:12
Django migrations without downtime

Django Migrations without Downtime

The following instructions describe a set of processes allowing you to run Django database migrations against a production database without having to bring the web service down.

Note that in the below instructions, migrations are all run manually at explicit points, and are not an automatic part of the deployment process.

Adding Fields or Tables

Adding a (nullable) field or a new table

  1. Make the model or column addition in your code.
@majackson
majackson / arrayfield_listfilter.py
Created November 2, 2015 11:16
A reusable list filter for displaying/filtering ArrayFields in the django admin
from django.contrib import admin
class ArrayFieldListFilter(admin.SimpleListFilter):
"Admin-filterable field for an ArrayList"
def __init__(self, request, params, model, model_admin):
self._choices = model._meta.get_field(self.parameter_name).base_field.choices
super(ArrayFieldListFilter, self).__init__(request, params, model, model_admin)
@majackson
majackson / jsonlistfile.py
Created April 24, 2012 14:11
Class providing methods and context manager to efficiently read and write list-based json documents.
import json
class JsonListFile(object):
"""Class providing methods and context manager to read and write list-based
json documents.
This class will write out to the file on each append (maybe, os-dependent),
eliminating the need to hold the entire json object in memory before writing
(as is necessary with the plain stdlib json module).
Primitive, but useful."""
@majackson
majackson / add_novalidate.js
Created March 5, 2012 10:09
Bookmarklet to remove HTML5 validation from all forms on a page
javascript:fs=document.getElementsByTagName('form'); for (f=0;f<fs.length;f++) { f.setAttribute('novalidate', 'novalidate') }