blob: 4bf1d0dc1ca3d59823a43792d147f1edb1241bc3 [file] [log] [blame]
Phil Sturgeon9758d842011-02-07 20:39:00 +00001<?php defined('BASEPATH') OR exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * @package CodeIgniter
8 * @author EllisLab Dev Team
9 * @copyright Copyright (c) 2006 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Migration Class
20 *
21 * All migrations should implement this, forces up() and down() and gives
22 * access to the CI super-global.
23 *
24 * @package CodeIgniter
25 * @subpackage Libraries
26 * @category Libraries
27 * @author Reactor Engineers
28 * @link
29 */
30class CI_Migration {
31
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010032 protected $_migration_enabled = FALSE;
33 protected $_migration_path = NULL;
34 protected $_migration_version = 0;
Phil Sturgeon9758d842011-02-07 20:39:00 +000035
36 public $error = '';
37
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010038 public function __construct($config = array())
Phil Sturgeon9758d842011-02-07 20:39:00 +000039 {
40 # Only run this constructor on main library load
41 if (get_parent_class($this) !== FALSE)
42 {
43 return;
44 }
45
46 foreach ($config as $key => $val)
47 {
48 $this->{'_' . $key} = $val;
49 }
50
51 log_message('debug', 'Migrations class initialized');
52
53 // Are they trying to use migrations while it is disabled?
54 if ($this->_migration_enabled !== TRUE)
55 {
56 show_error('Migrations has been loaded but is disabled or set up incorrectly.');
57 }
58
59 // If not set, set it
60 $this->_migration_path == '' OR $this->_migration_path = APPPATH . 'migrations/';
61
62 // Add trailing slash if not set
63 $this->_migration_path = rtrim($this->_migration_path, '/').'/';
64
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010065 // Load migration language
66 $this->lang->load('migration');
67
Phil Sturgeon9758d842011-02-07 20:39:00 +000068 // They'll probably be using dbforge
69 $this->load->dbforge();
70
71 // If the migrations table is missing, make it
72 if ( ! $this->db->table_exists('migrations'))
73 {
74 $this->dbforge->add_field(array(
75 'version' => array('type' => 'INT', 'constraint' => 3),
76 ));
77
78 $this->dbforge->create_table('migrations', TRUE);
79
80 $this->db->insert('migrations', array('version' => 0));
81 }
82 }
83
84 // --------------------------------------------------------------------
85
86 /**
87 * Migrate to a schema version
88 *
89 * Calls each migration step required to get to the schema version of
90 * choice
91 *
92 * @access public
93 * @param $version integer Target schema version
94 * @return mixed TRUE if already latest, FALSE if failed, int if upgraded
95 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010096 public function version($target_version)
Phil Sturgeon9758d842011-02-07 20:39:00 +000097 {
98 $start = $current_version = $this->_get_version();
99 $stop = $target_version;
100
101 if ($target_version > $current_version)
102 {
103 // Moving Up
104 ++$start;
105 ++$stop;
106 $step = 1;
107 }
108
109 else
110 {
111 // Moving Down
112 $step = -1;
113 }
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100114
Phil Sturgeon9758d842011-02-07 20:39:00 +0000115 $method = $step === 1 ? 'up' : 'down';
116 $migrations = array();
117
118 // We now prepare to actually DO the migrations
119 // But first let's make sure that everything is the way it should be
120 for ($i = $start; $i != $stop; $i += $step)
121 {
122 $f = glob(sprintf($this->_migration_path . '%03d_*.php', $i));
123
124 // Only one migration per step is permitted
125 if (count($f) > 1)
126 {
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100127 $this->error = sprintf($this->lang->line('migration_multiple_version'), $i);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000128 return FALSE;
129 }
130
131 // Migration step not found
132 if (count($f) == 0)
133 {
134 // If trying to migrate up to a version greater than the last
135 // existing one, migrate to the last one.
136 if ($step == 1)
137 {
138 break;
139 }
140
141 // If trying to migrate down but we're missing a step,
142 // something must definitely be wrong.
143 $this->error = sprintf($this->lang->line('migration_not_found'), $i);
144 return FALSE;
145 }
146
147 $file = basename($f[0]);
148 $name = basename($f[0], '.php');
149
150 // Filename validations
151 if (preg_match('/^\d{3}_(\w+)$/', $name, $match))
152 {
153 $match[1] = strtolower($match[1]);
154
155 // Cannot repeat a migration at different steps
156 if (in_array($match[1], $migrations))
157 {
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100158 $this->error = sprintf($this->lang->line('migration_multiple_version'), $match[1]);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000159 return FALSE;
160 }
161
162 include $f[0];
163 $class = 'Migration_' . ucfirst($match[1]);
164
165 if ( ! class_exists($class))
166 {
167 $this->error = sprintf($this->lang->line('migration_class_doesnt_exist'), $class);
168 return FALSE;
169 }
170
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100171 if ( ! is_callable(array($class, $method)))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000172 {
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100173 $this->error = sprintf($this->lang->line('migration_missing_'.$method.'_method'), $class);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000174 return FALSE;
175 }
176
177 $migrations[] = $match[1];
178 }
179 else
180 {
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100181 exit('313');
182 $this->error = sprintf($this->lang->line('migration_invalid_filename'), $file);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000183 return FALSE;
184 }
185 }
186
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100187 log_message('debug', 'Current migration: ' . $current_version);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000188
189 $version = $i + ($step == 1 ? -1 : 0);
190
191 // If there is nothing to do so quit
192 if ($migrations === array())
193 {
194 return TRUE;
195 }
196
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100197 log_message('debug', 'Migrating from ' . $method . ' to version ' . $version);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000198
199 // Loop through the migrations
200 foreach ($migrations AS $migration)
201 {
202 // Run the migration class
203 $class = 'Migration_' . ucfirst(strtolower($migration));
204 call_user_func(array(new $class, $method));
205
206 $current_version += $step;
207 $this->_update_version($current_version);
208 }
209
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100210 log_message('debug', 'Finished migrating to '.$current_version);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000211
212 return $current_version;
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Set's the schema to the latest migration
219 *
220 * @access public
221 * @return mixed true if already latest, false if failed, int if upgraded
222 */
223 public function latest()
224 {
225 if ( ! $migrations = $this->find_migrations())
226 {
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100227 $this->error = $this->line->lang('migration_none_found');
Phil Sturgeon9758d842011-02-07 20:39:00 +0000228 return false;
229 }
230
231 $last_migration = basename(end($migrations));
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100232
Phil Sturgeon9758d842011-02-07 20:39:00 +0000233 // Calculate the last migration step from existing migration
234 // filenames and procceed to the standard version migration
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100235 return $this->version((int) substr($last_migration, 0, 3));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Set's the schema to the migration version set in config
242 *
243 * @access public
244 * @return mixed true if already current, false if failed, int if upgraded
245 */
246 public function current()
247 {
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100248 return $this->version($this->_migration_version);
249 }
250
251 // --------------------------------------------------------------------
252
253 /**
254 * Error string
255 *
256 * @access public
257 * @return string Error message returned as a string
258 */
259 public function error_string()
260 {
261 return $this->error;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * Set's the schema to the latest migration
268 *
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100269 * @access protected
Phil Sturgeon9758d842011-02-07 20:39:00 +0000270 * @return mixed true if already latest, false if failed, int if upgraded
271 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100272 protected function find_migrations()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000273 {
274 // Load all *_*.php files in the migrations path
275 $files = glob($this->_migration_path . '*_*.php');
276 $file_count = count($files);
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100277
Phil Sturgeon9758d842011-02-07 20:39:00 +0000278 for ($i = 0; $i < $file_count; $i++)
279 {
280 // Mark wrongly formatted files as false for later filtering
281 $name = basename($files[$i], '.php');
282 if ( ! preg_match('/^\d{3}_(\w+)$/', $name))
283 {
284 $files[$i] = FALSE;
285 }
286 }
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100287
Phil Sturgeon9758d842011-02-07 20:39:00 +0000288 sort($files);
289
290 return $files;
291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Retrieves current schema version
297 *
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100298 * @access protected
299 * @return integer Current Migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000300 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100301 protected function _get_version()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000302 {
303 $row = $this->db->get('migrations')->row();
304 return $row ? $row->version : 0;
305 }
306
307 // --------------------------------------------------------------------
308
309 /**
310 * Stores the current schema version
311 *
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100312 * @access protected
313 * @param $migrations integer Migration reached
Phil Sturgeon9758d842011-02-07 20:39:00 +0000314 * @return void Outputs a report of the migration
315 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100316 protected function _update_version($migrations)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000317 {
318 return $this->db->update('migrations', array(
319 'version' => $migrations
320 ));
321 }
322
323 // --------------------------------------------------------------------
324
325 /**
Phil Sturgeon9758d842011-02-07 20:39:00 +0000326 * Enable the use of CI super-global
327 *
328 * @access public
329 * @param $var
330 * @return mixed
331 */
332 public function __get($var)
333 {
334 return get_instance()->$var;
335 }
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100336}
337
338/* End of file Migration.php */
339/* Location: ./system/libraries/Migration.php */