compare_db.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php // $Id: compare_db.php 10527 2006-12-19 11:01:20Z yannoo $
  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. $sql_server_old='localhost';
  47. $sql_user_old='root';
  48. $sql_pass_old='';
  49. $prefix_old = 'dokeos160_';
  50. $bases_old=array($prefix_old.'dokeos_main',$prefix_old.'dokeos_stats',$prefix_old.'dokeos_user',$prefix_old.'COURSE',$prefix_old.'dokeos_scorm');
  51. $db_old = mysql_connect($sql_server_old,$sql_user_old,$sql_pass_old) or die(mysql_error());
  52. $field_details = array(0=>'Field',1=>'Type',2=>'Null',3=>'Key',4=>'Default',5=>'Extra');
  53. /********************************/
  54. $all_db_changes = array();
  55. echo "<h1>Databases structure comparison script</h1>";
  56. //iterate through databases given above (checking from the 'new' database side)
  57. foreach($bases_new as $num_base=>$base)
  58. {
  59. //init tables lists for this database
  60. $modif_tables=array();
  61. $tables_db_new=array();
  62. $tables_db_old=array();
  63. $dump=array();
  64. //display current processed database
  65. echo "<h2>Now analysing differences between databases <em>$base</em> and <em>".$bases_old[$num_base]."</em></h2>";
  66. //get a list of tables for this database
  67. $query_new="SHOW TABLES FROM ".$bases_new[$num_base];
  68. $result_new=mysql_query($query_new,$db_new);
  69. if($result_new) //if there are tables in this database
  70. {
  71. $i=0;
  72. //as there are tables, process them one by one
  73. while($row_new=mysql_fetch_row($result_new))
  74. {
  75. $dump[$i]['table_name']=$row_new[0];
  76. $dump[$i]['fields']=array();
  77. $query_old="SHOW FIELDS FROM ".$bases_new[$num_base].".".$row_new[0];
  78. $result_old=mysql_query($query_old,$db_old) or die(mysql_error());
  79. $j=0;
  80. //get the fields details (numbered fields)
  81. while($row_old=mysql_fetch_row($result_old))
  82. {
  83. $dump[$i]['fields'][$j][0]=$row_old[0];
  84. $dump[$i]['fields'][$j][1]=$row_old[1];
  85. $dump[$i]['fields'][$j][2]=$row_old[2];
  86. $dump[$i]['fields'][$j][3]=$row_old[3];
  87. $dump[$i]['fields'][$j][4]=$row_old[4];
  88. $dump[$i]['fields'][$j][5]=$row_old[5];
  89. //get the field name in one special element of this array
  90. $dump[$i]['field_names'][$row_old[0]]=$j;
  91. $j++;
  92. }
  93. $i++;
  94. }
  95. foreach($dump as $table)
  96. {
  97. $query="SHOW FIELDS FROM ".$bases_old[$num_base].".".$table['table_name'];
  98. $result=mysql_query($query,$db_old);
  99. if(!$result)
  100. {
  101. $modif_tables[]='**'.$table['table_name'].'**';
  102. }
  103. else
  104. {
  105. $i=0;
  106. //check for removed, new or modified fields
  107. $fields_old = array();
  108. $fields_new = array();
  109. //list the new fields in a enumeration array
  110. foreach($table['field_names'] as $dummy_key=>$dummy_field){
  111. $fields_new[] = $dummy_key;
  112. }
  113. //list the old fields in an enumeration array and check if their corresponding
  114. //field in the new table is different (if any)
  115. $modif_fields = array();
  116. while($row_old = mysql_fetch_row($result)){
  117. $fields_old[] = $row_old[0];
  118. $modif_field = '';
  119. if(isset($table['fields'][$table['field_names'][$row_old[0]]])){
  120. $field_infos=$table['fields'][$table['field_names'][$row_old[0]]];
  121. foreach($row_old as $key=>$enreg)
  122. {
  123. //if the old field information of this kind doesn't match the new, record it
  124. if($row_old[$key] != $field_infos[$key])
  125. {
  126. $modif_field .='~+~'.$field_details[$key].'~+~,';
  127. break;
  128. }
  129. }
  130. //only record the whole stuff if the string is not empty
  131. if(strlen($modif_field)>0){
  132. $modif_fields[$row_old[0]] .= substr($modif_field,0,-1);
  133. }
  134. }
  135. }
  136. $new_fields = array_diff($fields_new,$fields_old);
  137. foreach($new_fields as $dummy=>$val){
  138. $new_fields[$dummy] = '++'.$val.'++';
  139. }
  140. $old_fields = array_diff($fields_old,$fields_new);
  141. foreach($old_fields as $dummy=>$val){
  142. $old_fields[$dummy] = '--'.$val.'--';
  143. }
  144. if(count($old_fields)>0 or count($modif_fields)>0 or count($new_fields)>0 ){
  145. $modif_tables[]=array(
  146. 'table'=>$table['table_name'],
  147. 'old_fields'=>$old_fields,
  148. 'changed_fields'=>$modif_fields,
  149. 'new_fields'=>$new_fields,
  150. );
  151. }
  152. }
  153. $tables_db_new[]=$table['table_name'];
  154. }
  155. $query="SHOW TABLES FROM ".$bases_old[$num_base];
  156. $result=mysql_query($query,$db_old) or die(mysql_error());
  157. while($row=mysql_fetch_row($result))
  158. {
  159. $tables_db_old[]=$row[0];
  160. }
  161. $diff=array_diff($tables_db_old,$tables_db_new);
  162. foreach($diff as $enreg)
  163. {
  164. $modif_tables[]='---'.$enreg.'---';
  165. }
  166. //$modif_tables=array_unique($modif_tables); //deprecated with the structure complexification
  167. }else{ //this database was removed in the new version
  168. $query="SHOW TABLES FROM ".$bases_old[$num_base];
  169. $result=mysql_query($query,$db_old) or die(mysql_error());
  170. while($row=mysql_fetch_row($result))
  171. {
  172. $tables_db_old[]=$row[0];
  173. }
  174. $diff=array_diff($tables_db_old,$tables_db_new);
  175. foreach($diff as $enreg)
  176. {
  177. $modif_tables[]='---'.$enreg.'---';
  178. }
  179. $modif_tables=array_unique($modif_tables);
  180. echo "<h3>This database has been removed!</h3>";
  181. }
  182. echo "<h3>Differences between each table</h3>" .
  183. "- fields display under each table's name, <br>" .
  184. "- new tables are surrounded by '**', <br/>" .
  185. "- removed tables are surrounded by '---',<br/>" .
  186. "- new fields are surrounded by '++',<br/>" .
  187. "- removed fields are surrounded by '--',<br/>" .
  188. "- modified fields are surrounded by '~+~',<br/>";
  189. echo '<pre>'.print_r($modif_tables,true).'</pre>';
  190. $all_db_changes[$base] = $modif_tables;
  191. }
  192. mysql_close($db_new);
  193. mysql_close($db_old);
  194. echo "<h2>Generating SQL</h2>";
  195. //going through all databases changes
  196. foreach($all_db_changes as $base => $changes){
  197. echo "<h3>SQL for DB $base</h3>";
  198. foreach($changes as $table){
  199. if(is_array($table)){
  200. //we have a field-level difference
  201. $mytable = $table['table'];
  202. $myold = $table['old_fields'];
  203. $mychanged = $table['changed_fields'];
  204. $mynew = $table['new_fields'];
  205. foreach($myold as $myname){
  206. //column lost, display DROP command
  207. $myname = str_replace('--','',$myname);
  208. echo "ALTER TABLE ".$mytable." DROP ".$myname."<br/>";
  209. }
  210. foreach($mychanged as $myname=>$myprop){
  211. //field changed, display SET command
  212. $myprops = split(',',$myprop);
  213. $myprops_string = '';
  214. foreach($myprops as $myprop){
  215. $myprop = str_replace('~+~','',$myprop);
  216. $myprops_string .= $myprop." ";
  217. }
  218. echo "ALTER TABLE ".$mytable." CHANGE $myname $myname $myprops_string<br/>";
  219. }
  220. foreach($mynew as $myname){
  221. //column created, display ADD command
  222. $myname = str_replace('++','',$myname);
  223. echo "ALTER TABLE ".$mytable." ADD $myname...<br/>";
  224. }
  225. }else{
  226. //we have a table-level difference
  227. $open_tag = substr($table,0,2);
  228. switch($open_tag){
  229. case '**':
  230. //new table, display CREATE TABLE command
  231. $table = str_replace('**','',$table);
  232. echo "CREATE TABLE ".$table."();<br/>";
  233. break;
  234. case '--':
  235. //dropped table, display DROP TABLE command
  236. $table = str_replace('---','',$table);
  237. echo "DROP TABLE ".$table."();<br/>";
  238. break;
  239. default:
  240. echo "Unknown table problem: ".$table."<br/>";
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. ?>