Fix: Migration error

This commit is contained in:
Laurent Destailleur 2012-10-17 14:15:11 +02:00
parent 0dffe08c07
commit c56e3b5c90

View File

@ -180,68 +180,75 @@ if (! GETPOST("action") || preg_match('/upgrade/i',GETPOST('action')))
*/ */
if ($ok) if ($ok)
{ {
$couples=array(); $result = $db->DDLDescTable(MAIN_DB_PREFIX."categorie_association");
$filles=array(); $obj = $db->fetch_object($result);
$sql = "SELECT fk_categorie_mere, fk_categorie_fille"; if ($obj) // It table categorie_association exists
$sql.= " FROM ".MAIN_DB_PREFIX."categorie_association";
dolibarr_install_syslog("upgrade: search duplicate sql=".$sql);
$resql = $db->query($sql);
if ($resql)
{ {
$num=$db->num_rows($resql); $couples=array();
while ($obj=$db->fetch_object($resql)) $filles=array();
{ $sql = "SELECT fk_categorie_mere, fk_categorie_fille";
if (! isset($filles[$obj->fk_categorie_fille])) // Only one record as child (a child has only on parent). $sql.= " FROM ".MAIN_DB_PREFIX."categorie_association";
{ dolibarr_install_syslog("upgrade: search duplicate sql=".$sql);
if ($obj->fk_categorie_mere != $obj->fk_categorie_fille) $resql = $db->query($sql);
{ if ($resql)
$filles[$obj->fk_categorie_fille]=1; // Set record for this child {
$couples[$obj->fk_categorie_mere.'_'.$obj->fk_categorie_fille]=array('mere'=>$obj->fk_categorie_mere, 'fille'=>$obj->fk_categorie_fille); $num=$db->num_rows($resql);
} while ($obj=$db->fetch_object($resql))
} {
} if (! isset($filles[$obj->fk_categorie_fille])) // Only one record as child (a child has only on parent).
{
if ($obj->fk_categorie_mere != $obj->fk_categorie_fille)
{
$filles[$obj->fk_categorie_fille]=1; // Set record for this child
$couples[$obj->fk_categorie_mere.'_'.$obj->fk_categorie_fille]=array('mere'=>$obj->fk_categorie_mere, 'fille'=>$obj->fk_categorie_fille);
}
}
}
dolibarr_install_syslog("upgrade: result is num=".$num." count(couples)=".count($couples)); dolibarr_install_syslog("upgrade: result is num=".$num." count(couples)=".count($couples));
// If there is duplicates couples or child with two parents // If there is duplicates couples or child with two parents
if (count($couples) > 0 && $num > count($couples)) if (count($couples) > 0 && $num > count($couples))
{ {
$error=0; $error=0;
$db->begin(); $db->begin();
$sql="DELETE FROM ".MAIN_DB_PREFIX."categorie_association"; // We delete all
dolibarr_install_syslog("upgrade: delete association sql=".$sql); $sql="DELETE FROM ".MAIN_DB_PREFIX."categorie_association";
$resqld=$db->query($sql); dolibarr_install_syslog("upgrade: delete association sql=".$sql);
if ($resqld) $resqld=$db->query($sql);
{ if ($resqld)
foreach($couples as $key => $val) {
{ // And we insert only each record once
$sql ="INSERT INTO ".MAIN_DB_PREFIX."categorie_association(fk_categorie_mere,fk_categorie_fille)"; foreach($couples as $key => $val)
$sql.=" VALUES(".$val['mere'].", ".$val['fille'].")"; {
dolibarr_install_syslog("upgrade: insert association sql=".$sql); $sql ="INSERT INTO ".MAIN_DB_PREFIX."categorie_association(fk_categorie_mere,fk_categorie_fille)";
$resqli=$db->query($sql); $sql.=" VALUES(".$val['mere'].", ".$val['fille'].")";
if (! $resqli) $error++; dolibarr_install_syslog("upgrade: insert association sql=".$sql);
} $resqli=$db->query($sql);
} if (! $resqli) $error++;
}
}
if (! $error) if (! $error)
{ {
print '<tr><td>'.$langs->trans("RemoveDuplicates").'</td>'; print '<tr><td>'.$langs->trans("RemoveDuplicates").'</td>';
print '<td align="right">'.$langs->trans("Success").' ('.$num.'=>'.count($couples).')</td></tr>'; print '<td align="right">'.$langs->trans("Success").' ('.$num.'=>'.count($couples).')</td></tr>';
$db->commit(); $db->commit();
} }
else else
{ {
print '<tr><td>'.$langs->trans("RemoveDuplicates").'</td>'; print '<tr><td>'.$langs->trans("RemoveDuplicates").'</td>';
print '<td align="right">'.$langs->trans("Failed").'</td></tr>'; print '<td align="right">'.$langs->trans("Failed").'</td></tr>';
$db->rollback(); $db->rollback();
} }
} }
} }
else else
{ {
print '<div class="error">'.$langs->trans("Error").'</div>'; print '<div class="error">'.$langs->trans("Error").' '.$db->lasterror().'</div>';
}
} }
} }