[ Index ]
 

Code source de vtiger CRM 5.0.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/modules/Migration/ -> Migration.php (source)

   1  <?php
   2  /*********************************************************************************
   3  ** The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9  *
  10   ********************************************************************************/
  11  
  12  //Migration Procedure
  13  //Step : 1 => Take a dump of old database
  14  //Step : 2 => Drop the New Database
  15  //Step : 3 => Create the New Database
  16  //Step : 4 => Put the old dump into the New Database
  17  //Step : 5 => Modify the new database with the new changes
  18  
  19  class Migration
  20  {
  21  
  22      var $conn;
  23      var $oldconn;
  24  
  25      //Old Database Parameters
  26      var $old_hostname;
  27      var $old_mysql_port;
  28      var $old_mysql_username;
  29      var $old_mysql_password;
  30      var $old_dbname;
  31  
  32      //New Database Parameters
  33      var $new_hostname;
  34      var $new_mysql_port;
  35      var $new_mysql_username;
  36      var $new_mysql_password;
  37      var $new_dbname;
  38  
  39      /**    Constructor with old database and new database connections
  40       */
  41  	function Migration($old='',$new='')
  42      {
  43          global $migrationlog;
  44          $migrationlog->debug("Inside the constructor Migration.");
  45          
  46          $this->oldconn = $old;
  47          $this->conn = $new;
  48          $migrationlog->debug("Database Object has been created.");
  49      }
  50  
  51      /**    function used to set the Old database parameters in the migration object properties
  52       *    @param string $hostname - old database hostname
  53       *    @param int $mysql_port - old database mysql port number
  54       *    @param string $mysql_username - old database mysql user name
  55       *    @param string $mysql_password - old database mysql password
  56       *    @param string $dbname - old database name
  57       *    @return void
  58       */
  59  	function setOldDatabaseParams($hostname,$mysql_port,$mysql_username,$mysql_password,$dbname)
  60      {
  61          global $migrationlog;
  62          $migrationlog->debug("Inside the function setOldDatabaseParams($hostname,$mysql_port,$mysql_username,$mysql_password,$dbname)");
  63          
  64          $this->old_hostname = $hostname;
  65          $this->old_mysql_port = $mysql_port;
  66          $this->old_mysql_username = $mysql_username;
  67          $this->old_mysql_password = $mysql_password;
  68          $this->old_dbname = $dbname;
  69          
  70          $migrationlog->debug("Old Database Parameters has been set.");
  71      }
  72  
  73      /**    function used to set the Current ie., new 5.0 database parameters in the migration object properties
  74       *    @param string $hostname - new database hostname
  75       *    @param int $mysql_port - new database mysql port number
  76       *    @param string $mysql_username - new database mysql user name
  77       *    @param string $mysql_password - new database mysql password
  78       *    @param string $dbname - new database name
  79       *      @return void
  80       */
  81  	function setNewDatabaseParams($hostname,$mysql_port,$mysql_username,$mysql_password,$dbname)
  82      {
  83          global $migrationlog;
  84          $migrationlog->debug("Inside the function setNewDatabaseParams($hostname,$mysql_port,$mysql_username,$mysql_password,$dbname)");
  85          
  86          $this->new_hostname = $hostname;
  87          $this->new_mysql_port = $mysql_port;
  88          $this->new_mysql_username = $mysql_username;
  89          $this->new_mysql_password = $mysql_password;
  90          $this->new_dbname = $dbname;
  91          
  92          $migrationlog->debug("New Database Parameters has been set.");
  93      }
  94  
  95      /**    function used to take the database dump
  96       *    @param string $host_name - host name in where the database is available
  97       *    @param int $mysql_port - mysql port number 
  98       *    @param string $mysql_username - mysql user name
  99       *    @param string $mysql_password - mysql password
 100       *    @param string $dbname - database name
 101       *      @return string $dump_filename - return the dump filename
 102       */
 103  	function takeDatabaseDump($host_name,$mysql_port,$mysql_username,$mysql_password,$dbname)
 104      {
 105          global $migrationlog;
 106          $migrationlog->debug("Inside the function takeDatabaseDump($host_name,$mysql_port,$mysql_username,$mysql_password,$dbname). Going to take the specified database dump...");
 107  
 108          $dump_filename = 'dump_'.$dbname.'.txt';
 109  
 110          if($mysql_password != '')
 111          {
 112              $password_str = " -p".$mysql_password;
 113          }
 114          else
 115          {
 116              $password_str = '';
 117          }
 118  
 119          //This if is used when we cannot access mysql from vtiger root directory
 120          if($_SESSION['set_server_mysql_path'] != '')
 121          {
 122              $current_working_dir = getcwd();
 123              $server_mysql_path = $_SESSION['set_server_mysql_path'];
 124  
 125              $dump_str = "mysqldump -u".$mysql_username.$password_str." -h ".$host_name." --port=".$mysql_port." ".$dbname." >> ".$dump_filename;
 126              $migrationlog->debug("Server path set. Dump string to execute ==> $dump_str");
 127              
 128              chdir ($server_mysql_path);
 129  
 130              exec("echo 'set FOREIGN_KEY_CHECKS = 0;' > ".$dump_filename);
 131              exec($dump_str);
 132              exec("echo 'set FOREIGN_KEY_CHECKS = 1;' >> ".$dump_filename);
 133              
 134              exec('cp "'.$server_mysql_path.'\\'.$dump_filename.'" "'.$current_working_dir.'\\'.$dump_filename).'"';
 135              chdir ($current_working_dir);
 136          }
 137          else
 138          {
 139              $migrationlog->debug("Dump string to execute ==> mysqldump -u".$mysql_username." -h ".$host_name.$password_str." --port=".$mysql_port." ".$dbname." >> ".$dump_filename);
 140  
 141              exec("echo 'set FOREIGN_KEY_CHECKS = 0;' > ".$dump_filename);
 142              exec("mysqldump -u".$mysql_username." -h ".$host_name.$password_str." --port=".$mysql_port." ".$dbname." >> ".$dump_filename);
 143              exec("echo 'set FOREIGN_KEY_CHECKS = 1;' >> ".$dump_filename);
 144          }
 145  
 146          $_SESSION['migration_log'] .= '<br> <b>'.$dbname.'</b> Database Dump has been taken and the file is ==> '.$dump_filename;
 147          $migrationlog->debug("<br> <b> $dbname </b> Database Dump has been taken and the file is ==> $dump_filename");
 148  
 149          return $dump_filename;
 150      }
 151  
 152      /**    function used to drop the database
 153       *    @param object $conn - adodb object which is connected with the current(new) database 
 154       *    @param string $dbname - database name which we want to drop
 155       *      @return void
 156       */
 157  	function dropDatabase($conn,$dbname)
 158      {
 159          global $migrationlog;
 160          $migrationlog->debug("Inside the function dropDatabase($conn,$dbname). Going to drop the database - $dbname");
 161          $sql = "drop database ".$dbname;
 162          $conn->query($sql);
 163  
 164          $migrationlog->debug("Database ($dbname) has been dropped.");
 165          $_SESSION['migration_log'] .= '<br> <b>'.$dbname.'</b> Database has been dropped.';
 166      }
 167  
 168      /**     function used to create the database
 169       *    @param object $conn - adodb object which is connected with the current(new) database
 170       *    @param string $dbname - database name which we want to drop
 171       *      @return void
 172       */
 173  	function createDatabase($conn,$dbname)
 174      {
 175          global $migrationlog;
 176          $migrationlog->debug("Inside the function createDatabase($conn, $dbname). Going to create the database - $dbname");
 177          
 178          $sql = "create database ".$dbname;
 179          $conn->query($sql);
 180  
 181          $_SESSION['migration_log'] .= '<br> <b>'.$dbname.'</b> Database has been created.';
 182          $migrationlog->debug("Database ($dbname) has been dropped.");
 183          
 184          //Added to avoid the No Database Selected error when execute the queries
 185          $conn->connect();
 186      }
 187  
 188      /**    function used to apply the dump data to a database
 189       *    @param string $host_name - host name
 190       *    @param int $mysql_port - mysql port number
 191       *    @param string $mysql_username - mysql user name
 192       *    @param string $mysql_password - mysql password
 193       *    @param string $dbname - database name to which we want to apply the dump
 194       *    @param string $dumpfile - dump file which contains the data dump of a database
 195       *      @return void
 196       */
 197  	function applyDumpData($host_name,$mysql_port,$mysql_username,$mysql_password,$dbname,$dumpfile)
 198      {
 199          global $migrationlog;
 200          $migrationlog->debug("Inside the function applyDumpData($host_name,$mysql_port,$mysql_username,$mysql_password,$dbname,$dumpfile).");
 201  
 202          if($mysql_password != '')
 203          {
 204              $password_str = " --password=".$mysql_password;
 205          }
 206          else
 207          {
 208              $password_str = '';
 209          }
 210  
 211          //This if is used when we cannot access mysql from vtiger root directory
 212          if($_SESSION['set_server_mysql_path'] != '')
 213          {
 214              $current_working_dir = getcwd();
 215              $server_mysql_path = $_SESSION['set_server_mysql_path'];
 216  
 217              $dump_str = "mysql --user=".$mysql_username.$password_str." -h ".$host_name." --force --port=".$mysql_port." ".$dbname." < ".$dumpfile;
 218              $migrationlog->debug("MySQL server path set. Dump string to apply ==> $dump_str");
 219  
 220              //exec("path = $server_mysql_path");
 221              chdir ($server_mysql_path);
 222  
 223              exec($dump_str);
 224              
 225              chdir ($current_working_dir);
 226          }
 227          else
 228          {
 229              exec("mysql --user=".$mysql_username." -h ".$host_name." --force --port=".$mysql_port.$password_str." ".$dbname." < ".$dumpfile);
 230              $migrationlog->debug("Dump string to apply ==> mysql --user=$mysql_username -h $host_name --force --port=$mysql_port $password_str $dbname < $dumpfile");
 231          }
 232  
 233          $_SESSION['migration_log'] .= '<br> Database Dump has been applied to the <b>'.$dbname.'</b> Database from <b>'.$dumpfile.'</b>';
 234          $migrationlog->debug("<br> Database Dump has been applied to the <b> $dbname </b> database from <b> $dumpfile </b>");
 235      }
 236  
 237  
 238      /**    function used to get the tabid
 239       *    @param string $module - module to which we want to get the tabid
 240       *    @return int $tabid - return the tabid of the module
 241       */
 242  	function localGetTabID($module)
 243      {
 244          global $conn;
 245  
 246          $sql = "select tabid from vtiger_tab where name='".$module."'";
 247          $result = $conn->query($sql);
 248          $tabid=  $conn->query_result($result,0,"tabid");
 249  
 250          return $tabid;
 251      }
 252  
 253      /**    function used to get the table count of the new database
 254       *    @return int $tables - return the number of tables available in the new database
 255       */
 256  	function getTablesCountInNewDatabase()
 257      {
 258          global $migrationlog;
 259          $migrationlog->debug("Inside the function getTablesCountInNewDatabase()");
 260          $newconn = @mysql_connect($this->new_hostname.':'.$this->new_mysql_port,$this->new_mysql_username,$this->new_mysql_password);
 261          $tables = @mysql_num_rows(mysql_list_tables($this->new_dbname,$newconn));
 262  
 263          $migrationlog->debug("Number of Tables in New Database = $tables");
 264          return $tables;
 265      }
 266  
 267      /**    function used to get the table count of the old database
 268       *    @return int $tables - return the number of tables available in the old database
 269       */
 270  	function getTablesCountInOldDatabase()
 271      {
 272          global $migrationlog;
 273          $migrationlog->debug("Inside the function getTablesCountInOldDatabase()");
 274          $oldconn = @mysql_connect($this->old_hostname.':'.$this->old_mysql_port,$this->old_mysql_username,$this->old_mysql_password);
 275          $tables = @mysql_num_rows(mysql_list_tables($this->old_dbname,$oldconn));
 276  
 277          $migrationlog->debug("Number of Tables in Old Database = $tables");
 278          return $tables;
 279      }
 280  
 281      /**    function used to modify the database from old version to match with new version
 282       *    @param object $conn - adodb object which is connected with the current(new) database 
 283       *      @return void
 284       */
 285  	function modifyDatabase($conn)
 286      {
 287          global $migrationlog;
 288          $migrationlog->debug("Inside the function modifyDatabase($conn)");
 289          $migrationlog->debug("\n\n\nMickie ---- Starts");
 290  
 291          $_SESSION['migration_log'] .= "<br>The current database is going to be modified by executing the following queries...<br>";
 292          
 293          //Added variables to get the queries list and count
 294          $query_count = 1;
 295          $success_query_count = 1;
 296          $failure_query_count = 1;
 297          $success_query_array = Array();
 298          $failure_query_array = Array();
 299  
 300          //To handle the file includes for each and every version
 301          //Here we have to decide which files should be included, where the files will be added newly for every public release
 302          //Handle Here -- Mickie
 303          include ("modules/Migration/ModifyDatabase/MigrationInfo.php");
 304  
 305          $migrationlog->debug("Mickie ---- Ends\n\n\n");
 306      }
 307  
 308      /**    function used to run the migration process based on the option selected and values given
 309       *    @param int $same_databases - 1 if both databases are same otherwise 0
 310       *    @param string $option - selected migration option (dbsource or dumpsource)
 311       *    @param string $old_dump_file_name - dump file name of the old database which is optional when we use dbsource
 312       *      @return void
 313       */
 314  	function migrate($same_databases, $option, $old_dump_file_name='')
 315      {
 316          //1. Migration Procedure -- when we give the Source Database values
 317          //Step : 1 => Take a dump of old database
 318          //Step : 2 => Drop the New Database
 319          //Step : 3 => Create the New Database
 320          //Step : 4 => Put the old dump into the New Database
 321          //Step : 5 => Modify the new database with the new changes
 322  
 323          //2. Migration Procedure -- when we give the Database dump file
 324          //Step : 1 => Drop the New Database
 325          //Step : 2 => Create the New Database
 326          //Step : 3 => Put the dump into the New Database
 327          //Step : 4 => Modify the new database with the new changes
 328  
 329          global $migrationlog;
 330          global $conn;
 331          $migrationlog->debug("Database Migration from Old Database to the Current Database Starts.");
 332          $migrationlog->debug("Migration Option = $option");
 333  
 334          //Set the old database parameters
 335          $old_host_name = $this->old_hostname;
 336          $old_mysql_port = $this->old_mysql_port;
 337          $old_mysql_username = $this->old_mysql_username;
 338          $old_mysql_password = $this->old_mysql_password;
 339          $old_dbname = $this->old_dbname;
 340  
 341          //Set the new database parameters
 342          $new_host_name = $this->new_hostname;
 343          $new_mysql_port = $this->new_mysql_port;
 344          $new_mysql_username = $this->new_mysql_username;
 345          $new_mysql_password = $this->new_mysql_password;
 346          $new_dbname = $this->new_dbname;
 347  
 348          //This will be done when we give the Source Database details
 349          if($option == 'dbsource')
 350          {
 351              //Take the dump of the old Database
 352              $migrationlog->debug("Going to take the old Database Dump.");
 353              $dump_file = $this->takeDatabaseDump($old_host_name,$old_mysql_port,$old_mysql_username,$old_mysql_password,$old_dbname);
 354  
 355              //check the file size is greater than 10000 bytes (~app) and if yes then continue else goto step1
 356              if(is_file($dump_file) && filesize($dump_file) > 10000)
 357              {
 358                  $_SESSION['migration_log'] .= "Source database dump taken successfully.";
 359              }
 360              else
 361              {
 362                  echo '<br><font color="red"><b>The Source database dump taken may not contain all values. So please use other option.</font></b>';
 363                  include ("modules/Migration/MigrationStep1.php");
 364                  exit;
 365              }
 366          }
 367          elseif($option == 'dumpsource')
 368          {
 369              $dump_file = $old_dump_file_name;
 370          }
 371  
 372          //if old db and new db are different then take new db dump
 373          if($old_dbname != $new_dbname)
 374          {
 375              //This is to take dump of the new database for backup purpose
 376              $migrationlog->debug("Going to take the current Database Dump.");
 377              $new_dump_file = $this->takeDatabaseDump($new_host_name,$new_mysql_port,$new_mysql_username,$new_mysql_password,$new_dbname);
 378  
 379              //check the file size is greater than 10000 bytes (~app) and if yes then continue else goto step1
 380              if(is_file($new_dump_file) && filesize($new_dump_file) > 10000)
 381              {
 382                  $_SESSION['migration_log'] .= "Current database dump taken successfully.";
 383              }
 384              else
 385              {
 386                  $_SESSION['migration_log'] .= '<br><font color="red"><b>The Current database dump taken may not contain all values. So may not be reload the current database if any problem occurs in migration. If the migration not completed please rename the install.php and install folder and run the install.php</font></b>';
 387                  //include("modules/Migration/MigrationStep1.php");
 388                  //exit;
 389              }
 390  
 391          }
 392  
 393  
 394          $continue_process = 1;
 395          if($same_databases == 1)
 396          {
 397              $_SESSION['migration_log'] .= '<br> Same databases are used. so skip the process of drop and create the current database.';
 398          }
 399          else
 400          {
 401              $_SESSION['migration_log'] .= '<br> Databases are different. So drop the Current Database and create. Also apply the dump of Old Database';
 402              //Drop the current(latest) Database
 403              $migrationlog->debug("Going to Drop the current Database");
 404              $this->dropDatabase($conn,$new_dbname);
 405  
 406              //Create the new current(latest) Database
 407              $migrationlog->debug("Going to Create the current Database");
 408              $this->createDatabase($conn,$new_dbname);
 409  
 410              //Apply the dump of the old database to the current database
 411              $migrationlog->debug("Going to apply the old database dump to the new database.");
 412              $this->applyDumpData($new_host_name,$new_mysql_port,$new_mysql_username,$new_mysql_password,$new_dbname,$dump_file);
 413  
 414              //get the number of tables in new database 
 415              $new_tables_count = $this->getTablesCountInNewDatabase();
 416  
 417              //get the number of tables in old database 
 418              $old_tables_count = $this->getTablesCountInOldDatabase();
 419  
 420              //if tables are missing after apply the dump, then alert the user and quit
 421              if(($new_tables_count != $old_tables_count && $option == 'dbsource') || ($new_tables_count < 180 && $option == 'dumpsource'))
 422              {
 423                  $migrationlog->debug("New Database tables not equal to Old Database tables count. Reload the current database again and quit.");
 424                  
 425                  $continue_process = 0;
 426                  $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";
 427                 ?>
 428                  <script language="javascript">
 429                      alert("<?php echo $msg; ?>");
 430                  </script>
 431                 <?php
 432              }
 433          }
 434  
 435          if($continue_process == 1)
 436          {
 437              //Modify the database which is now as old database setup
 438              $migrationlog->debug("Going to modify the current database which is now as old database setup");
 439              $this->modifyDatabase($conn);
 440          
 441              $migrationlog->debug("Database Modifications Ends......");
 442              $migrationlog->debug("Database Migration from Old Database to the Current Database has been Finished.");
 443          }
 444          else
 445          {
 446              //Drop the current(latest) Database
 447              $migrationlog->debug("Problem in migration. so going to Restore the current Database");
 448              $migrationlog->debug("Going to Drop the current Database");
 449              $this->dropDatabase($conn,$new_dbname);
 450  
 451              //Create the new current(latest) Database
 452              $migrationlog->debug("Going to Create the current Database");
 453              $this->createDatabase($conn,$new_dbname);
 454  
 455              //Reload the new db dump and quit
 456              $migrationlog->debug("Going to apply the new backup db dump");
 457              $this->applyDumpData($new_host_name,$new_mysql_port,$new_mysql_username,$new_mysql_password,$new_dbname,$new_dump_file);
 458  
 459              //Return to Step1
 460              echo '<br><font color="red"><b>Dump could not be applied correctly. so your previous database restored.</b></font>';
 461              include ("modules/Migration/MigrationStep1.php");
 462          }
 463  
 464          //Now we should recalculate the user and sharing privileges
 465          RecalculateSharingRules();
 466      }
 467  
 468  }
 469  
 470  
 471  
 472  ?>


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7