blob: ce4683fc1faf7dc034ddeddb58af043eb19f7236 [file] [log] [blame]
Andrey Andreevdd4702f2011-12-24 19:33:44 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Phil Sturgeon9758d842011-02-07 20:39:00 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Phil Sturgeon9758d842011-02-07 20:39:00 +00006 *
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2006 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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
Phil Sturgeon9758d842011-02-07 20:39:00 +000028/**
29 * Migration Class
30 *
31 * All migrations should implement this, forces up() and down() and gives
32 * access to the CI super-global.
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category Libraries
37 * @author Reactor Engineers
38 * @link
39 */
40class CI_Migration {
41
Timothy Warren86611db2012-04-27 10:06:25 -040042 /**
43 * Whether the library is enabled
44 *
45 * @var bool
46 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010047 protected $_migration_enabled = FALSE;
Timothy Warren86611db2012-04-27 10:06:25 -040048
49 /**
50 * Path to migration classes
51 *
52 * @var string
53 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010054 protected $_migration_path = NULL;
Timothy Warren86611db2012-04-27 10:06:25 -040055
56 /**
57 * Current migration version
58 *
59 * @var mixed
60 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010061 protected $_migration_version = 0;
Timothy Warren86611db2012-04-27 10:06:25 -040062
63 /**
64 * Database table with migration info
65 *
66 * @var string
67 */
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -070068 protected $_migration_table = 'migrations';
Timothy Warren86611db2012-04-27 10:06:25 -040069
70 /**
71 * Whether to automatically run migrations
72 *
73 * @var bool
74 */
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -070075 protected $_migration_auto_latest = FALSE;
Eric Barnesdd81c432011-11-16 11:07:35 -050076
Timothy Warren86611db2012-04-27 10:06:25 -040077 /**
78 * Error message
79 *
80 * @var string
81 */
Phil Sturgeoncb06c652011-05-04 10:50:25 +010082 protected $_error_string = '';
Phil Sturgeon9758d842011-02-07 20:39:00 +000083
Timothy Warren86611db2012-04-27 10:06:25 -040084 /**
85 * Initialize Migration Class
86 *
87 * @param array
88 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010089 public function __construct($config = array())
Phil Sturgeon9758d842011-02-07 20:39:00 +000090 {
91 # Only run this constructor on main library load
92 if (get_parent_class($this) !== FALSE)
93 {
94 return;
95 }
96
97 foreach ($config as $key => $val)
98 {
99 $this->{'_' . $key} = $val;
100 }
101
102 log_message('debug', 'Migrations class initialized');
103
104 // Are they trying to use migrations while it is disabled?
105 if ($this->_migration_enabled !== TRUE)
106 {
107 show_error('Migrations has been loaded but is disabled or set up incorrectly.');
108 }
109
110 // If not set, set it
Andrey Andreev9b1db792012-03-26 19:45:51 +0300111 $this->_migration_path != '' OR $this->_migration_path = APPPATH.'migrations/';
Phil Sturgeon9758d842011-02-07 20:39:00 +0000112
113 // Add trailing slash if not set
114 $this->_migration_path = rtrim($this->_migration_path, '/').'/';
115
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100116 // Load migration language
117 $this->lang->load('migration');
118
Phil Sturgeon9758d842011-02-07 20:39:00 +0000119 // They'll probably be using dbforge
120 $this->load->dbforge();
121
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700122 // Make sure the migration table name was set.
Cloudmanic Labs, LLC63b61e32011-09-19 09:35:05 -0700123 if (empty($this->_migration_table))
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700124 {
Eric Barnesdd81c432011-11-16 11:07:35 -0500125 show_error('Migrations configuration file (migration.php) must have "migration_table" set.');
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700126 }
127
Phil Sturgeon9758d842011-02-07 20:39:00 +0000128 // If the migrations table is missing, make it
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700129 if ( ! $this->db->table_exists($this->_migration_table))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000130 {
131 $this->dbforge->add_field(array(
132 'version' => array('type' => 'INT', 'constraint' => 3),
133 ));
134
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700135 $this->dbforge->create_table($this->_migration_table, TRUE);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000136
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700137 $this->db->insert($this->_migration_table, array('version' => 0));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000138 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500139
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -0700140 // Do we auto migrate to the latest migration?
Andrey Andreev9b1db792012-03-26 19:45:51 +0300141 if ($this->_migration_auto_latest === TRUE && ! $this->latest())
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -0700142 {
Andrey Andreevdd4702f2011-12-24 19:33:44 +0200143 show_error($this->error_string());
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -0700144 }
Phil Sturgeon9758d842011-02-07 20:39:00 +0000145 }
146
147 // --------------------------------------------------------------------
148
149 /**
150 * Migrate to a schema version
151 *
152 * Calls each migration step required to get to the schema version of
153 * choice
154 *
Andrey Andreev9b1db792012-03-26 19:45:51 +0300155 * @param int Target schema version
Phil Sturgeon9758d842011-02-07 20:39:00 +0000156 * @return mixed TRUE if already latest, FALSE if failed, int if upgraded
157 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100158 public function version($target_version)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000159 {
160 $start = $current_version = $this->_get_version();
161 $stop = $target_version;
162
163 if ($target_version > $current_version)
164 {
165 // Moving Up
166 ++$start;
167 ++$stop;
168 $step = 1;
169 }
Phil Sturgeon9758d842011-02-07 20:39:00 +0000170 else
171 {
172 // Moving Down
173 $step = -1;
174 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500175
Phil Sturgeon9758d842011-02-07 20:39:00 +0000176 $method = $step === 1 ? 'up' : 'down';
177 $migrations = array();
178
179 // We now prepare to actually DO the migrations
180 // But first let's make sure that everything is the way it should be
181 for ($i = $start; $i != $stop; $i += $step)
182 {
Eric Barnesdd81c432011-11-16 11:07:35 -0500183 $f = glob(sprintf($this->_migration_path.'%03d_*.php', $i));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000184
185 // Only one migration per step is permitted
186 if (count($f) > 1)
187 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100188 $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $i);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000189 return FALSE;
190 }
191
192 // Migration step not found
Andrey Andreevdd4702f2011-12-24 19:33:44 +0200193 if (count($f) === 0)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000194 {
195 // If trying to migrate up to a version greater than the last
196 // existing one, migrate to the last one.
Andrey Andreevdd4702f2011-12-24 19:33:44 +0200197 if ($step === 1)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000198 {
199 break;
200 }
201
202 // If trying to migrate down but we're missing a step,
203 // something must definitely be wrong.
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100204 $this->_error_string = sprintf($this->lang->line('migration_not_found'), $i);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000205 return FALSE;
206 }
207
208 $file = basename($f[0]);
209 $name = basename($f[0], '.php');
210
211 // Filename validations
212 if (preg_match('/^\d{3}_(\w+)$/', $name, $match))
213 {
214 $match[1] = strtolower($match[1]);
215
216 // Cannot repeat a migration at different steps
217 if (in_array($match[1], $migrations))
218 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100219 $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $match[1]);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000220 return FALSE;
221 }
222
223 include $f[0];
Eric Barnesdd81c432011-11-16 11:07:35 -0500224 $class = 'Migration_'.ucfirst($match[1]);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000225
226 if ( ! class_exists($class))
227 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100228 $this->_error_string = sprintf($this->lang->line('migration_class_doesnt_exist'), $class);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000229 return FALSE;
230 }
231
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100232 if ( ! is_callable(array($class, $method)))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000233 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100234 $this->_error_string = sprintf($this->lang->line('migration_missing_'.$method.'_method'), $class);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000235 return FALSE;
236 }
237
238 $migrations[] = $match[1];
239 }
240 else
241 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100242 $this->_error_string = sprintf($this->lang->line('migration_invalid_filename'), $file);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000243 return FALSE;
244 }
245 }
246
Eric Barnesdd81c432011-11-16 11:07:35 -0500247 log_message('debug', 'Current migration: '.$current_version);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000248
Andrey Andreevdd4702f2011-12-24 19:33:44 +0200249 $version = $i + ($step === 1 ? -1 : 0);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000250
251 // If there is nothing to do so quit
252 if ($migrations === array())
253 {
254 return TRUE;
255 }
256
Eric Barnesdd81c432011-11-16 11:07:35 -0500257 log_message('debug', 'Migrating from '.$method.' to version '.$version);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000258
259 // Loop through the migrations
260 foreach ($migrations AS $migration)
261 {
262 // Run the migration class
Eric Barnesdd81c432011-11-16 11:07:35 -0500263 $class = 'Migration_'.ucfirst(strtolower($migration));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000264 call_user_func(array(new $class, $method));
265
266 $current_version += $step;
267 $this->_update_version($current_version);
268 }
269
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100270 log_message('debug', 'Finished migrating to '.$current_version);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000271
272 return $current_version;
273 }
274
275 // --------------------------------------------------------------------
276
277 /**
278 * Set's the schema to the latest migration
279 *
Phil Sturgeon9758d842011-02-07 20:39:00 +0000280 * @return mixed true if already latest, false if failed, int if upgraded
281 */
282 public function latest()
283 {
284 if ( ! $migrations = $this->find_migrations())
285 {
Cloudmanic Labs, LLC3d036e32011-11-11 22:46:21 -0800286 $this->_error_string = $this->lang->line('migration_none_found');
Phil Sturgeon9758d842011-02-07 20:39:00 +0000287 return false;
288 }
289
290 $last_migration = basename(end($migrations));
Eric Barnesdd81c432011-11-16 11:07:35 -0500291
Phil Sturgeon9758d842011-02-07 20:39:00 +0000292 // Calculate the last migration step from existing migration
293 // filenames and procceed to the standard version migration
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100294 return $this->version((int) substr($last_migration, 0, 3));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000295 }
296
297 // --------------------------------------------------------------------
298
299 /**
300 * Set's the schema to the migration version set in config
301 *
Phil Sturgeon9758d842011-02-07 20:39:00 +0000302 * @return mixed true if already current, false if failed, int if upgraded
303 */
304 public function current()
305 {
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100306 return $this->version($this->_migration_version);
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Error string
313 *
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100314 * @return string Error message returned as a string
315 */
316 public function error_string()
317 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100318 return $this->_error_string;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000319 }
320
321 // --------------------------------------------------------------------
322
323 /**
324 * Set's the schema to the latest migration
325 *
Phil Sturgeon9758d842011-02-07 20:39:00 +0000326 * @return mixed true if already latest, false if failed, int if upgraded
327 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100328 protected function find_migrations()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000329 {
330 // Load all *_*.php files in the migrations path
Eric Barnesdd81c432011-11-16 11:07:35 -0500331 $files = glob($this->_migration_path.'*_*.php');
Eric Barnesdd81c432011-11-16 11:07:35 -0500332
Andrey Andreevdd4702f2011-12-24 19:33:44 +0200333 for ($i = 0, $c = count($files); $i < $c; $i++)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000334 {
335 // Mark wrongly formatted files as false for later filtering
Andrey Andreevdd4702f2011-12-24 19:33:44 +0200336 if ( ! preg_match('/^\d{3}_(\w+)$/', basename($files[$i], '.php')))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000337 {
338 $files[$i] = FALSE;
339 }
340 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500341
Phil Sturgeon9758d842011-02-07 20:39:00 +0000342 sort($files);
343
344 return $files;
345 }
346
347 // --------------------------------------------------------------------
348
349 /**
350 * Retrieves current schema version
351 *
Andrey Andreev9b1db792012-03-26 19:45:51 +0300352 * @return int Current Migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000353 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100354 protected function _get_version()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000355 {
Phil Sturgeond268eda2011-12-31 16:20:11 +0000356 $row = $this->db->select('version')->get($this->_migration_table)->row();
Phil Sturgeon9758d842011-02-07 20:39:00 +0000357 return $row ? $row->version : 0;
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * Stores the current schema version
364 *
Andrey Andreev9b1db792012-03-26 19:45:51 +0300365 * @param int Migration reached
366 * @return void Outputs a report of the migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000367 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100368 protected function _update_version($migrations)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000369 {
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700370 return $this->db->update($this->_migration_table, array(
Phil Sturgeon9758d842011-02-07 20:39:00 +0000371 'version' => $migrations
372 ));
373 }
374
375 // --------------------------------------------------------------------
376
377 /**
Phil Sturgeon9758d842011-02-07 20:39:00 +0000378 * Enable the use of CI super-global
379 *
Andrey Andreev9b1db792012-03-26 19:45:51 +0300380 * @param $var
Phil Sturgeon9758d842011-02-07 20:39:00 +0000381 * @return mixed
382 */
383 public function __get($var)
384 {
385 return get_instance()->$var;
386 }
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100387}
388
389/* End of file Migration.php */
Andrey Andreev9b1db792012-03-26 19:45:51 +0300390/* Location: ./system/libraries/Migration.php */