From 04420495314563744bca66fcc7619f7360a281cb Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 20 Apr 2009 22:16:56 +0000 Subject: [PATCH] Fix: Removed duplicate records in categorie_association during migration --- htdocs/install/upgrade.php | 84 ++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 9 deletions(-) diff --git a/htdocs/install/upgrade.php b/htdocs/install/upgrade.php index a8139adf4b1..9d9262bee86 100644 --- a/htdocs/install/upgrade.php +++ b/htdocs/install/upgrade.php @@ -74,7 +74,7 @@ if (! isset($_GET["action"]) || $_GET["action"] == "upgrade") print ''; $error=0; - + // decode database pass if needed if (! empty($dolibarr_main_db_encrypted_pass)) { @@ -112,7 +112,7 @@ if (! isset($_GET["action"]) || $_GET["action"] == "upgrade") print ""; dolibarr_install_syslog("upgrade: Database connection successfull : $dolibarr_main_db_name"); - $ok=1; + $ok=1; } else { @@ -132,11 +132,77 @@ if (! isset($_GET["action"]) || $_GET["action"] == "upgrade") dolibarr_install_syslog("upgrade: ".$langs->transnoentities("ServerVersion")." : $version"); //print ''; } - - // Force l'affichage de la progression - print ''; + + // Force l'affichage de la progression + print ''; flush(); + // Delete duplicates in table categorie_association + $couples=array(); + $filles=array(); + $sql = "SELECT fk_categorie_mere, fk_categorie_fille"; + $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); + 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." sizeof(couples)=".sizeof($couples)); + + // If there is duplicates couples or child with two parents + if (sizeof($couples) > 0 && $num > sizeof($couples)) + { + $error=0; + + $db->begin(); + + $sql="DELETE FROM ".MAIN_DB_PREFIX."categorie_association"; + dolibarr_install_syslog("upgrade: delete association sql=".$sql); + $resqld=$db->query($sql); + if ($resqld) + { + foreach($couples as $key => $val) + { + $sql ="INSERT INTO ".MAIN_DB_PREFIX."categorie_association(fk_categorie_mere,fk_categorie_fille)"; + $sql.=" VALUES(".$val['mere'].", ".$val['fille'].")"; + dolibarr_install_syslog("upgrade: insert association sql=".$sql); + $resqli=$db->query($sql); + if (! $resqli) $error++; + } + } + + if (! $error) + { + print ''; + print ''; + $db->commit(); + } + else + { + print ''; + print ''; + $db->rollback(); + } + } + } + else + { + print '
'.$langs->trans("Error").'
'; + } + + if ($ok) { $versioncommande=split('\.','4.0'); @@ -155,7 +221,7 @@ if (! isset($_GET["action"]) || $_GET["action"] == "upgrade") 'llx_telephonie_contact_facture', 'llx_telephonie_societe_ligne', 'llx_telephonie_tarif_client'); -*/ +*/ $listtables = $db->DDLListTables($conf->db->name,''); foreach ($listtables as $val) { @@ -201,7 +267,7 @@ if (! isset($_GET["action"]) || $_GET["action"] == "upgrade") if ($choix==1) $dir = "../../mysql/migration/"; elseif ($choix==2) $dir = "../../pgsql/migration/"; else $dir = "../../mssql/migration/"; - + $filelist=array(); $i = 0; $ok = 0; @@ -235,9 +301,9 @@ if (! isset($_GET["action"]) || $_GET["action"] == "upgrade") { print ''; - + $name = substr($file, 0, strlen($file) - 4); - + // Run sql script $ok=run_sql($dir.$file, 0); }
"; print $langs->trans("DatabaseConnection")." : ".$dolibarr_main_db_name."".$langs->trans("OK")."
'.join('.',$versionarray).'
'.$langs->trans("PleaseBePatient").'
'.$langs->trans("PleaseBePatient").'
'.$langs->trans("RemoveDuplicates").''.$langs->trans("Success").' ('.$num.'=>'.sizeof($couples).')
'.$langs->trans("RemoveDuplicates").''.$langs->trans("Failed").'
'; print $langs->trans("ChoosedMigrateScript").''.$file.'