From 912db9910faa76d043cf03f0a7f24ff84f432644 Mon Sep 17 00:00:00 2001
From: Brandon DeRosier <bdero@mit.edu>
Date: Fri, 7 Aug 2015 16:56:02 -0400
Subject: [PATCH] Fix CCX 0002 migration

(cherry picked from commit 558b93fdeaaf72d02e050808698b2015e123a01c)
---
 lms/djangoapps/ccx/migrations/0002_convert_memberships.py | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/lms/djangoapps/ccx/migrations/0002_convert_memberships.py b/lms/djangoapps/ccx/migrations/0002_convert_memberships.py
index 3030c46..4f595eb 100644
--- a/lms/djangoapps/ccx/migrations/0002_convert_memberships.py
+++ b/lms/djangoapps/ccx/migrations/0002_convert_memberships.py
@@ -3,6 +3,7 @@ from south.utils import datetime_utils as datetime
 from south.db import db
 from south.v2 import DataMigration
 from django.db import models
+from opaque_keys import InvalidKeyError
 
 class Migration(DataMigration):
 
@@ -12,11 +13,14 @@ class Migration(DataMigration):
         memberships = orm['ccx.CcxMembership'].objects.select_related('ccx', 'student').all()
         for membership in memberships:
             ccx = membership.ccx
-            course_key = CCXLocator.from_course_locator(ccx.course_id, ccx.id)
-            enrollment, created = orm['student.CourseEnrollment'].objects.get_or_create(
+            try:
+                course_key = CCXLocator.from_course_locator(ccx.course_id, ccx.id)
+                enrollment, created = orm['student.CourseEnrollment'].objects.get_or_create(
                     user=membership.student,
                     course_id=course_key,
-            )
+                )
+            except InvalidKeyError:
+                membership.delete()
 
 
     def backwards(self, orm):
--
libgit2 0.26.0