compare_db.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php // $Id: compare_db.php 16653 2008-11-03 22:49:07Z ivantcholakov $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2006 Yannick Warnier <yannick.warnier@dokeos.com>
  6. Copyright (c) 2004 Dokeos S.A.
  7. Copyright (c) 2003 Ghent University (UGent)
  8. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. ==============================================================================
  22. * This script allows you to know which tables have been modified
  23. * between two versions of Dokeos databases.
  24. *
  25. * @package dokeos.install
  26. ==============================================================================
  27. */
  28. /**
  29. * Database configuration
  30. * INSTRUCTIONS
  31. * ============
  32. * Change these parameters to compare between an old and a new database install.
  33. * You will need to create a course called 'COURSE' on each side to be able to compare the
  34. * courses databases.
  35. * If you have given fancy names to your databases, you will need to modify these names
  36. * in the two $bases_* variables definitions below.
  37. * Also, make sure about the prefix possibly used in front of the normal prefix for courses
  38. * databases (i.e. 'zPrefix_course' contains 'z' as additional prefix).
  39. */
  40. $sql_server_new='localhost';
  41. $sql_user_new='root';
  42. $sql_pass_new='';
  43. $prefix_new = 'dokeos180_';
  44. $bases_new=array($prefix_new.'dokeos_main',$prefix_new.'dokeos_stats',$prefix_new.'dokeos_user','z'.$prefix_new.'COURSE',$prefix_new.'dokeos_scorm');
  45. $db_new = mysql_connect($sql_server_new,$sql_user_new,$sql_pass_new) or die(mysql_error());
  46. // The Dokeos system has not been designed to use special SQL modes that were introduced since MySQL 5
  47. @mysql_query("set session sql_mode='';", $db_new);
  48. $sql_server_old='localhost';
  49. $sql_user_old='root';
  50. $sql_pass_old='';
  51. $prefix_old = 'dokeos160_';
  52. $bases_old=array($prefix_old.'dokeos_main',$prefix_old.'dokeos_stats',$prefix_old.'dokeos_user',$prefix_old.'COURSE',$prefix_old.'dokeos_scorm');
  53. $db_old = mysql_connect($sql_server_old,$sql_user_old,$sql_pass_old) or die(mysql_error());
  54. // The Dokeos system has not been designed to use special SQL modes that were introduced since MySQL 5
  55. @mysql_query("set session sql_mode='';", $db_old);
  56. $field_details = array(0=>'Field',1=>'Type',2=>'Null',3=>'Key',4=>'Default',5=>'Extra');
  57. /********************************/
  58. $all_db_changes = array();
  59. echo "<h1>Databases structure comparison script</h1>";
  60. //iterate through databases given above (checking from the 'new' database side)
  61. foreach($bases_new as $num_base=>$base)
  62. {
  63. //init tables lists for this database
  64. $modif_tables=array();
  65. $tables_db_new=array();
  66. $tables_db_old=array();
  67. $dump=array();
  68. //display current processed database
  69. echo "<h2>Now analysing differences between databases <em>$base</em> and <em>".$bases_old[$num_base]."</em></h2>";
  70. //get a list of tables for this database
  71. $query_new="SHOW TABLES FROM ".$bases_new[$num_base];
  72. $result_new=mysql_query($query_new,$db_new);
  73. if($result_new) //if there are tables in this database
  74. {
  75. $i=0;
  76. //as there are tables, process them one by one
  77. while($row_new=mysql_fetch_row($result_new))
  78. {
  79. $dump[$i]['table_name']=$row_new[0];
  80. $dump[$i]['fields']=array();
  81. $query_old="SHOW FIELDS FROM ".$bases_new[$num_base].".".$row_new[0];
  82. $result_old=mysql_query($query_old,$db_old) or die(mysql_error());
  83. $j=0;
  84. //get the fields details (numbered fields)
  85. while($row_old=mysql_fetch_row($result_old))
  86. {
  87. $dump[$i]['fields'][$j][0]=$row_old[0];
  88. $dump[$i]['fields'][$j][1]=$row_old[1];
  89. $dump[$i]['fields'][$j][2]=$row_old[2];
  90. $dump[$i]['fields'][$j][3]=$row_old[3];
  91. $dump[$i]['fields'][$j][4]=$row_old[4];
  92. $dump[$i]['fields'][$j][5]=$row_old[5];
  93. //get the field name in one special element of this array
  94. $dump[$i]['field_names'][$row_old[0]]=$j;
  95. $j++;
  96. }
  97. $i++;
  98. }
  99. foreach($dump as $table)
  100. {
  101. $query="SHOW FIELDS FROM ".$bases_old[$num_base].".".$table['table_name'];
  102. $result=mysql_query($query,$db_old);
  103. if(!$result)
  104. {
  105. $modif_tables[]='**'.$table['table_name'].'**';
  106. }
  107. else
  108. {
  109. $i=0;
  110. //check for removed, new or modified fields
  111. $fields_old = array();
  112. $fields_new = array();
  113. //list the new fields in a enumeration array
  114. foreach($table['field_names'] as $dummy_key=>$dummy_field){
  115. $fields_new[] = $dummy_key;
  116. }
  117. //list the old fields in an enumeration array and check if their corresponding
  118. //field in the new table is different (if any)
  119. $modif_fields = array();
  120. while($row_old = mysql_fetch_row($result)){
  121. $fields_old[] = $row_old[0];
  122. $modif_field = '';
  123. if(isset($table['fields'][$table['field_names'][$row_old[0]]])){
  124. $field_infos=$table['fields'][$table['field_names'][$row_old[0]]];
  125. foreach($row_old as $key=>$enreg)
  126. {
  127. //if the old field information of this kind doesn't match the new, record it
  128. if($row_old[$key] != $field_infos[$key])
  129. {
  130. $modif_field .='~+~'.$field_details[$key].'~+~,';
  131. break;
  132. }
  133. }
  134. //only record the whole stuff if the string is not empty
  135. if(strlen($modif_field)>0){
  136. $modif_fields[$row_old[0]] .= substr($modif_field,0,-1);
  137. }
  138. }
  139. }
  140. $new_fields = array_diff($fields_new,$fields_old);
  141. foreach($new_fields as $dummy=>$val){
  142. $new_fields[$dummy] = '++'.$val.'++';
  143. }
  144. $old_fields = array_diff($fields_old,$fields_new);
  145. foreach($old_fields as $dummy=>$val){
  146. $old_fields[$dummy] = '--'.$val.'--';
  147. }
  148. if(count($old_fields)>0 or count($modif_fields)>0 or count($new_fields)>0 ){
  149. $modif_tables[]=array(
  150. 'table'=>$table['table_name'],
  151. 'old_fields'=>$old_fields,
  152. 'changed_fields'=>$modif_fields,
  153. 'new_fields'=>$new_fields,
  154. );
  155. }
  156. }
  157. $tables_db_new[]=$table['table_name'];
  158. }
  159. $query="SHOW TABLES FROM ".$bases_old[$num_base];
  160. $result=mysql_query($query,$db_old) or die(mysql_error());
  161. while($row=mysql_fetch_row($result))
  162. {
  163. $tables_db_old[]=$row[0];
  164. }
  165. $diff=array_diff($tables_db_old,$tables_db_new);
  166. foreach($diff as $enreg)
  167. {
  168. $modif_tables[]='---'.$enreg.'---';
  169. }
  170. //$modif_tables=array_unique($modif_tables); //deprecated with the structure complexification
  171. }else{ //this database was removed in the new version
  172. $query="SHOW TABLES FROM ".$bases_old[$num_base];
  173. $result=mysql_query($query,$db_old) or die(mysql_error());
  174. while($row=mysql_fetch_row($result))
  175. {
  176. $tables_db_old[]=$row[0];
  177. }
  178. $diff=array_diff($tables_db_old,$tables_db_new);
  179. foreach($diff as $enreg)
  180. {
  181. $modif_tables[]='---'.$enreg.'---';
  182. }
  183. $modif_tables=array_unique($modif_tables);
  184. echo "<h3>This database has been removed!</h3>";
  185. }
  186. echo "<h3>Differences between each table</h3>" .
  187. "- fields display under each table's name, <br>" .
  188. "- new tables are surrounded by '**', <br/>" .
  189. "- removed tables are surrounded by '---',<br/>" .
  190. "- new fields are surrounded by '++',<br/>" .
  191. "- removed fields are surrounded by '--',<br/>" .
  192. "- modified fields are surrounded by '~+~',<br/>";
  193. echo '<pre>'.print_r($modif_tables,true).'</pre>';
  194. $all_db_changes[$base] = $modif_tables;
  195. }
  196. mysql_close($db_new);
  197. mysql_close($db_old);
  198. echo "<h2>Generating SQL</h2>";
  199. //going through all databases changes
  200. foreach($all_db_changes as $base => $changes){
  201. echo "<h3>SQL for DB $base</h3>";
  202. foreach($changes as $table){
  203. if(is_array($table)){
  204. //we have a field-level difference
  205. $mytable = $table['table'];
  206. $myold = $table['old_fields'];
  207. $mychanged = $table['changed_fields'];
  208. $mynew = $table['new_fields'];
  209. foreach($myold as $myname){
  210. //column lost, display DROP command
  211. $myname = str_replace('--','',$myname);
  212. echo "ALTER TABLE ".$mytable." DROP ".$myname."<br/>";
  213. }
  214. foreach($mychanged as $myname=>$myprop){
  215. //field changed, display SET command
  216. $myprops = split(',',$myprop);
  217. $myprops_string = '';
  218. foreach($myprops as $myprop){
  219. $myprop = str_replace('~+~','',$myprop);
  220. $myprops_string .= $myprop." ";
  221. }
  222. echo "ALTER TABLE ".$mytable." CHANGE $myname $myname $myprops_string<br/>";
  223. }
  224. foreach($mynew as $myname){
  225. //column created, display ADD command
  226. $myname = str_replace('++','',$myname);
  227. echo "ALTER TABLE ".$mytable." ADD $myname...<br/>";
  228. }
  229. }else{
  230. //we have a table-level difference
  231. $open_tag = substr($table,0,2);
  232. switch($open_tag){
  233. case '**':
  234. //new table, display CREATE TABLE command
  235. $table = str_replace('**','',$table);
  236. echo "CREATE TABLE ".$table."();<br/>";
  237. break;
  238. case '--':
  239. //dropped table, display DROP TABLE command
  240. $table = str_replace('---','',$table);
  241. echo "DROP TABLE ".$table."();<br/>";
  242. break;
  243. default:
  244. echo "Unknown table problem: ".$table."<br/>";
  245. break;
  246. }
  247. }
  248. }
  249. }
  250. ?>