root/modules/branches/2.5/cidlookup/install.php

Revision 6277, 3.1 kB (checked in by ethans, 5 years ago)

Sqlite3-related fixes that have grown since 2.3. Closes #2987

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
Line 
1 <?php
2
3 global $db;
4 global $amp_conf;
5
6 if (! function_exists("out")) {
7   function out($text) {
8     echo $text."<br />";
9   }
10 }
11
12 if (! function_exists("outn")) {
13   function outn($text) {
14     echo $text;
15   }
16 }
17
18 $autoincrement = (($amp_conf["AMPDBENGINE"] == "sqlite") || ($amp_conf["AMPDBENGINE"] == "sqlite3")) ? "AUTOINCREMENT":"AUTO_INCREMENT";
19
20 // create the tables
21 $sql = "CREATE TABLE IF NOT EXISTS cidlookup (
22   cidlookup_id INTEGER NOT NULL PRIMARY KEY $autoincrement,
23   description varchar(50) NOT NULL,
24   sourcetype varchar(100) NOT NULL,
25   cache tinyint(1) NOT NULL default '0',
26   deptname varchar(30) default NULL,
27   http_host varchar(30) default NULL,
28   http_port varchar(30) default NULL,
29   http_username varchar(30) default NULL,
30   http_password varchar(30) default NULL,
31   http_path varchar(100) default NULL,
32   http_query varchar(100) default NULL,
33   mysql_host varchar(60) default NULL,
34   mysql_dbname varchar(60) default NULL,
35   mysql_query text,
36   mysql_username varchar(30) default NULL,
37   mysql_password varchar(30) default NULL
38 );";
39 $check = $db->query($sql);
40 if (DB::IsError($check)) {
41         die_freepbx( "Can not create `cidlookup` table: " . $check->getMessage() .  "\n");
42 }
43
44
45 $sql = "CREATE TABLE IF NOT EXISTS cidlookup_incoming (
46   cidlookup_id INT NOT NULL,
47   extension VARCHAR(50),
48   cidnum VARCHAR(30)
49 );";
50 $check = $db->query($sql);
51 if (DB::IsError($check)) {
52         die_freepbx( "Can not create `cidlookup_incomming` table: " . $check->getMessage() .  "\n");
53 }
54
55 // first update
56 $sql = "SELECT cache FROM cidlookup";
57 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
58 if (DB::IsError($check)) {
59   // add new field
60   $sql = "ALTER TABLE cidlookup ADD cache INTEGER NOT NULL DEFAULT 0;";
61   $result = $db->query($sql);
62   if(DB::IsError($result)) {
63     die_freepbx($result->getMessage());
64   }
65 }
66
67 outn("Migrating channel routing to Zap DID routing..");
68 $sql = "SELECT channel FROM cidlookup_incoming";
69 $check = @$db->query($sql);
70 if (!DB::IsError($check)) {
71   $chan_prefix = 'zapchan';
72   $sql = "UPDATE cidlookup_incoming SET extension=CONCAT('$chan_prefix',channel), channel='' WHERE channel != ''";
73   $results = $db->query($sql);
74   if (DB::IsError($results)) {
75     out("FATAL: failed to transform old routes: ".$results->getMessage());
76   } else {
77     out("OK");
78     // ALTER...DROP is not supported by sqlite3.  This table was setup properly in the CREATE anyway
79     if($amp_conf["AMPDBENGINE"] != "sqlite3")  {
80       outn("Removing deprecated channel field from incoming..");
81       $sql = "ALTER TABLE cidlookup_incoming DROP channel";
82       $results = $db->query($sql);
83       if (DB::IsError($results)) {
84       out("ERROR: failed: ".$results->getMessage());
85       } else {
86         out("OK");
87       }
88     }
89   }
90 } else {
91   out("Not Needed");
92 }
93
94 // This field had been wrongfully added to incoming quite some time ago
95 // this should maybe be added to core as well
96 //
97 // ALTER...DROP is not supported by sqlite3.  This table was setup properly in the CREATE anyway
98 if($amp_conf["AMPDBENGINE"] != "sqlite3")  {
99
100   outn("Checking for cidlookup field in core's incoming table..");
101   $sql = "ALTER TABLE incoming DROP cidlookup";
102   $results = $db->query($sql);
103   if (DB::IsError($results)) {
104     out("not present");
105   } else {
106   out("removed");
107   }
108 }
109 ?>
110
Note: See TracBrowser for help on using the browser.