root/trunk/application/controllers/migrate.php
| 1 | 3 | ||
|---|---|---|---|
4 | * by Martin Wernstahl <m4rw3r@gmail.com> | 4 | * by Martin Wernstahl <m4rw3r@gmail.com> |
5 | */ | 5 | */ |
6 | class migrate extends Controller{ | 6 | class migrate extends Controller{ |
7 | var $ignore_classes = array('array','array_object','migrate','controller'); | ||
7 | function migrate(){ | 8 | function migrate(){ |
8 | parent::Controller(); | 9 | parent::Controller(); |
9 | $this->load->database(); | 10 | $this->load->database(); |
10 | $this->load->dbforge(); | 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; | ||
11 | } | 17 | } |
12 | function index(){ | 18 | function index(){ |
13 | 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 | ||
14 | } | 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 | } | ||
15 | } | 56 | } |
16 | class amigrate_001 extends IRM{ | 57 | class migrate_002{ |
17 | function up(){ | 58 | function up($CI){ |
18 | table('testaaaa') | 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') | ||
19 | ->column('id') | 69 | ->column('id') |
20 | ->type('int') | 70 | ->type('int') |
21 | ->options(array('auto_increment' => true, | 71 | ->options(array('auto_increment' => true, |
... | ... | ||
35 | ->type('varchar') | 85 | ->type('varchar') |
36 | ->options(array('constraint' => 100)) | 86 | ->options(array('constraint' => 100)) |
37 | ->end() | 87 | ->end() |
38 | ->execute(); | 88 | ->execute();*/ |
39 | } | 89 | } |
40 | } | 90 | } |
41 | /** | 91 | /** |
... | ... | ||
47 | $this->db =& $CI->db; | 97 | $this->db =& $CI->db; |
48 | $this->dbforge =& $CI->dbforge; | 98 | $this->dbforge =& $CI->dbforge; |
49 | } | 99 | } |
50 | abstract function up(); | 100 | abstract function up(&$CI); |
51 | function down(){ | 101 | function down(&$CI){ |
52 | return '!!!!'; | 102 | echo '!!!!'; |
53 | } | 103 | } |
54 | } | 104 | } |
55 | class IRM_table{ | 105 | class IRM_table{ |
Download diff