diff mbox

[1/3] migrations: Use a data migration to load initial states and tags

Message ID 1440087229-17618-2-git-send-email-damien.lespiau@intel.com
State Superseded
Headers show

Commit Message

Damien Lespiau Aug. 20, 2015, 4:13 p.m. UTC
django 1.9 is going to remove initial data through fixtures:

  If an application uses migrations, there is no automatic loading of fixtures.
  Since migrations will be required for applications in Django 1.9, this
  behavior is considered deprecated. If you want to load initial data for an
  app, consider doing it in a data migration.

  Source: https://docs.djangoproject.com/en/1.8/howto/initial-data/

So, for convenience (to not have to load fixtures by hand), load the
content of the state and tag tables as a migration step. We can then
remove a step in the install process: the initial data will populated by
the migration system (syncdb is running that under the hood).

Signed-off-by: Damien Lespiau <damien.lespiau@intel.com>
---
 docs/INSTALL                                   |  5 ----
 patchwork/migrations/0002_load_initial_data.py | 37 ++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 5 deletions(-)
 create mode 100644 patchwork/migrations/0002_load_initial_data.py
diff mbox

Patch

diff --git a/docs/INSTALL b/docs/INSTALL
index bd95770..1a5d777 100644
--- a/docs/INSTALL
+++ b/docs/INSTALL
@@ -148,11 +148,6 @@  in brackets):
 
      PYTHONPATH=lib/python ./manage.py collectstatic
 
-    You'll also need to load the initial tags and states into the
-    patchwork database:
-
-     PYTHONPATH=lib/python ./manage.py loaddata default_tags default_states
-
     Finally, add privileges for your mail and web users. This is only needed if
     you use the ident-based approach. If you use password-based database
     authentication, you can skip this step.
diff --git a/patchwork/migrations/0002_load_initial_data.py b/patchwork/migrations/0002_load_initial_data.py
new file mode 100644
index 0000000..118bee1
--- /dev/null
+++ b/patchwork/migrations/0002_load_initial_data.py
@@ -0,0 +1,37 @@ 
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+from django.core.management import call_command
+
+
+# https://docs.djangoproject.com/en/1.8/topics/migrations/#data-migrations
+
+def load_fixture(name):
+    call_command('loaddata', name, app_label='patchwork', verbosity=0,
+                 interactive=False)
+
+def load_states(apps, schema_editor):
+    load_fixture('default_states')
+
+def load_tags(apps, schema_editor):
+    load_fixture('default_tags')
+
+def reset_states(apps, schema_editor):
+    State = apps.get_model('patchwork', 'State')
+    State.objects.all().delete()
+
+def reset_tags(apps, schema_editor):
+    Tag = apps.get_model('patchwork', 'Tag')
+    Tag.objects.all().delete()
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('patchwork', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.RunPython(load_states, reset_states),
+        migrations.RunPython(load_tags, reset_tags),
+    ]