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"""
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))
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)