blob: 94961b5685fe98a693753bde2116c91c3b7eeeb5 [file] [log] [blame]
Derek Jonesf4a4bd82011-10-20 12:18:42 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Phil Sturgeon9758d842011-02-07 20:39:00 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnesdd81c432011-11-16 11:07:35 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnesdd81c432011-11-16 11:07:35 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Phil Sturgeon9758d842011-02-07 20:39:00 +000019 * @package CodeIgniter
20 * @author EllisLab Dev Team
Derek Jonesf4a4bd82011-10-20 12:18:42 -050021 * @copyright Copyright (c) 2006 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Phil Sturgeon9758d842011-02-07 20:39:00 +000023 * @link http://codeigniter.com
Derek Jonesf4a4bd82011-10-20 12:18:42 -050024 * @since Version 3.0
Phil Sturgeon9758d842011-02-07 20:39:00 +000025 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Migration Class
32 *
33 * All migrations should implement this, forces up() and down() and gives
34 * access to the CI super-global.
35 *
36 * @package CodeIgniter
37 * @subpackage Libraries
38 * @category Libraries
39 * @author Reactor Engineers
40 * @link
41 */
42class CI_Migration {
43
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010044 protected $_migration_enabled = FALSE;
45 protected $_migration_path = NULL;
46 protected $_migration_version = 0;
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -070047 protected $_migration_table = 'migrations';
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -070048 protected $_migration_auto_latest = FALSE;
Eric Barnesdd81c432011-11-16 11:07:35 -050049
Phil Sturgeoncb06c652011-05-04 10:50:25 +010050 protected $_error_string = '';
Phil Sturgeon9758d842011-02-07 20:39:00 +000051
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010052 public function __construct($config = array())
Phil Sturgeon9758d842011-02-07 20:39:00 +000053 {
54 # Only run this constructor on main library load
55 if (get_parent_class($this) !== FALSE)
56 {
57 return;
58 }
59
60 foreach ($config as $key => $val)
61 {
62 $this->{'_' . $key} = $val;
63 }
64
65 log_message('debug', 'Migrations class initialized');
66
67 // Are they trying to use migrations while it is disabled?
68 if ($this->_migration_enabled !== TRUE)
69 {
70 show_error('Migrations has been loaded but is disabled or set up incorrectly.');
71 }
72
73 // If not set, set it
Eric Barnesdd81c432011-11-16 11:07:35 -050074 $this->_migration_path == '' AND $this->_migration_path = APPPATH.'migrations/';
Phil Sturgeon9758d842011-02-07 20:39:00 +000075
76 // Add trailing slash if not set
77 $this->_migration_path = rtrim($this->_migration_path, '/').'/';
78
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010079 // Load migration language
80 $this->lang->load('migration');
81
Phil Sturgeon9758d842011-02-07 20:39:00 +000082 // They'll probably be using dbforge
83 $this->load->dbforge();
84
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -070085 // Make sure the migration table name was set.
Cloudmanic Labs, LLC63b61e32011-09-19 09:35:05 -070086 if (empty($this->_migration_table))
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -070087 {
Eric Barnesdd81c432011-11-16 11:07:35 -050088 show_error('Migrations configuration file (migration.php) must have "migration_table" set.');
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -070089 }
90
Phil Sturgeon9758d842011-02-07 20:39:00 +000091 // If the migrations table is missing, make it
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -070092 if ( ! $this->db->table_exists($this->_migration_table))
Phil Sturgeon9758d842011-02-07 20:39:00 +000093 {
94 $this->dbforge->add_field(array(
95 'version' => array('type' => 'INT', 'constraint' => 3),
96 ));
97
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -070098 $this->dbforge->create_table($this->_migration_table, TRUE);
Phil Sturgeon9758d842011-02-07 20:39:00 +000099
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700100 $this->db->insert($this->_migration_table, array('version' => 0));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000101 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500102
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -0700103 // Do we auto migrate to the latest migration?
Eric Barnesdd81c432011-11-16 11:07:35 -0500104 if ($this->_migration_auto_latest == TRUE)
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -0700105 {
106 if ( ! $this->latest() )
107 {
108 show_error($this->error_string());
109 }
110 }
Phil Sturgeon9758d842011-02-07 20:39:00 +0000111 }
112
113 // --------------------------------------------------------------------
114
115 /**
116 * Migrate to a schema version
117 *
118 * Calls each migration step required to get to the schema version of
119 * choice
120 *
121 * @access public
122 * @param $version integer Target schema version
123 * @return mixed TRUE if already latest, FALSE if failed, int if upgraded
124 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100125 public function version($target_version)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000126 {
127 $start = $current_version = $this->_get_version();
128 $stop = $target_version;
129
130 if ($target_version > $current_version)
131 {
132 // Moving Up
133 ++$start;
134 ++$stop;
135 $step = 1;
136 }
137
138 else
139 {
140 // Moving Down
141 $step = -1;
142 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500143
Phil Sturgeon9758d842011-02-07 20:39:00 +0000144 $method = $step === 1 ? 'up' : 'down';
145 $migrations = array();
146
147 // We now prepare to actually DO the migrations
148 // But first let's make sure that everything is the way it should be
149 for ($i = $start; $i != $stop; $i += $step)
150 {
Eric Barnesdd81c432011-11-16 11:07:35 -0500151 $f = glob(sprintf($this->_migration_path.'%03d_*.php', $i));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000152
153 // Only one migration per step is permitted
154 if (count($f) > 1)
155 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100156 $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $i);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000157 return FALSE;
158 }
159
160 // Migration step not found
161 if (count($f) == 0)
162 {
163 // If trying to migrate up to a version greater than the last
164 // existing one, migrate to the last one.
165 if ($step == 1)
166 {
167 break;
168 }
169
170 // If trying to migrate down but we're missing a step,
171 // something must definitely be wrong.
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100172 $this->_error_string = sprintf($this->lang->line('migration_not_found'), $i);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000173 return FALSE;
174 }
175
176 $file = basename($f[0]);
177 $name = basename($f[0], '.php');
178
179 // Filename validations
180 if (preg_match('/^\d{3}_(\w+)$/', $name, $match))
181 {
182 $match[1] = strtolower($match[1]);
183
184 // Cannot repeat a migration at different steps
185 if (in_array($match[1], $migrations))
186 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100187 $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $match[1]);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000188 return FALSE;
189 }
190
191 include $f[0];
Eric Barnesdd81c432011-11-16 11:07:35 -0500192 $class = 'Migration_'.ucfirst($match[1]);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000193
194 if ( ! class_exists($class))
195 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100196 $this->_error_string = sprintf($this->lang->line('migration_class_doesnt_exist'), $class);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000197 return FALSE;
198 }
199
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100200 if ( ! is_callable(array($class, $method)))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000201 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100202 $this->_error_string = sprintf($this->lang->line('migration_missing_'.$method.'_method'), $class);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000203 return FALSE;
204 }
205
206 $migrations[] = $match[1];
207 }
208 else
209 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100210 $this->_error_string = sprintf($this->lang->line('migration_invalid_filename'), $file);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000211 return FALSE;
212 }
213 }
214
Eric Barnesdd81c432011-11-16 11:07:35 -0500215 log_message('debug', 'Current migration: '.$current_version);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000216
217 $version = $i + ($step == 1 ? -1 : 0);
218
219 // If there is nothing to do so quit
220 if ($migrations === array())
221 {
222 return TRUE;
223 }
224
Eric Barnesdd81c432011-11-16 11:07:35 -0500225 log_message('debug', 'Migrating from '.$method.' to version '.$version);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000226
227 // Loop through the migrations
228 foreach ($migrations AS $migration)
229 {
230 // Run the migration class
Eric Barnesdd81c432011-11-16 11:07:35 -0500231 $class = 'Migration_'.ucfirst(strtolower($migration));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000232 call_user_func(array(new $class, $method));
233
234 $current_version += $step;
235 $this->_update_version($current_version);
236 }
237
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100238 log_message('debug', 'Finished migrating to '.$current_version);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000239
240 return $current_version;
241 }
242
243 // --------------------------------------------------------------------
244
245 /**
246 * Set's the schema to the latest migration
247 *
248 * @access public
249 * @return mixed true if already latest, false if failed, int if upgraded
250 */
251 public function latest()
252 {
253 if ( ! $migrations = $this->find_migrations())
254 {
Cloudmanic Labs, LLC3d036e32011-11-11 22:46:21 -0800255 $this->_error_string = $this->lang->line('migration_none_found');
Phil Sturgeon9758d842011-02-07 20:39:00 +0000256 return false;
257 }
258
259 $last_migration = basename(end($migrations));
Eric Barnesdd81c432011-11-16 11:07:35 -0500260
Phil Sturgeon9758d842011-02-07 20:39:00 +0000261 // Calculate the last migration step from existing migration
262 // filenames and procceed to the standard version migration
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100263 return $this->version((int) substr($last_migration, 0, 3));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000264 }
265
266 // --------------------------------------------------------------------
267
268 /**
269 * Set's the schema to the migration version set in config
270 *
271 * @access public
272 * @return mixed true if already current, false if failed, int if upgraded
273 */
274 public function current()
275 {
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100276 return $this->version($this->_migration_version);
277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Error string
283 *
284 * @access public
285 * @return string Error message returned as a string
286 */
287 public function error_string()
288 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100289 return $this->_error_string;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000290 }
291
292 // --------------------------------------------------------------------
293
294 /**
295 * Set's the schema to the latest migration
296 *
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100297 * @access protected
Phil Sturgeon9758d842011-02-07 20:39:00 +0000298 * @return mixed true if already latest, false if failed, int if upgraded
299 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100300 protected function find_migrations()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000301 {
302 // Load all *_*.php files in the migrations path
Eric Barnesdd81c432011-11-16 11:07:35 -0500303 $files = glob($this->_migration_path.'*_*.php');
Phil Sturgeon9758d842011-02-07 20:39:00 +0000304 $file_count = count($files);
Eric Barnesdd81c432011-11-16 11:07:35 -0500305
Phil Sturgeon9758d842011-02-07 20:39:00 +0000306 for ($i = 0; $i < $file_count; $i++)
307 {
308 // Mark wrongly formatted files as false for later filtering
309 $name = basename($files[$i], '.php');
310 if ( ! preg_match('/^\d{3}_(\w+)$/', $name))
311 {
312 $files[$i] = FALSE;
313 }
314 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500315
Phil Sturgeon9758d842011-02-07 20:39:00 +0000316 sort($files);
317
318 return $files;
319 }
320
321 // --------------------------------------------------------------------
322
323 /**
324 * Retrieves current schema version
325 *
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100326 * @access protected
327 * @return integer Current Migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000328 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100329 protected function _get_version()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000330 {
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700331 $row = $this->db->get($this->_migration_table)->row();
Phil Sturgeon9758d842011-02-07 20:39:00 +0000332 return $row ? $row->version : 0;
333 }
334
335 // --------------------------------------------------------------------
336
337 /**
338 * Stores the current schema version
339 *
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100340 * @access protected
341 * @param $migrations integer Migration reached
Phil Sturgeon9758d842011-02-07 20:39:00 +0000342 * @return void Outputs a report of the migration
343 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100344 protected function _update_version($migrations)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000345 {
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700346 return $this->db->update($this->_migration_table, array(
Phil Sturgeon9758d842011-02-07 20:39:00 +0000347 'version' => $migrations
348 ));
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
Phil Sturgeon9758d842011-02-07 20:39:00 +0000354 * Enable the use of CI super-global
355 *
356 * @access public
357 * @param $var
358 * @return mixed
359 */
360 public function __get($var)
361 {
362 return get_instance()->$var;
363 }
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100364}
365
366/* End of file Migration.php */
367/* Location: ./system/libraries/Migration.php */