final move of files
[web.mtrack] / MTrack / DBSchema / mysql.php
1 <?php
2
3
4 require_once 'DBSchema/Generic.php';
5
6 class MTrackDBSchema_mysql extends MTrackDBSchema_Generic 
7 {
8     var $typemap = array(
9         'autoinc' =>  'INT(11) NOT NULL AUTO_INCREMENT',
10         'timestamp' => 'datetime',
11         'blob' => 'longtext', // eak what blob is stored?
12         'text' => 'VARCHAR(128)',
13     );
14
15
16     function determineVersion() {
17         $ret = parent::determineVersion();
18         if ($ret) {
19             return $ret;
20         }
21         parse_str(str_replace(array(";",":"), "&", $this->db->dsn), $dsn);
22         
23         try {
24             $this->db->query("CREATE DATABASE {$dsn['dbname']}");
25         } catch (Exception $e) {
26             // technically this should not get here..
27             // as pdo's dsn design is borked..
28         }
29          
30          
31         // assume we need to create database..
32         return null;
33     }
34     
35     
36     function computeFieldCreate($f)
37     {
38         $str = "\t`$f->name` ";
39         $str .= isset($this->typemap[$f->type]) ? $this->typemap[$f->type] : $f->type;
40         if (isset($f->nullable) && $f->nullable == '0') {
41           $str .= ' NOT NULL ';
42         }
43         if (!isset($f->default)) {
44             return $str;
45         }
46         if (!strlen($f->default)) {
47             return $str . " DEFAULT ''";
48         }
49         
50         return $str . ($f->default == 'CURRENT_TIMESTAMP' ? '' : 'DEFAULT '. $f->default);
51     }
52     
53     // may as well rewrite this.. as we have too loop throug hit anyway..
54     function createTable(MTrackDBSchema_Table $table)
55     {
56         echo "Create $table->name\n";
57
58         $pri_key = null;
59         $sql = array();
60         foreach ($table->fields as $f) {
61           if ($f->type == 'autoinc') {
62             $pri_key = $f->name;
63             
64           }
65           $str = $this->computeFieldCreate($f);
66           $sql[] = $str;
67         }
68         if (is_array($table->keys)) {
69             
70             foreach ($table->keys as $kn=>$k) {
71               if ($k->type != 'primary') continue;
72              //   if ($pri_key !== null) continue;
73                 $sql[] = "\tprimary key (`" . join('`, `', $k->fields) . "`)";
74             }
75         }
76
77         $sql = "CREATE TABLE $table->name (\n" .
78           join(",\n", $sql) .
79           ")\n";
80
81         echo $sql;
82
83         $this->db->exec($sql);
84     }
85   function alterTable(MTrackDBSchema_Table $from, MTrackDBSchema_Table $to)
86   {
87     $sql = array();
88     $actions = array();
89     
90     //print_r($to);
91     /* if keys have changed, we drop the old key definitions before changing the columns */
92     if (is_array($from->keys)) foreach ($from->keys as $k) {
93       if (!isset($to->keys[$k->name]) || $to->keys[$k->name] != $k) {
94         if ($k->type == 'primary') {
95           $actions[] = "DROP PRIMARY KEY";
96         } else {
97           $actions[] = "DROP INDEX $k->name";
98         }
99       }
100     }
101
102     foreach ($from->fields as $f) {
103         
104         if (isset($to->fields[$f->name])) {
105             continue;
106         }
107         // let's check to see if it got renamed..
108         $found = false;
109         foreach($to->fields as $ff) {
110             
111             if ($ff->oldname == $f->name) {
112                 $actions[] = "CHANGE COLUMN `$f->name` " .  $this->computeFieldCreate($ff);
113                 unset($to->fields[$ff->name]);
114                 $found = true;
115             }
116         }
117         if ($found) continue;
118         $actions[] = "DROP COLUMN $f->name";
119          
120       
121     }
122     foreach ($to->fields as $f) {
123       if (isset($from->fields[$f->name])) continue;
124       $actions[] = "ADD COLUMN " . $this->computeFieldCreate($f);
125     }
126
127     /* changed and new keys */
128     if (is_array($from->keys)) foreach ($from->keys as $k) {
129       if (isset($to->keys[$k->name]) && $to->keys[$k->name] != $k) {
130         if ($k->type == 'primary') {
131           $actions[] = "ADD primary key (" . join(', ', $k->fields) . ")";
132         } else {
133           $sql[] = $this->computeIndexCreate($to, $k);
134         }
135       }
136     }
137     if (is_array($to->keys)) foreach ($to->keys as $k) {
138       if (isset($from->keys[$k->name])) continue;
139       if ($k->type == 'primary') {
140         $actions[] = "ADD primary key (" . join(', ', $k->fields) . ")";
141       } else {
142         $sql[] = $this->computeIndexCreate($to, $k);
143       }
144     }
145
146     if (count($actions)) {
147       $sql[] = "ALTER TABLE $from->name " . join(",\n", $actions);
148     }
149     echo "Need to alter $from->name\n";
150     echo "SQL:\n";
151     var_dump($sql);
152     foreach ($sql as $s) {
153       $this->db->exec($s);
154     }
155   }
156 }