Fixed an issue where orphaned comments breaks the import
authorJoar Wandborg <git@wandborg.com>
Mon, 2 Apr 2012 11:44:28 +0000 (13:44 +0200)
committerJoar Wandborg <git@wandborg.com>
Mon, 2 Apr 2012 11:44:28 +0000 (13:44 +0200)
mediagoblin/db/sql/convert.py

index 97f29bfcc8101a4e94f60eb1d384aed1bfe148ae..ec308f6cd2964deea7f68ac6752d4b7c34384121 100644 (file)
@@ -31,6 +31,7 @@ from mediagoblin.db.mongo.open import setup_connection_and_db_from_config as \
 
 obj_id_table = dict()
 
+
 def add_obj_ids(entry, new_entry):
     global obj_id_table
     print "%r -> %r" % (entry._id, new_entry.id)
@@ -42,6 +43,7 @@ def copy_attrs(entry, new_entry, attr_list):
         val = entry[a]
         setattr(new_entry, a, val)
 
+
 def copy_reference_attr(entry, new_entry, ref_attr):
     val = entry[ref_attr]
     val = obj_id_table[val]
@@ -174,12 +176,16 @@ def convert_media_comments(mk_db):
         copy_attrs(entry, new_entry,
             ('created',
              'content',))
-        copy_reference_attr(entry, new_entry, "media_entry")
-        copy_reference_attr(entry, new_entry, "author")
 
-        session.add(new_entry)
-        session.flush()
-        add_obj_ids(entry, new_entry)
+        try:
+            copy_reference_attr(entry, new_entry, "media_entry")
+            copy_reference_attr(entry, new_entry, "author")
+        except KeyError as e:
+            print('KeyError in convert_media_comments(): {0}'.format(e))
+        else:
+            session.add(new_entry)
+            session.flush()
+            add_obj_ids(entry, new_entry)
 
     session.commit()
     session.close()