Skip to content

Instantly share code, notes, and snippets.

@destos
Created April 9, 2015 19:36
Show Gist options
  • Select an option

  • Save destos/b808a601166fc8a74188 to your computer and use it in GitHub Desktop.

Select an option

Save destos/b808a601166fc8a74188 to your computer and use it in GitHub Desktop.
How to add waffle flags to your Django Migrations
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
flag_name = 'the_flag_is_nigh'
Flag = models.get_app('waffle').Flag
def update_waffle_forward(apps, schema_editor):
"""Create or udpate flag"""
Flag.objects.update_or_create(
name=flag_name,
defaults={
'superusers': True,
'everyone': True,
'note': 'your waffle flag note'
}
)
def update_waffle_backward(apps, schema_editor):
"""Delete flag"""
Flag.objects.get(name=flag_name).delete()
class Migration(migrations.Migration):
dependencies = [
('an_app', 'another_migration'),
]
operations = [
migrations.RunPython(update_waffle_forward, update_waffle_backward),
]

First: create an empty migrations

This should include a link to any dependant migrations in the dependencies attribute. We'll need to make sure those get coppied over.

./manage.py makemigrations your_app --empty

Second: copy over the example

Open the newly created migration and paste in the example_migration.py

Copy over your dependancies and change the flag_name module variable to your new flag.

If needed, change the defaults on the newfly created Flag model in update_waffle_forward.

Third: Done!

Now you have a nice migration that can run on everyone's machine and in production/staging to setup your waffle flags!

Note: this could also be applied to switches and samples!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment