Browse Source

[svn r10520] Update to use with 1.8 - second and last step - adding SQL generation helper and comments

Yannick Warnier 18 years ago
parent
commit
a9f1903531
1 changed files with 141 additions and 25 deletions
  1. 141 25
      main/install/compare_db.php

+ 141 - 25
main/install/compare_db.php

@@ -1,4 +1,4 @@
-<?php // $Id: compare_db.php 10519 2006-12-18 17:12:37Z yannoo $
+<?php // $Id: compare_db.php 10520 2006-12-18 23:14:27Z yannoo $
 /*
 ==============================================================================
 	Dokeos - elearning and course management software
@@ -58,22 +58,35 @@ $prefix_old = 'dokeos160_';
 $bases_old=array($prefix_old.'dokeos_main',$prefix_old.'dokeos_stats',$prefix_old.'dokeos_user',$prefix_old.'COURSE',$prefix_old.'dokeos_scorm');
 $db_old = mysql_connect($sql_server_old,$sql_user_old,$sql_pass_old) or die(mysql_error());
 
+$field_details = array(0=>'Field',1=>'Type',2=>'Null',3=>'Key',4=>'Default',5=>'Extra');
+
 /********************************/
 
+$all_db_changes = array();
+
+echo "<h1>Databases structure comparison script</h1>";
+
+//iterate through databases given above (checking from the 'new' database side)
 foreach($bases_new as $num_base=>$base)
 {
+	//init tables lists for this database
 	$modif_tables=array();
 	$tables_db_new=array();
 	$tables_db_old=array();
 	$dump=array();
-
+	
+	//display current processed database
+	echo "<h2>Now analysing differences between databases <em>$base</em> and <em>".$bases_old[$num_base]."</em></h2>";
+	
+	//get a list of tables for this database
 	$query_new="SHOW TABLES FROM ".$bases_new[$num_base];
 	$result_new=mysql_query($query_new,$db_new);
 
-	if($result_new)
+	if($result_new) //if there are tables in this database
 	{
 		$i=0;
 
+		//as there are tables, process them one by one
 		while($row_new=mysql_fetch_row($result_new))
 		{
 			$dump[$i]['table_name']=$row_new[0];
@@ -84,6 +97,7 @@ foreach($bases_new as $num_base=>$base)
 
 			$j=0;
 
+			//get the fields details (numbered fields)
 			while($row_old=mysql_fetch_row($result_old))
 			{
 				$dump[$i]['fields'][$j][0]=$row_old[0];
@@ -92,6 +106,8 @@ foreach($bases_new as $num_base=>$base)
 				$dump[$i]['fields'][$j][3]=$row_old[3];
 				$dump[$i]['fields'][$j][4]=$row_old[4];
 				$dump[$i]['fields'][$j][5]=$row_old[5];
+				//get the field name in one special element of this array
+				$dump[$i]['field_names'][$row_old[0]]=$j;
 
 				$j++;
 			}
@@ -106,37 +122,59 @@ foreach($bases_new as $num_base=>$base)
 
 			if(!$result)
 			{
-				$modif_tables[]=$table['table_name'];
+				$modif_tables[]='**'.$table['table_name'].'**';
 			}
 			else
 			{
 				$i=0;
-
-				if(sizeof($table['fields']) != mysql_num_rows($result))
-				{
-					$modif_tables[]=$table['table_name'];
+				
+				//check for removed, new or modified fields
+				$fields_old = array();
+				$fields_new = array();
+				//list the new fields in a enumeration array
+				foreach($table['field_names'] as $dummy_key=>$dummy_field){
+					$fields_new[] = $dummy_key;
 				}
-				else
-				{
-					while($row=mysql_fetch_row($result))
-					{
-						$field_infos=$table['fields'][$i];
-
-						foreach($row as $key=>$enreg)
+				//list the old fields in an enumeration array and check if their corresponding
+				//field in the new table is different (if any)
+				$modif_fields = array();
+				while($row_old = mysql_fetch_row($result)){
+					$fields_old[] = $row_old[0];
+					$modif_field = '';
+					if(isset($table['fields'][$table['field_names'][$row_old[0]]])){
+						$field_infos=$table['fields'][$table['field_names'][$row_old[0]]];
+						foreach($row_old as $key=>$enreg)
 						{
-							if($row[$key] != $field_infos[$key])
+							//if the old field information of this kind doesn't match the new, record it
+							if($row_old[$key] != $field_infos[$key])
 							{
-								$modif_tables[]=$table['table_name'];
-
-								break;
+								$modif_field .='~+~'.$field_details[$key].'~+~,';
+									break;
 							}
 						}
-
-						$i++;
+						//only record the whole stuff if the string is not empty
+						if(strlen($modif_field)>0){
+							$modif_fields[$row_old[0]] .= substr($modif_field,0,-1);
+						}
 					}
 				}
+				$new_fields = array_diff($fields_new,$fields_old);
+				foreach($new_fields as $dummy=>$val){
+					$new_fields[$dummy] = '++'.$val.'++';
+				}
+				$old_fields = array_diff($fields_old,$fields_new); 
+				foreach($old_fields as $dummy=>$val){
+					$old_fields[$dummy] = '--'.$val.'--';
+				}
+				if(count($old_fields)>0 or count($modif_fields)>0 or count($new_fields)>0 ){
+					$modif_tables[]=array(
+						'table'=>$table['table_name'],
+						'old_fields'=>$old_fields,
+						'changed_fields'=>$modif_fields,
+						'new_fields'=>$new_fields,
+					);
+				}				
 			}
-
 			$tables_db_new[]=$table['table_name'];
 		}
 
@@ -152,15 +190,93 @@ foreach($bases_new as $num_base=>$base)
 
 		foreach($diff as $enreg)
 		{
-			$modif_tables[]=$enreg;
+			$modif_tables[]='---'.$enreg.'---';
 		}
+		//$modif_tables=array_unique($modif_tables); //deprecated with the structure complexification
+		
+	}else{ //this database was removed in the new version
+		$query="SHOW TABLES FROM ".$bases_old[$num_base];
+		$result=mysql_query($query,$db_old) or die(mysql_error());
 
-		$modif_tables=array_unique($modif_tables);
+		while($row=mysql_fetch_row($result))
+		{
+			$tables_db_old[]=$row[0];
+		}
+
+		$diff=array_diff($tables_db_old,$tables_db_new);
 
-		echo '<pre>'.print_r($modif_tables,true).'</pre>';
+		foreach($diff as $enreg)
+		{
+			$modif_tables[]='---'.$enreg.'---';
+		}
+
+		$modif_tables=array_unique($modif_tables);
+		echo "<h3>This database has been removed!</h3>";	
 	}
+	echo "<h3>Differences between each table</h3>" .
+			"- fields display under each table's name, <br>" .
+			"- new tables are surrounded by '**', <br/>" .
+			"- removed tables are surrounded by '---',<br/>" .
+			"- new fields are surrounded by '++',<br/>" .
+			"- removed fields are surrounded by '--',<br/>" .
+			"- modified fields are surrounded by '~+~',<br/>";
+	echo '<pre>'.print_r($modif_tables,true).'</pre>';
+	$all_db_changes[$base] = $modif_tables;
 }
 
 mysql_close($db_new);
 mysql_close($db_old);
+
+echo "<h2>Generating SQL</h2>";
+//going through all databases changes
+foreach($all_db_changes as $base => $changes){
+	echo "<h3>SQL for DB $base</h3>";
+	foreach($changes as $table){
+		if(is_array($table)){
+			//we have a field-level difference
+			$mytable = $table['table'];
+			$myold = $table['old_fields'];
+			$mychanged = $table['changed_fields'];
+			$mynew = $table['new_fields'];
+			foreach($myold as $myname){
+				//column lost, display DROP command
+				$myname = str_replace('--','',$myname);
+				echo "ALTER TABLE ".$mytable." DROP COLUMN ".$myname."<br/>";
+			}
+			foreach($mychanged as $myname=>$myprop){
+				//field changed, display SET command
+				$myprops = split(',',$myprop);
+				$myprops_string = '';
+				foreach($myprops as $myprop){
+					$myprop = str_replace('~+~','',$myprop);
+					$myprops_string .= $myprop." ";
+				}
+				echo "ALTER TABLE ".$mytable." SET COLUMN $myname $myprops_string<br/>";
+			}
+			foreach($mynew as $myname){
+				//column created, display ADD command
+				$myname = str_replace('++','',$myname);
+				echo "ALTER TABLE ".$mytable." ADD COLUMN $myname...<br/>";
+			}
+		}else{
+			//we have a table-level difference
+			$open_tag = substr($table,0,2);
+			switch($open_tag){
+				case '**':
+					//new table, display CREATE TABLE command
+					$table = str_replace('**','',$table);
+					echo "CREATE TABLE ".$table."();<br/>";
+					break;
+				case '--':
+					//dropped table, display DROP TABLE command
+					$table = str_replace('---','',$table);
+					echo "DROP TABLE ".$table."();<br/>";
+					break;
+				default:
+					echo "Unknown table problem: ".$table."<br/>";
+					break;
+			}
+		}
+	}
+}
 ?>