Fix: Add foreign keys directives

This commit is contained in:
Laurent Destailleur 2011-10-15 20:45:16 +02:00
parent 85be289842
commit 920336b895

View File

@ -185,6 +185,7 @@ if ($what == 'mysql')
dol_syslog("Failed to open file ".$outputfile,LOG_ERR); dol_syslog("Failed to open file ".$outputfile,LOG_ERR);
$errormsg=$langs->trans("ErrorFailedToWriteInDir"); $errormsg=$langs->trans("ErrorFailedToWriteInDir");
} }
// Get errorstring // Get errorstring
if ($compression == 'none') $handle = fopen($outputfile, 'r'); if ($compression == 'none') $handle = fopen($outputfile, 'r');
if ($compression == 'gz') $handle = gzopen($outputfile, 'r'); if ($compression == 'gz') $handle = gzopen($outputfile, 'r');
@ -351,8 +352,6 @@ $db->close();
* @param string $outputfile Output file name * @param string $outputfile Output file name
* @param string $tables Table name or '*' for all * @param string $tables Table name or '*' for all
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*
* FIXME Must add directives to have restore working even with constraints not loaded into correct order, etc...
*/ */
function backup_tables($outputfile, $tables='*') function backup_tables($outputfile, $tables='*')
{ {
@ -387,45 +386,77 @@ function backup_tables($outputfile, $tables='*')
$errormsg=$langs->trans("ErrorFailedToWriteInDir"); $errormsg=$langs->trans("ErrorFailedToWriteInDir");
return -1; return -1;
} }
// Print headers and global mysql config vars
$sqlhead = '';
$sqlhead .= "-- ".$db->label." dump via php
--
-- Host: ".$db->db->host_info." Database: ".$db->database_name."
-- ------------------------------------------------------
-- Server version ".$db->db->server_info."
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
";
fwrite($handle, $sqlhead);
// Process each table and print their definition + their datas
foreach($tables as $table) foreach($tables as $table)
{ {
// Saving the table structure
fwrite($handle, "--\n-- Table structure for table `".$table."`\n--\n\n");
fwrite($handle,"DROP TABLE IF EXISTS `".$table."`;\n");
fwrite($handle,"/*!40101 SET @saved_cs_client = @@character_set_client */;\n");
fwrite($handle,"/*!40101 SET character_set_client = utf8 */;\n");
$resqldrop=$db->query('SHOW CREATE TABLE '.$table);
$row2 = $db->fetch_row($resqldrop);
fwrite($handle,$row2[1].";\n");
fwrite($handle,"/*!40101 SET character_set_client = @saved_cs_client */;\n\n");
// Dumping the data (locking the table and disabling the keys check while doing the process)
fwrite($handle, "--\n-- Dumping data for table `".$table."`\n--\n\n");
fwrite($handle, "LOCK TABLES `".$table."` WRITE;\n");
fwrite($handle, "/*!40000 ALTER TABLE `".$table."` DISABLE KEYS */;\n");
$sql='SELECT * FROM '.$table; $sql='SELECT * FROM '.$table;
$result = $db->query($sql); $result = $db->query($sql);
$num_fields = $db->num_rows($result); $num_fields = $db->num_rows($result);
while($row = $db->fetch_row($result)) {
fwrite($handle,'DROP TABLE IF EXISTS '.$table); // For each row of data we print a line of INSERT
$resqldrop=$db->query('SHOW CREATE TABLE '.$table); fwrite($handle,'INSERT INTO `'.$table.'` VALUES (');
$row2 = $db->fetch_row($resqldrop);
fwrite($handle,"\n\n".$row2[1].";\n\n");
while($row = $db->fetch_row($result))
{
fwrite($handle,'INSERT INTO '.$table.' VALUES(');
$columns = count($row); $columns = count($row);
for($j=0; $j<$columns; $j++) $rowsarr = array();
{ for($j=0; $j<$columns; $j++) {
// Processing each columns of the row to ensure that we correctly save the value (eg: add quotes for string - in fact we add quotes for everything, it's easier)
if ($row[$j] == null and !is_string($row[$j])) {
// IMPORTANT: if the field is NULL we set it NULL
$row[$j] = 'NULL';
} elseif(is_string($row[$j]) and $row[$j] == '') {
// if it's an empty string, we set it as an empty string
$row[$j] = "''";
} elseif(is_numeric($row[$j])) {
// if it's a number, we return it as-is
$row[$j] = $row[$j];
} else { // else for all other cases we escape the value and put quotes around
$row[$j] = addslashes($row[$j]); $row[$j] = addslashes($row[$j]);
$row[$j] = preg_replace("#\n#", "\\n", $row[$j]); $row[$j] = preg_replace("#\n#", "\\n", $row[$j]);
if (isset($row[$j])) { fwrite($handle,'"'.$row[$j].'"'); } else { fwrite($handle,'""'); } $row[$j] = "'".$row[$j]."'";
if (isset($row[$j]))
{
if ($row[$j] == null)
{
fwrite($handle,'"null"');
}
else
{
fwrite($handle,'"'.$row[$j].'"');
} }
} }
else fwrite($handle,implode(',', $row).");\n");
{
fwrite($handle,'""');
}
if ($j<($num_fields-1)) { fwrite($handle,','); }
}
fwrite($handle,");\n");
} }
fwrite($handle, "/*!40000 ALTER TABLE `".$table."` ENABLE KEYS */;\n"); // Enabling back the keys/index checking
fwrite($handle, "UNLOCK TABLES;\n"); // Unlocking the tables
fwrite($handle,"\n\n\n"); fwrite($handle,"\n\n\n");
} }
@ -448,6 +479,22 @@ function backup_tables($outputfile, $tables='*')
*/ */
/* Backup Procedure structure*/ /* Backup Procedure structure*/
// Write the footer (restore the previous database settings)
$sqlfooter='';
$sqlfooter.="
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on ".date('Y-m-d G-i-s');
fwrite($handle, $sqlfooter);
fclose($handle); fclose($handle);
return 1; return 1;