blob: af9e53030b51555404f54686bbad16b0a84ef1c9 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Phil Sturgeon9758d842011-02-07 20:39:00 +000028
Phil Sturgeon9758d842011-02-07 20:39:00 +000029/**
30 * Migration Class
31 *
32 * All migrations should implement this, forces up() and down() and gives
33 * access to the CI super-global.
34 *
35 * @package CodeIgniter
36 * @subpackage Libraries
37 * @category Libraries
38 * @author Reactor Engineers
39 * @link
40 */
41class CI_Migration {
42
Timothy Warren86611db2012-04-27 10:06:25 -040043 /**
44 * Whether the library is enabled
45 *
46 * @var bool
47 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010048 protected $_migration_enabled = FALSE;
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040049
50 /**
Jonathon Hillb719bfd2012-11-12 09:03:36 -050051 * Migration numbering type
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040052 *
53 * @var bool
54 */
Jonathon Hillb719bfd2012-11-12 09:03:36 -050055 protected $_migration_type = 'sequential';
Andrey Andreev56454792012-05-17 14:32:19 +030056
Timothy Warren86611db2012-04-27 10:06:25 -040057 /**
58 * Path to migration classes
59 *
60 * @var string
61 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010062 protected $_migration_path = NULL;
Andrey Andreev56454792012-05-17 14:32:19 +030063
Timothy Warren86611db2012-04-27 10:06:25 -040064 /**
65 * Current migration version
66 *
67 * @var mixed
68 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010069 protected $_migration_version = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030070
Timothy Warren86611db2012-04-27 10:06:25 -040071 /**
72 * Database table with migration info
73 *
74 * @var string
75 */
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -070076 protected $_migration_table = 'migrations';
Andrey Andreev56454792012-05-17 14:32:19 +030077
Timothy Warren86611db2012-04-27 10:06:25 -040078 /**
79 * Whether to automatically run migrations
80 *
81 * @var bool
82 */
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -070083 protected $_migration_auto_latest = FALSE;
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040084
85 /**
86 * Migration basename regex
87 *
88 * @var bool
89 */
90 protected $_migration_regex = NULL;
Eric Barnesdd81c432011-11-16 11:07:35 -050091
Timothy Warren86611db2012-04-27 10:06:25 -040092 /**
93 * Error message
94 *
95 * @var string
96 */
Phil Sturgeoncb06c652011-05-04 10:50:25 +010097 protected $_error_string = '';
Phil Sturgeon9758d842011-02-07 20:39:00 +000098
Timothy Warren86611db2012-04-27 10:06:25 -040099 /**
100 * Initialize Migration Class
101 *
102 * @param array
Andrey Andreev56454792012-05-17 14:32:19 +0300103 * @return void
Timothy Warren86611db2012-04-27 10:06:25 -0400104 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100105 public function __construct($config = array())
Phil Sturgeon9758d842011-02-07 20:39:00 +0000106 {
107 # Only run this constructor on main library load
108 if (get_parent_class($this) !== FALSE)
109 {
110 return;
111 }
112
113 foreach ($config as $key => $val)
114 {
Andrey Andreev56454792012-05-17 14:32:19 +0300115 $this->{'_'.$key} = $val;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000116 }
117
118 log_message('debug', 'Migrations class initialized');
119
120 // Are they trying to use migrations while it is disabled?
121 if ($this->_migration_enabled !== TRUE)
122 {
123 show_error('Migrations has been loaded but is disabled or set up incorrectly.');
124 }
125
126 // If not set, set it
Alex Bilbied261b1e2012-06-02 11:12:16 +0100127 $this->_migration_path !== '' OR $this->_migration_path = APPPATH.'migrations/';
Phil Sturgeon9758d842011-02-07 20:39:00 +0000128
129 // Add trailing slash if not set
130 $this->_migration_path = rtrim($this->_migration_path, '/').'/';
131
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100132 // Load migration language
133 $this->lang->load('migration');
134
Phil Sturgeon9758d842011-02-07 20:39:00 +0000135 // They'll probably be using dbforge
136 $this->load->dbforge();
137
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700138 // Make sure the migration table name was set.
Cloudmanic Labs, LLC63b61e32011-09-19 09:35:05 -0700139 if (empty($this->_migration_table))
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700140 {
Eric Barnesdd81c432011-11-16 11:07:35 -0500141 show_error('Migrations configuration file (migration.php) must have "migration_table" set.');
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700142 }
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400143
144 // Migration basename regex
Jonathon Hillb719bfd2012-11-12 09:03:36 -0500145 $this->_migration_regex = $this->_migration_type === 'timestamp' ? '/^\d{14}_(\w+)$/' : '/^\d{3}_(\w+)$/';
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400146
Jonathon Hillb719bfd2012-11-12 09:03:36 -0500147 // Make sure a valid migration numbering type was set.
148 if ( ! in_array($this->_migration_type, array('sequential', 'timestamp')))
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400149 {
Jonathon Hillb719bfd2012-11-12 09:03:36 -0500150 show_error('An invalid migration numbering type was specified: '.$this->_migration_type);
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400151 }
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700152
Phil Sturgeon9758d842011-02-07 20:39:00 +0000153 // If the migrations table is missing, make it
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700154 if ( ! $this->db->table_exists($this->_migration_table))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000155 {
156 $this->dbforge->add_field(array(
Jonathon Hill275cf272012-11-12 08:42:28 -0500157 'version' => array('type' => 'BIGINT', 'constraint' => 20),
Phil Sturgeon9758d842011-02-07 20:39:00 +0000158 ));
159
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700160 $this->dbforge->create_table($this->_migration_table, TRUE);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000161
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700162 $this->db->insert($this->_migration_table, array('version' => 0));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000163 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500164
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -0700165 // Do we auto migrate to the latest migration?
Andrey Andreev9b1db792012-03-26 19:45:51 +0300166 if ($this->_migration_auto_latest === TRUE && ! $this->latest())
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -0700167 {
Andrey Andreevdd4702f2011-12-24 19:33:44 +0200168 show_error($this->error_string());
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -0700169 }
Phil Sturgeon9758d842011-02-07 20:39:00 +0000170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Migrate to a schema version
176 *
177 * Calls each migration step required to get to the schema version of
178 * choice
179 *
Andrey Andreev9b1db792012-03-26 19:45:51 +0300180 * @param int Target schema version
Phil Sturgeon9758d842011-02-07 20:39:00 +0000181 * @return mixed TRUE if already latest, FALSE if failed, int if upgraded
182 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100183 public function version($target_version)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000184 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400185 $current_version = (int) $this->_get_version();
186 $target_version = (int) $target_version;
187
188 $migrations = $this->find_migrations();
189
190 if ($target_version > 0 AND ! isset($migrations[$target_version]))
191 {
192 $this->_error_string = sprintf($this->lang->line('migration_not_found'), $target_version);
193 return FALSE;
194 }
195
Phil Sturgeon9758d842011-02-07 20:39:00 +0000196 if ($target_version > $current_version)
197 {
198 // Moving Up
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400199 $method = 'up';
Phil Sturgeon9758d842011-02-07 20:39:00 +0000200 }
Phil Sturgeon9758d842011-02-07 20:39:00 +0000201 else
202 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400203 // Moving Down, apply in reverse order
204 $method = 'down';
205 krsort($migrations);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000206 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500207
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400208 if (empty($migrations))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000209 {
210 return TRUE;
211 }
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400212
213 $previous = FALSE;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000214
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400215 // Validate all available migrations, and run the ones within our target range
216 foreach ($migrations as $number => $file)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000217 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400218 // Check for sequence gaps
Jonathon Hillb719bfd2012-11-12 09:03:36 -0500219 if ($this->_migration_type === 'sequential' AND $previous !== FALSE AND abs($number - $previous) > 1)
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400220 {
221 $this->_error_string = sprintf($this->lang->line('migration_sequence_gap'), $number);
222 return FALSE;
223 }
224
225 include $file;
226 $class = 'Migration_'.ucfirst(strtolower($this->_get_migration_name(basename($file, '.php'))));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000227
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400228 // Validate the migration file structure
229 if ( ! class_exists($class))
230 {
231 $this->_error_string = sprintf($this->lang->line('migration_class_doesnt_exist'), $class);
232 return FALSE;
233 }
234 elseif ( ! is_callable(array($class, $method)))
235 {
236 $this->_error_string = sprintf($this->lang->line('migration_missing_'.$method.'_method'), $class);
237 return FALSE;
238 }
239
240 $previous = $number;
241
242 // Run migrations that are inside the target range
243 if (
244 ($method === 'up' AND $number > $current_version AND $number <= $target_version) OR
245 ($method === 'down' AND $number <= $current_version AND $number > $target_version)
246 ) {
247 log_message('debug', 'Migrating '.$method.' from version '.$current_version.' to version '.$number);
248 call_user_func(array(new $class, $method));
249 $current_version = $number;
250 $this->_update_version($current_version);
251 }
252 }
253
254 // This is necessary when moving down, since the the last migration applied
255 // will be the down() method for the next migration up from the target
256 if ($current_version <> $target_version)
257 {
258 $current_version = $target_version;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000259 $this->_update_version($current_version);
260 }
261
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100262 log_message('debug', 'Finished migrating to '.$current_version);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000263
264 return $current_version;
265 }
266
267 // --------------------------------------------------------------------
268
269 /**
270 * Set's the schema to the latest migration
271 *
Phil Sturgeon9758d842011-02-07 20:39:00 +0000272 * @return mixed true if already latest, false if failed, int if upgraded
273 */
274 public function latest()
275 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400276 $migrations = $this->find_migrations();
277
278 if (empty($migrations))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000279 {
Cloudmanic Labs, LLC3d036e32011-11-11 22:46:21 -0800280 $this->_error_string = $this->lang->line('migration_none_found');
Alex Bilbieafee2262012-07-15 18:59:01 +0100281 return FALSE;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000282 }
283
284 $last_migration = basename(end($migrations));
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400285
Phil Sturgeon9758d842011-02-07 20:39:00 +0000286 // Calculate the last migration step from existing migration
287 // filenames and procceed to the standard version migration
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400288 return $this->version($this->_get_migration_number($last_migration));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000289 }
290
291 // --------------------------------------------------------------------
292
293 /**
294 * Set's the schema to the migration version set in config
295 *
Phil Sturgeon9758d842011-02-07 20:39:00 +0000296 * @return mixed true if already current, false if failed, int if upgraded
297 */
298 public function current()
299 {
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100300 return $this->version($this->_migration_version);
301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Error string
307 *
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100308 * @return string Error message returned as a string
309 */
310 public function error_string()
311 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100312 return $this->_error_string;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000313 }
314
315 // --------------------------------------------------------------------
316
317 /**
Dumk05db48272012-07-05 02:58:10 +0300318 * Retrieves list of available migration scripts
Phil Sturgeon9758d842011-02-07 20:39:00 +0000319 *
Dumk05db48272012-07-05 02:58:10 +0300320 * @return array list of migration file paths sorted by version
Phil Sturgeon9758d842011-02-07 20:39:00 +0000321 */
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400322 public function find_migrations()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000323 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400324 $migrations = array();
325
Phil Sturgeon9758d842011-02-07 20:39:00 +0000326 // Load all *_*.php files in the migrations path
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400327 foreach (glob($this->_migration_path.'*_*.php') as $file)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000328 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400329 $name = basename($file, '.php');
330
331 // Filter out non-migration files
332 if (preg_match($this->_migration_regex, $name))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000333 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400334 $number = $this->_get_migration_number($name);
335
336 // There cannot be duplicate migration numbers
337 if (isset($migrations[$number]))
338 {
339 $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $number);
340 show_error($this->_error_string);
341 }
342
343 $migrations[$number] = $file;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000344 }
345 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500346
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400347 ksort($migrations);
348 return $migrations;
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
354 * Extracts the migration number from a filename
355 *
356 * @return int Numeric portion of a migration filename
357 */
358 protected function _get_migration_number($migration)
359 {
360 $parts = explode('_', $migration);
361 return (int) $parts[0];
362 }
363
364 // --------------------------------------------------------------------
365
366 /**
367 * Extracts the migration class name from a filename
368 *
369 * @return string text portion of a migration filename
370 */
371 protected function _get_migration_name($migration)
372 {
373 $parts = explode('_', $migration);
374 array_shift($parts);
375 return implode('_', $parts);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000376 }
377
378 // --------------------------------------------------------------------
379
380 /**
381 * Retrieves current schema version
382 *
Andrey Andreev9b1db792012-03-26 19:45:51 +0300383 * @return int Current Migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000384 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100385 protected function _get_version()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000386 {
Phil Sturgeond268eda2011-12-31 16:20:11 +0000387 $row = $this->db->select('version')->get($this->_migration_table)->row();
Phil Sturgeon9758d842011-02-07 20:39:00 +0000388 return $row ? $row->version : 0;
389 }
390
391 // --------------------------------------------------------------------
392
393 /**
394 * Stores the current schema version
395 *
Andrey Andreev9b1db792012-03-26 19:45:51 +0300396 * @param int Migration reached
397 * @return void Outputs a report of the migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000398 */
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400399 protected function _update_version($migration)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000400 {
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700401 return $this->db->update($this->_migration_table, array(
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400402 'version' => $migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000403 ));
404 }
405
406 // --------------------------------------------------------------------
407
408 /**
Phil Sturgeon9758d842011-02-07 20:39:00 +0000409 * Enable the use of CI super-global
410 *
Andrey Andreev9b1db792012-03-26 19:45:51 +0300411 * @param $var
Phil Sturgeon9758d842011-02-07 20:39:00 +0000412 * @return mixed
413 */
414 public function __get($var)
415 {
416 return get_instance()->$var;
417 }
Andrey Andreev56454792012-05-17 14:32:19 +0300418
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100419}
420
421/* End of file Migration.php */
Andrey Andreev9b1db792012-03-26 19:45:51 +0300422/* Location: ./system/libraries/Migration.php */