dokeos-install
[ class tree: dokeos-install ] [ index: dokeos-install ] [ all elements ]

Source for file compare_db.php

Documentation is available at compare_db.php

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

Documentation generated on Thu, 12 Jun 2008 13:08:41 -0500 by phpDocumentor 1.4.1