Changeset 9082

Show
Ignore:
Timestamp:
03/05/10 01:05:27 (2 years ago)
Author:
p_lindheimer
Message:

first pass at migration code, currently bypassed so no effect, re #4110 for outbound routes changes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • freepbx/trunk/upgrades/2.8.0alpha1/tables.php

    r8705 r9082  
    33global $db; 
    44 
    5 if($amp_conf["AMPDBENGINE"] != "sqlite3")  { 
    6   outn("Alter table incomming to increase extension lengths.. "); 
    7   $result = $db->query("ALTER TABLE `incoming` CHANGE `extension` `extension` varchar(50) NOT NULL");   
    8   if (DB::IsError($result)) { 
    9     out("ERROR ALTER TABLE FAILED"); 
     5// TODO: exit here for now. This will be migration code for new routes once the work is finished. 
     6exit; 
     7 
     8//TODO: DEBUG remove and restart for testing 
     9// 
     10$result = $db->query("DROP TABLE IF EXISTS `outbound_routes`"); 
     11$result = $db->query("DROP TABLE IF EXISTS `outbound_route_patterns`"); 
     12$result = $db->query("DROP TABLE IF EXISTS `outbound_route_trunks`"); 
     13$result = $db->query("DROP TABLE IF EXISTS `outbound_route_sequence`"); 
     14 
     15$outbound_routes = " 
     16CREATE TABLE outbound_routes ( 
     17  `route_id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, 
     18  `name` VARCHAR( 40 ), 
     19  `outcid` VARCHAR( 40 ), 
     20  `outcid_override_exten` VARCHAR( 20 ), 
     21  `password` VARCHAR( 30 ), 
     22  `emergency_route` VARCHAR( 4 ), 
     23  `intra_company_route` VARCHAR( 4 ), 
     24  `mohclass` VARCHAR( 80 ), 
     25  `time_group_id` INTEGER DEFAULT NULL 
     26
     27"; 
     28 
     29$outbound_route_patterns = " 
     30CREATE TABLE outbound_route_patterns ( 
     31  `route_id` INTEGER NOT NULL, 
     32  `match_pattern_prefix` VARCHAR( 60 ), 
     33  `match_pattern_pass` VARCHAR( 60 ), 
     34  `prepend_digits` VARCHAR( 100 ), 
     35  PRIMARY KEY (`route_id`, `match_pattern_prefix`, `match_pattern_pass`) 
     36
     37"; 
     38 
     39$outbound_route_trunks = " 
     40CREATE TABLE outbound_route_trunks ( 
     41  `route_id` INTEGER NOT NULL, 
     42  `trunk_id` INTEGER NOT NULL, 
     43  `seq` INTEGER NOT NULL, 
     44  PRIMARY KEY  (`route_id`, `trunk_id`, `seq`)  
     45
     46"; 
     47 
     48$outbound_route_sequence = " 
     49CREATE TABLE outbound_route_sequence ( 
     50  `route_id` INTEGER NOT NULL, 
     51  `seq` INTEGER NOT NULL, 
     52  PRIMARY KEY  (`route_id`, `seq`)  
     53
     54"; 
     55 
     56outn("Create new outbound_routes table.. "); 
     57$result = $db->query($outbound_routes); 
     58if (DB::IsError($result) && $result->getCode() == DB_ERROR_ALREADY_EXISTS ) { 
     59  out("Table exists, skipping migration"); 
     60} elseif (DB::IsError($result)) { 
     61  out("failed, FATAL Error"); 
     62  out($result->getMessage()); 
     63} else { 
     64  out("ok"); 
     65  outn("create outbound_route_patterns.. "); 
     66  $result = $db->query($outbound_route_patterns); 
     67  if (DB::IsError($result) && $result->getCode() != DB_ERROR_ALREADY_EXISTS ) { 
     68    out("failed, FATAL Error"); 
     69    out($result->getDebugInfo()); 
    1070  } else { 
    11     out("Altered"); 
     71    out("ok"); 
     72    outn("create outbound_route_trunks.. "); 
     73    $result = $db->query($outbound_route_trunks); 
     74    if (DB::IsError($result) && $result->getCode() != DB_ERROR_ALREADY_EXISTS ) { 
     75      out("failed, FATAL Error"); 
     76      out($result->getDebugInfo()); 
     77    } else { 
     78      out("ok"); 
     79      outn("create outbound_route_sequence.. "); 
     80      $result = $db->query($outbound_route_sequence); 
     81      if (DB::IsError($result) && $result->getCode() != DB_ERROR_ALREADY_EXISTS ) { 
     82        out("failed, FATAL Error"); 
     83        out($result->getDebugInfo()); 
     84      } else { 
     85        out("ok"); 
     86 
     87        $routepriority = core_routing_getroutenames(); 
     88 
     89        $routes = array(); 
     90        $accum = array(); 
     91        $dialpattern = array(); 
     92        $turnkpriority = array(); 
     93        foreach ($routepriority as $route) { 
     94          $extdisplay = $route[0]; 
     95          $accum[] = substr($extdisplay,4); 
     96   
     97          $routecid_array = core_routing_getroutecid($extdisplay); 
     98          $accum[] = $routecid_array['routecid']; 
     99          $accum[] = $routecid_array['routecid_mode']; 
     100 
     101          $accum[] = core_routing_getroutepassword($extdisplay); 
     102          $accum[] = core_routing_getrouteemergency($extdisplay); 
     103          $accum[] = core_routing_getrouteintracompany($extdisplay); 
     104          $accum[] = core_routing_getroutemohsilence($extdisplay); 
     105 
     106          $dialpattern[$extdisplay] = core_routing_getroutepatterns($extdisplay); 
     107          $trunkpriority[$extdisplay] = core_routing_getroutetrunks($extdisplay); 
     108 
     109          $routes[$extdisplay] = $accum; 
     110          unset($accum); 
     111        } 
     112 
     113        $compiled = $db->prepare('INSERT INTO `outbound_routes` (`name`, `outcid`, `outcid_override_exten`, `password`, `emergency_route`, `intra_company_route`, `mohclass`) values (?,?,?,?,?,?,?)'); 
     114        $result = $db->executeMultiple($compiled,$routes); 
     115        if(DB::IsError($result)) { 
     116          out("FATAL: ".$result->getDebugInfo()."\n".'error inserting into outbound_routes table');  
     117        } 
     118        $route_ids = $db->getCol('SELECT `route_id` FROM `outbound_routes` ORDER BY `route_id`'); 
     119        if(DB::IsError($route_ids)) { 
     120          out("FATAL: ".$route_ids->getDebugInfo()."\n".'error getting route_ids to create outbound_route_sequence');  
     121        } 
     122        // assumption here is that routepriorities always return in order, I think that is correct, which means we inserted in order 
     123        // TODO: update existing pinsets here whether enabled or not 
     124        $seq = 0; 
     125        $outbound_route_sequence = array(); 
     126        foreach ($route_ids as $route_id) { 
     127          outn("processing route_id $route_id.."); 
     128          $outbound_route_sequence[] = array($route_id,$seq); 
     129          $seq++; 
     130 
     131          $this_patterns = array_shift($dialpattern); 
     132 
     133          $insert_patterns = array(); 
     134          foreach ($this_patterns as $pattern) { 
     135            $parts = explode('|',$pattern,2); 
     136            if (count($parts) == 1) { 
     137              $insert_patterns[] = array($route_id, '', $pattern); 
     138            } else { 
     139              $insert_patterns[] = array($route_id, $parts[0], $parts[1]); 
     140            } 
     141          } 
     142          $compiled = $db->prepare('INSERT INTO `outbound_route_patterns` (`route_id`, `match_pattern_prefix`, `match_pattern_pass`) values (?,?,?)'); 
     143          $result = $db->executeMultiple($compiled,$insert_patterns); 
     144          if(DB::IsError($result)) { 
     145            out("FATAL: ".$result->getDebugInfo()."\n".'error inserting into outbound_route_patterns table');  
     146          } else { 
     147            outn('patterns..'); 
     148          } 
     149          unset($insert_pattern); 
     150 
     151          $this_trunks = array_shift($trunkpriority); 
     152          $trunk_seq = 0; 
     153          $insert_trunks = array(); 
     154          foreach ($this_trunks as $trunk) { 
     155            $insert_trunks[] = array($route_id, substr($trunk,4),$trunk_seq); 
     156            $trunk_seq++; 
     157          } 
     158 
     159          $compiled = $db->prepare('INSERT INTO `outbound_route_trunks` (`route_id`, `trunk_id`, `seq`) values (?,?,?)'); 
     160          $result = $db->executeMultiple($compiled,$insert_trunks); 
     161          if(DB::IsError($result)) { 
     162            out("FATAL: ".$result->getDebugInfo()."\n".'error inserting into outbound_route_trunks table');  
     163          } else { 
     164            outn('trunks..'); 
     165          } 
     166          unset($insert_trunks); 
     167          out("migrated"); 
     168        } 
     169 
     170        $compiled = $db->prepare('INSERT INTO `outbound_route_sequence` (`route_id`, `seq`) values (?,?)'); 
     171        $result = $db->executeMultiple($compiled,$outbound_route_sequence); 
     172        if(DB::IsError($result)) { 
     173          out("FATAL: ".$result->getDebugInfo()."\n".'error inserting into outbound_route_sequence table');  
     174        } 
     175      } 
     176    } 
    12177  } 
    13 } else { 
    14   out("WARNING: column extension in incoming table has NOT been altered to 50 from 20 and will need to be addressed manually"); 
    15178} 
    16 ?>