[Vtigercrm-commits] [vtiger-commits] r5141 - in /vtigercrm/trunk/modules/Migration: Migration.php MigrationCheck.php ModifyDatabase/42P2_to_50Alpha.php

vtigercrm-commits at vtiger.fosslabs.com vtigercrm-commits at vtiger.fosslabs.com
Sat Apr 15 10:26:09 EDT 2006


Author: saraj
Date: Sat Apr 15 08:26:03 2006
New Revision: 5141

Log:
* Modified to handle when dump apply process failed and some more fine tuning

Modified:
    vtigercrm/trunk/modules/Migration/Migration.php
    vtigercrm/trunk/modules/Migration/MigrationCheck.php
    vtigercrm/trunk/modules/Migration/ModifyDatabase/42P2_to_50Alpha.php

Modified: vtigercrm/trunk/modules/Migration/Migration.php
==============================================================================
--- vtigercrm/trunk/modules/Migration/Migration.php (original)
+++ vtigercrm/trunk/modules/Migration/Migration.php Sat Apr 15 08:26:03 2006
@@ -74,6 +74,8 @@
 		{
 			$password_str = '';
 		}
+
+		//This if is only for windows environment where we cannot access mysql from vtiger root directory
 		if($_SESSION['windows_mysql_path'] != '')
 		{
 			$current_working_dir = getcwd();
@@ -92,10 +94,12 @@
 		}
 		else
 		{
+			exec("echo 'set FOREIGN_KEY_CHECKS = 0;' > ".$dump_filename);
 			exec("mysqldump -u".$mysql_username." -h ".$host_name.$password_str." --port=".$mysql_port." ".$dbname." >> ".$dump_filename);
-		}
-
-		echo '<br> Old Database Dump has been taken and the file is ==> '.$dump_filename;
+			exec("echo 'set FOREIGN_KEY_CHECKS = 1;' >> ".$dump_filename);
+		}
+
+		echo '<br> <b>'.$dbname.'</b> Database Dump has been taken and the file is ==> '.$dump_filename;
 
 		return $dump_filename;
 	}
@@ -106,7 +110,7 @@
 		$sql = "drop database ".$dbname;
 		$conn->query($sql);
 
-		echo '<br> Current Database has been dropped.';
+		echo '<br> <b>'.$dbname.'</b> Database has been dropped.';
 	}
 	function createDatabase($conn,$dbname)
 	{
@@ -114,7 +118,7 @@
 		$sql = "create database ".$dbname;
 		$conn->query($sql);
 
-		echo '<br> Current Database has been created.';
+		echo '<br> <b>'.$dbname.'</b> Database has been created.';
 
 		//Added to avoid the No Database Selected error when execute the queries
 		$conn->connect();
@@ -122,14 +126,17 @@
 
 	function applyDumpData($host_name,$mysql_port,$mysql_username,$mysql_password,$dbname,$dumpfile)
 	{
+		$this->conn->println("Inside the function appluDumpData.");
 		if($mysql_password != '')
 		{
-			$password_str = " --password".$mysql_password;
+			$password_str = " --password=".$mysql_password;
 		}
 		else
 		{
 			$password_str = '';
 		}
+
+		//This if is only for windows environment where we cannot access mysql from vtiger root directory
 		if($_SESSION['windows_mysql_path'] != '')
 		{
 			$current_working_dir = getcwd();
@@ -148,7 +155,7 @@
 			exec("mysql --user=".$mysql_username." -h ".$host_name." --force --port=".$mysql_port.$password_str." ".$dbname." < ".$dumpfile);
 		}
 
-		echo '<br> Old Database Dump has been applied to the Current Database.';
+		echo '<br> Database Dump has been applied to the <b>'.$dbname.'</b> Database from <b>'.$dumpfile.'</b>';
 	}
 
 
@@ -163,8 +170,29 @@
 		return $tabid;
 	}
 
+	function getTablesCountInNewDatabase()
+	{
+		$this->conn->println("Inside the function getTablesCountInNewDatabase");
+		$newconn = @mysql_connect($this->new_hostname.':'.$this->new_mysql_port,$this->new_mysql_username,$this->new_mysql_password);
+		$tables = @mysql_num_rows(mysql_list_tables($this->new_dbname,$newconn));
+
+		$this->conn->println("Number of Tables in New Database = ".$tables);
+		return $tables;
+	}
+
+	function getTablesCountInOldDatabase()
+	{
+		$this->conn->println("Inside the function getTablesCountInOldDatabase");
+		$oldconn = @mysql_connect($this->old_hostname.':'.$this->old_mysql_port,$this->old_mysql_username,$this->old_mysql_password);
+		$tables = @mysql_num_rows(mysql_list_tables($this->old_dbname,$oldconn));
+
+		$this->conn->println("Number of Tables in Old Database = ".$tables);
+		return $tables;
+	}
+
 	function modifyDatabase($conn)
 	{
+		$this->conn->println("Inside the function modifyDatabase");
 		echo '<br><br>Note : Please note that for each query the "Object" string will be displayed at the starting of the line if the query executed successfully. If the query fails then "Object" will not be displayed. we can find out the failed queries based on these Object display.';
 		$conn->println("\n\n\nMickie ---- Starts");
 
@@ -208,6 +236,16 @@
 		$this->conn->println("Going to take the old Database Dump.");
 		$dump_file = $this->takeDatabaseDump($old_host_name,$old_mysql_port,$old_mysql_username,$old_mysql_password,$old_dbname);
 
+		//if old db and new db are different then take new db dump
+		if($old_dbname != $new_dbname)
+		{
+			//This is to take dump of the new database for backup purpose
+			$this->conn->println("Going to take the current Database Dump.");
+			$new_dump_file = $this->takeDatabaseDump($new_host_name,$new_mysql_port,$new_mysql_username,$new_mysql_password,$new_dbname);
+		}
+
+
+		$continue_process = 1;
 		if($same_databases == 1)
 		{
 			echo '<br> Same databases are used. so skip the process of drop and crate the current database.';
@@ -226,14 +264,51 @@
 			//Apply the dump of the old database to the current database
 			$this->conn->println("Going to apply the old database dump to the new database.");
 			$this->applyDumpData($new_host_name,$new_mysql_port,$new_mysql_username,$new_mysql_password,$new_dbname,$dump_file);
-		}
-
-		//Modify the database which is now as old database setup
-		$this->conn->println("Going to modify the current database which is now as old database setup");
-		$this->modifyDatabase($conn);
+
+			//get the number of tables in new database 
+			$new_tables_count = $this->getTablesCountInNewDatabase();
+
+			//get the number of tables in old database 
+			$old_tables_count = $this->getTablesCountInOldDatabase();
+
+			//if tables are missing after apply the dump, then alert the user and quit
+			if($new_tables_count != $old_tables_count)
+			{
+				$this->conn->println("New Database tables not equal to Old Database tables count. Reload the current database again and quit.");
+				
+				$continue_process = 0;
+				$msg = "The dump may not be applied correctly. Tables exist in 4.2.3 database : $old_tables_count. Tables exist in current database after apply the dump : $new_tables_count";
+			?>
+				<script language="javascript">
+					alert("<?php echo $msg; ?>");
+				</script>
+			<?php
+			}
+		}
+
+		if($continue_process == 1)
+		{
+			//Modify the database which is now as old database setup
+			$this->conn->println("Going to modify the current database which is now as old database setup");
+			$this->modifyDatabase($conn);
 		
-		$this->conn->println("Database Modifications Ends......");
-		$this->conn->println("Database Migration from Old Database to the Current Database has been Finished.");
+			$this->conn->println("Database Modifications Ends......");
+			$this->conn->println("Database Migration from Old Database to the Current Database has been Finished.");
+		}
+		else
+		{
+			//Drop the current(latest) Database
+			$this->conn->println("Going to Drop the current Database");
+			$this->dropDatabase($conn,$new_dbname);
+
+			//Create the new current(latest) Database
+			$this->conn->println("Going to Create the current Database");
+			$this->createDatabase($conn,$new_dbname);
+
+			//Reload the new db dump and quit
+			$this->conn->println("Going to apply the new backup db dump");
+			$this->applyDumpData($new_host_name,$new_mysql_port,$new_mysql_username,$new_mysql_password,$new_dbname,$new_dump_file);
+		}
 	}
 
 }

Modified: vtigercrm/trunk/modules/Migration/MigrationCheck.php
==============================================================================
--- vtigercrm/trunk/modules/Migration/MigrationCheck.php (original)
+++ vtigercrm/trunk/modules/Migration/MigrationCheck.php Sat Apr 15 08:26:03 2006
@@ -37,17 +37,17 @@
 
 if(!$oldconn)
 {
-	echo '<br> Source Database Server can not be connected';
+	echo '<br><br><br>  Source Database Server cannot be connected';
 	$continue1 = 0;
 }
 elseif(!$newconn)
 {
-	echo '<br> New Database Server can not be connected';
+	echo '<br><br><br>  Current working Database Server cannot be connected';
 	$continue1 = 0;
 }
 else
 {
-	echo '<br> Database Servers can be conneted';
+	echo '<br><br><br> Database Servers can be connected. Can proceed with migration';
 	$continue1 = 1;
 }
 
@@ -58,7 +58,7 @@
 	//$newdb_exist = @mysql_select_db($new_dbname,$oldconn);
 	if(!$olddb_exist)
 	{
-		echo '<br> Source Database is not exist';
+		echo '<br> Source Database does not exist';
 		$continue2 = 0;
 	}
 	//elseif(!$newdb_exist)
@@ -68,7 +68,7 @@
 	//}
 	else
 	{
-		echo '<br> Databases are exist';
+		echo '<br> Required databases exist';
 		$continue2 = 1;
 	}
 }
@@ -81,7 +81,7 @@
 
 	if(!$old_tables)
 	{
-		echo '<br> Tables are not exist in Source Database';
+		echo '<br> Tables do not exist in the Source Database';
 		$continue3 = 0;
 	}
 /*	if(!$new_tables)
@@ -92,7 +92,7 @@
 */
 	else
 	{
-		echo '<br> Tables are exist in the Database';
+		echo '<br> Tables exist in both the Databases';
 		$continue3 = 1;
 	}
 }
@@ -104,7 +104,7 @@
 
 	if($old_host_name == $new_host[0] && $old_mysql_port == $new_host[1] && $old_mysql_username == $new_mysql_username && $old_mysql_password == $new_mysql_password && $old_dbname == $new_dbname)
 	{
-		echo '<br> Two databases are same.';
+		echo '<br> Both the databases are the same.';
 		$continue4 = 1;//change the value to 0 if you don't want to proceed with the same database
 		$same_databases = 1;
 	}
@@ -133,6 +133,9 @@
 		<br> MySql Password : '.$old_mysql_password.'
 		<br> DB Name : '.$old_dbname;
 	echo '<br>*************************************************************';
+	echo '';
+	echo '';
+	echo '<br>*************************************************************';
 	echo '<br><b>Current Database Parameters : </b>
 		<br> Host Name : '.$new_host[0].'
 		<br> MySql Port : '.$new_host[1].'
@@ -141,21 +144,16 @@
 		<br> DB Name : '.$new_dbname;
 	echo '<br>*************************************************************';
 
-	//echo '<br>================'.$old_version.'==================';
 
 	$conn = new PearDatabase("mysql",$new_host_name,$new_dbname,$new_mysql_username,$new_mysql_password);
 	$conn->connect();
 	if($conn)
 	{
-		//$filename = 'migration_'.$old_version.'_to_'.$latest_version.'.php';
-	        //include("migration/$filename");
-
 		include("modules/Migration/Migration.php");
 		$obj = new Migration('',$conn);
 		$obj->setOldDatabaseParams($old_host_name,$old_mysql_port,$old_mysql_username,$old_mysql_password,$old_dbname);
 		$obj->setNewDatabaseParams($new_host[0],$new_host[1],$new_mysql_username,$new_mysql_password,$new_dbname);
 		$obj->migrate($same_databases);
-		//echo '<pre>';print_r($obj);echo '</pre>';
 	}
 	else
 	{
@@ -165,7 +163,7 @@
 }
 else
 {
-	echo '<br>Please check the values.';
+	echo '<br>ERROR!!!!!!Please check the input values, unable to proceed.';
 	include("modules/Migration/MigrationStep1.php");
 }
 

Modified: vtigercrm/trunk/modules/Migration/ModifyDatabase/42P2_to_50Alpha.php
==============================================================================
--- vtigercrm/trunk/modules/Migration/ModifyDatabase/42P2_to_50Alpha.php (original)
+++ vtigercrm/trunk/modules/Migration/ModifyDatabase/42P2_to_50Alpha.php Sat Apr 15 08:26:03 2006
@@ -18,8 +18,8 @@
 echo "<br><br><b>Database Modifications for 4.2 Patch2 ==> 5.0(Alpha) Dev 3 Starts here.....</b><br>";
 
 /****************** 5.0(Alpha) dev version 1 Database changes -- Starts*********************/
-//$list =  "<table border=1>";
-//$list .= '<tr width="100%"><td width="20%"><b> Status Object </b></td><td width="10%">Suceess/Failure</td><td width="80%"> Query</td></tr>';
+echo "<table border=1>";
+echo '<tr width="100%"><td width="20%"><b> Status Object </b></td><td width="10%">Suceess/Failure</td><td width="80%"> Query</td></tr>';
 
 //Added the announcement table creation to avoid the error
 $ann_query = "CREATE TABLE `announcement` (
@@ -420,7 +420,7 @@
 	$name = $conn->query_result($res,$i,'name');
 	$role_map_array[$roleid] = $name;
 }
-echo '<pre> List of role :';print_r($role_map_array);echo '</pre>';
+//echo '<pre> List of roles :';print_r($role_map_array);echo '</pre>';
 
 //Before delete the role take a backup array for the table user2role
 $sql = "select * from user2role";
@@ -432,7 +432,7 @@
 	$roleid = $conn->query_result($res,$i,'roleid');
 	$user2role_array[$userid] = $roleid;
 }
-echo '<pre> List of user2role : (userid => roleid)';print_r($user2role_array);echo '</pre>';
+//echo '<pre> List of user2role : (userid => roleid)';print_r($user2role_array);echo '</pre>';
 
 //Delete the role entries
 $sql = "truncate role";
@@ -530,7 +530,7 @@
 	$desc = $conn->query_result($res,$i,'description');
 	$group_map_array[$name] = $desc;
 }
-echo '<pre>List of Groups : ';print_r($group_map_array);echo '</pre>';
+//echo '<pre>List of Groups : ';print_r($group_map_array);echo '</pre>';
 
 
 //Step 2 : form an users2group_map_array array as userid => groupname from users2group table
@@ -543,7 +543,7 @@
 	$userid = $conn->query_result($res,$i,'userid');
 	$users2group_map_array[$userid] = $groupname;
 }
-echo '<pre>List of users2group : ';print_r($users2group_map_array);echo '</pre>';
+//echo '<pre>List of users2group : ';print_r($users2group_map_array);echo '</pre>';
 
 //Step 3 : delete all entries from groups table
 $sql = "truncate groups";
@@ -766,7 +766,7 @@
 
 
 $conn->println("Database Modifications for 5.0(Alpha) Dev 3 ==> 5.0 Alpha starts here.");
-echo "<br><br><b>Database Modifications for 5.0(Alpha) Dev3 ==> 5.0 Alpha starts here.....</b><br>";
+//echo "<br><br><b>Database Modifications for 5.0(Alpha) Dev3 ==> 5.0 Alpha starts here.....</b><br>";
 $alter_query_array6 = Array(
 				"ALTER TABLE users ADD column activity_view VARCHAR(25) DEFAULT 'Today' AFTER homeorder",
 				"ALTER TABLE activity ADD column notime CHAR(3) DEFAULT '0' AFTER location"
@@ -1750,7 +1750,7 @@
 
 
 
-echo "<br><br><b>Database Modifications for Indexing and some missded tables starts here.....</b><br>";
+//echo "<br><br><b>Database Modifications for Indexing and some missded tables starts here.....</b><br>";
 //Added queries which are for indexing and the missing tables - Mickie - on 06-04-2006
 
 $query_array = Array(
@@ -2528,10 +2528,23 @@
 
 
 $conn->println("Database Modifications for 5.0(Alpha) Dev 3 ==> 5.0 Alpha ends here.");
-echo "<br><br><b>Database Modifications for 5.0(Alpha) Dev3 ==> 5.0 Alpha ends here.....</b><br>";
+//echo "<br><br><b>Database Modifications for 5.0(Alpha) Dev3 ==> 5.0 Alpha ends here.....</b><br>";
 
 $conn->println("Database Modifications for 4.2 Patch2 ==> 5.0(Alpha) Dev 3 ends here.");
-echo "<br><br><b>Database Modifications for 4.2 Patch2 ==> 5.0(Alpha) Ends here.....</b><br>";
+//echo "<br><br><b>Database Modifications for 4.2 Patch2 ==> 5.0(Alpha) Ends here.....</b><br>";
+
+echo '</table>';
+echo "<br><b>Database Modifications for 4.2 Patch2 ==> 5.0(Alpha) Ends here.....</b><br>";
+echo '<br><b>Migration has been successfully completed. Data has been moved from your old vtiger to Latest vtigerCRM.';
+echo '<br>Please note down all the failed queries or please copy the whole table and save. This may be useful in future.</b>';
+echo '<br><br><br>';
+
+echo '<div align="center"><a href="index.php"><b> Home </b></a></div>';
+echo '<br><br><br>';
+
+
+
+
 
 
 
@@ -2541,17 +2554,28 @@
 	$status = $conn->query($query);
 	if(is_object($status))
 	{
-		//$list .= '<tr width="100%"><td width="20%">'.$status.'</td><td width="10%"><font color="green"> S </font></td><td width="80%">'.$query.'</td></tr>';
-		echo '<br>'.$status.' ==> '.$query;
+		echo '
+			<tr width="100%">
+				<td width="25%" nowrap>'.$status.'</td>
+				<td width="5%"><font color="green"> S </font></td>
+				<td width="70%">'.$query.'</td>
+			</tr>';
+		//echo '<br>'.$status.' ==> '.$query;
 		$success_query_array[] = $query;
 	}
 	else
 	{
-		//$list .= '<tr width="100%"><td width="20%">'.$status.'</td><td width="10%"><font color="red"> F </font></td><td width="80%">'.$query.'</td></tr>';
-		echo '<br><br>'.$status.' ======>  '.$query;
+		echo '
+			<tr width="100%">
+				<td width="25%">'.$status.'</td>
+				<td width="5%"><font color="red"><b> F </b></font></td>
+				<td width="70%">'.$query.'</td>
+			</tr>';
+		//echo '<br><br>'.$status.' ======>  '.$query;
 		$failure_query_array[] = $query;
 	}
 }
+
 //Added on 23-12-2005 which is used to populate the profile2field and def_org_field table entries for the field per tab
 //if we enter a field in field table then we must populate that field in these table for security access
 function populateFieldForSecurity($tabid,$fieldid)
@@ -2569,8 +2593,6 @@
 	$def_query = "insert into def_org_field values (".$tabid.",".$fieldid.",0,1)";
 	Execute($def_query);
 }
-//$list .= '</table>';
-//echo $list;
 
 
 ?>





More information about the vtigercrm-commits mailing list