Assembla home | Assembla project page
 

root/trunk/application/controllers/migrate.php

Revision 3, 6.3 kB (checked in by m4rw3r, 5 months ago)

A bit more on the migrate controller
Thanks to the inparo, elitemedia and the Linkster guidelines, I've added a lot more empty lines and comments to IgnitedQuery to improve readability :P
A bit more on the IgnitedQuery unit test
select() now has a switch for protect_identifiers
unions of arrays didn't work as expected, switched to array_merge()
added select_count(), is it possible?
_select_func() also has this protect_identifiers switch
or_where_not_in() was missing
Started on INSERT

Line 
1 <?php
2 /*
3  * Created on 2008 Jun 22
4  * by Martin Wernstahl <m4rw3r@gmail.com>
5  */
6 class migrate extends Controller{
7     var $ignore_classes = array('array','array_object','migrate','controller');
8     function migrate(){
9         parent::Controller();
10         $this->load->database();
11         $this->load->dbforge();
12         $this->load->helper('url');
13         if(file_exists(APPPATH.'/current_db_ver.txt'))
14             $this->current = file_get_contents(APPPATH.'/current_db_ver.txt');
15         else
16             $this->current = 0;
17     }
18     function index(){
19         $classes = array();
20         foreach(get_declared_classes() as $class){
21             if(stripos($class,'migrate') !== false && $class != 'migrate' && class_exists($class)){
22                 $classes[] = $class;
23             }
24         }
25 ?>
26 <ul>
27 <?php foreach($classes as $class){
28     $version = substr($class,8);
29     echo "<li>".anchor('migrate/to/'.$version,$version)."</li>";
30 }
31 ?>
32 </ul>
33
34 <?php
35     }
36     function to($version){
37         if($version == $this->current){
38             echo "<p>already at version $version</p>";
39             return;
40         }
41         if($this->current < $version)
42             $inc = 2;
43         else
44             $inc = 0;
45         for($i = $this->current + $inc; ($i <= $version && $inc == 2) || ($i > $version && $inc == 0); $i = $i - 1 + $inc){
46             $name = "migrate_".str_pad($i,3,'0',STR_PAD_LEFT);
47             $obj = new $name();
48             if($inc == 1)
49                 $obj->up($this);
50             else
51                 $obj->down($this);
52         }
53         file_put_contents(APPPATH.'/current_db_ver.txt',$version);
54         echo "<p>Done migrating to version $version</p>";
55     }
56 }
57 class migrate_002{
58     function up($CI){
59         echo "going to version 2";
60     }
61     function down($CI){
62         echo "going down from version 2";
63     }
64 }
65 class migrate_001 extends IRM{
66     function up(&$CI){
67         echo "going to version 1";
68         /*table('testaaaa')
69             ->column('id')
70                 ->type('int')
71                 ->options(array('auto_increment' => true,
72                     'unsigned' => true))
73                 ->primary(true)
74                 ->end()
75             ->column('lft')
76                 ->type('int')
77                 ->options(array('unsigned' => true))
78                 ->index(true)
79                 ->end()
80             ->column('rgt')
81                 ->type('int')
82                 ->options(array('unsigned' => true))
83                 ->end()
84             ->column('title')
85                 ->type('varchar')
86                 ->options(array('constraint' => 100))
87                 ->end()
88                     ->execute();*/
89     }
90 }
91 /**
92  *
93  */
94 abstract class IRM{
95     function IRM(){
96         $CI =& get_instance();
97         $this->db =& $CI->db;
98         $this->dbforge =& $CI->dbforge;
99     }
100     abstract function up(&$CI);
101     function down(&$CI){
102         echo '!!!!';
103     }
104 }
105 class IRM_table{
106     var $columns = array();
107     var $name;
108     var $new_name;
109     var $db;
110     var $edited = true;
111     var $new = true;
112     function IRM_table($name, $options){
113         $this->name = $name;
114         $this->options = $options;
115         $CI =& get_instance();
116         $this->db =& $CI->db;
117         $this->dbforge =& $CI->dbforge;
118         $this->_load();
119     }
120     function _load(){
121         if($this->db->table_exists($this->name)){
122             foreach($this->db->list_fields($this->name) as $col){
123                 $obj  = new IRM_column($this->name,array($col, 'table_obj' => &$this));
124                 $obj->existing_name = $col;
125                 $obj->edited = false;
126                 $this->columns[] = $obj;
127             }
128             $this->edited = false;
129             $this->new = false;
130         }
131     }
132     function &rename($name){
133         $this->new_name = $name;
134         return $this;
135     }
136     function &column($name = null, $type = null, $options = array()){
137         foreach($this->columns as $col){
138             if($col->name == $name)
139                 return $col;
140         }
141         $args = array_merge(func_get_args(), array('table_obj' => &$this));
142         $col = new IRM_column($this->name, $args);
143         $this->columns[] =& $col;
144         return $col;
145     }
146     function drop(){
147         $this->dbforge->drop_table($this->name);
148     }
149     function execute(){
150         if($this->new){
151             foreach($this->columns as $column){
152                 if($column->edited == true)
153                     $column->execute(true);
154             }
155         }
156         if($this->edited == true)
157             $this->dbforge->create_table($this->name, TRUE);
158         if(!$this->new){
159             foreach($this->columns as $column){
160                 if($column->edited == true)
161                     $column->execute();
162             }
163         }
164         if($this->new_name){
165             $this->dbforge->rename_table($this->name,$this->new_name);
166             $this->name = $this->new_name;
167             foreach($this->columns as $col){
168                 $col->table = $this->name;
169             }
170         }
171     }
172 }
173 class IRM_column{
174     var $table;
175     /**
176      * The new settings for this column
177      */
178     var $opts;
179     var $name;
180     var $existing_name;
181     var $type;
182     var $edited = true;
183     var $drop = false;
184     var $index = false;
185     var $primary = false;
186     function IRM_column($table, $options = array()){
187         $this->table = $table;
188         $this->name = isset($options[0]) && $options[0] != null ? $options[0] : null;
189         $this->type = isset($options[1]) && $options[1] != null ? $options[1] : null;
190         $this->opts = isset($options[2]) && $options[2] != null ? $options[2] : array();
191         $this->table_obj = isset($options['table_obj']) ? $options['table_obj'] : null;
192         $CI =& get_instance();
193         $this->db =& $CI->db;
194         $this->dbforge =& $CI->dbforge;
195     }
196     function &name($name){
197         $this->name = $name;
198         $this->edited = true;
199         return $this;
200     }
201     function &type($type){
202         $this->type = $type;
203         $this->edited = true;
204         return $this;
205     }
206     function &primary($val = true){
207         $this->primary = $val;
208         $this->edited = true;
209         return $this;
210     }
211     function &index($val = true){
212         $this->index = $val;
213         $this->edited = true;
214         return $this;
215     }
216     function &options($options){
217         $this->opts = $options;
218         $this->edited = true;
219         return $this;
220     }
221     function &drop(){
222         $this->drop = true;
223         $this->edited = true;
224         return $this;
225     }
226     function execute($new = false){
227         if($this->edited){
228             if($this->drop && !$new){
229                 $this->dbforge->drop_column($this->table, $this->name);
230                 return;
231             }
232             if(!$new && $this->existing_name != null && $this->existing_name != $this->name){
233                 // rename
234                 $opts = array_merge(array(
235                         'name' => $this->name,'type' => $this->type),$this->opts);
236                 $opts = array(
237                         $this->existing_name => $opts);
238                 $this->dbforge->modify_column($this->table,$opts);
239             }
240             else{
241                 // add
242                 $opts = array_merge(array('type' => $this->type),$this->opts);
243                 $opts = array($this->name => $opts);
244                 if($new){
245                     $this->dbforge->add_field($opts);
246                 }
247                 else{
248                     $this->dbforge->add_column($this->table,$opts);
249                 }
250             }
251             if($this->primary){
252                 $this->dbforge->add_key($this->name, true);
253             }
254             if($this->index){
255                 $this->dbforge->add_key($this->name);
256             }
257         }
258     }
259     function &end(){
260         return $this->table_obj;
261     }
262 }
263 function &table($name, $options = array()){
264     $table = new IRM_table($name, $options);
265     return $table;
266 }
267 function &add_column($table, $name, $type = 'int', $options = array()){
268     $obj = new IRM_column($table,array($name,$type,$options));
269     $obj->execute();
270 }
271 ?>
Note: See TracBrowser for help on using the browser.