]> git.parisson.com Git - telemeta-data.git/commitdiff
migration: do not import collections that trigger a dup entry exception instead of...
authorolivier <olivier@3bf09e05-f825-4182-b9bc-eedd7160adf0>
Wed, 22 Apr 2009 18:29:05 +0000 (18:29 +0000)
committerolivier <olivier@3bf09e05-f825-4182-b9bc-eedd7160adf0>
Wed, 22 Apr 2009 18:29:05 +0000 (18:29 +0000)
git-svn-id: http://svn.parisson.org/svn/crem@83 3bf09e05-f825-4182-b9bc-eedd7160adf0

trunk/import/migration/tasks/collections.py

index eade0d8d13b4e1f209f9f7b2ae5ab8d618f19273..2aa5bd35e37187d2dd730bce8c2bed76239ed7e0 100644 (file)
@@ -34,6 +34,8 @@
 from telemeta.core import *
 from api import IDataMigrator
 from core import DataMigrator
+from _mysql_exceptions import IntegrityError
+from MySQLdb.constants.ER import DUP_ENTRY
 
 class CollectionsMigrator(DataMigrator):
     """Perform a preliminary raw copy of the collection table"""
@@ -98,7 +100,7 @@ class CollectionsMigrator(DataMigrator):
             else:
                 enum_table = target_base + 's'
 
-            subquery = "COALESCE((SELECT id FROM `%s`.`%s` AS e WHERE %s.`%s` = e.value), -1)" % (
+            subquery = "(SELECT id FROM `%s`.`%s` AS e WHERE %s.`%s` = e.value)" % (
                        self.target_db_name, enum_table, src_table, src_field)
             
             assign.append((target_field, subquery))
@@ -118,8 +120,27 @@ class CollectionsMigrator(DataMigrator):
         target_fields  += [str(a[0]) for a in enum]
         src_fields     += [str(a[1]) for a in enum]
 
-        query = "INSERT INTO %s.media_collections (\n  %s\n)\nSELECT \n  %s\n FROM %s.Support AS s" % (
-                self.target_db_name, ",\n  ".join(target_fields), ",\n  ".join(src_fields),
-                self.src_db_name)
+        self.src_cursor.execute("SELECT COUNT(*) FROM %s.Support" %  self.src_db_name)
+        count = self.src_cursor.fetchone()[0]
+
+        for offset in range(0, count):
+            query = "INSERT INTO %s.media_collections (\n  %s\n)\n" \
+                    "SELECT \n  %s\n FROM %s.Support AS s LIMIT %d, 1" % (
+                        self.target_db_name, 
+                        ",\n  ".join(target_fields), 
+                        ",\n  ".join(src_fields),
+                        self.src_db_name,
+                        offset)
+
+            try:
+                self.target_cursor.execute(query)
+            except IntegrityError, e:
+                (errno, errmsg) = e
+                if errno == DUP_ENTRY:
+                    self.src_cursor.execute("SELECT Cote FROM %s.Support LIMIT %d, 1" % (self.src_db_name, offset))
+                    id = self.src_cursor.fetchone()[0]
+                    print "Collection %s not imported: %s" % (id, errmsg)
+                else:
+                    raise e
+
 
-        self.target_cursor.execute(query)