Location: PHPKode > projects > OpenCabling > opencabling/upgrade_version.php
<?php

// Check if migrating from 0.14 to 0.15

$sql="select * from settings where field='version'";
$query=mysql_query($sql,$mysql_conn);
$result=mysql_fetch_assoc($query);
$version=$result['value'];
switch($version) {
 case "":
   echo "<p class=smalltext>Detected a pre-0.15 version or a fresh install</p>";
   echo "<p class=smalltext>Creating settings table</p>";
   $sql="CREATE TABLE IF NOT EXISTS settings ( field VARCHAR( 255 ) NOT NULL , value VARCHAR( 255 ) NOT NULL, UNIQUE ( field  ) )";
   $query=mysql_query($sql,$mysql_conn) or die("<p class=smalltext>Unable to create table settings: ".mysql_error()." </p>");
   echo "<p class=smalltext>Populating settings table</p>";
   $sql="DELETE FROM settings";
   $query=mysql_query($sql,$mysql_conn) or die("<p class=smalltext>Unable to delete settings table: ".mysql_error()." </p>");
   $sql="INSERT INTO settings ( field , value ) VALUES ( 'version', '' )";
   $query=mysql_query($sql,$mysql_conn) or die("<p class=smalltext>Unable to populate table settings: ".mysql_error()." </p>");
 case "0.15":
   echo "<p class=smalltext>Migrating from 0.15 to 0.16 version</p>";
   echo "<p class=smalltext>Altering objects table</p>";
   $sql="ALTER TABLE `objects` DROP `towards`";
   $query=mysql_query($sql,$mysql_conn);
   $sql="ALTER TABLE `objects` ADD `towards` CHAR( 1 ) NOT NULL AFTER `position`";
   $query=mysql_query($sql,$mysql_conn) or die ("<p class=smalltext>Unable to alter objects table: ".mysql_error()." </p>");
   echo "<p class=smalltext>Updating objects table</p>";
   $sql="UPDATE `objects` SET `towards` = 'F'";
   $query=mysql_query($sql,$mysql_conn) or die ("<p class=smalltext>Unable to update objects table: ".mysql_error()." </p>");

   // Tables Upgrade 

   $tables=array('objtypes'=>'id_obj_type', 'cabletypes'=>'id_cable_type', 'connectors'=>'id_connector_type', 'conntypes'=>'id_conn_type', 'displays'=>'id_display');
   
   foreach( $tables as $table => $field) {
     
     // Delete table and reload from file. Note, only id < 10000 are deleted
     echo "<p class=smalltext>Upgrading ".$table." table</p>";
     $sql="DELETE FROM ".$table." where ".$field."<1000";
     $query=mysql_query($sql,$mysql_conn) or die("<p class=smalltext>Unable to delete ".$table." rows: ".mysql_error()." </p>");
     // Reload rows from file.
     $sql=file_get_contents("./dump/".$table.".sql");
     $query=mysql_query($sql,$mysql_conn) or die("<p class=smalltext>Unable to insert data in ".$table." table: ".mysql_error()." </p>");
   }

   $sql="UPDATE settings set value='0.16' where field = 'version'";
   $query=mysql_query($sql,$mysql_conn) or die("<p class=smalltext>Unable to update table settings: ".mysql_error()." </p>");

}

?>
Return current item: OpenCabling