blob: a2dce1371ef94e813c919ba5c15429f5232d969b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Phil Sturgeon9758d842011-02-07 20:39:00 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Phil Sturgeon9758d842011-02-07 20:39:00 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Eric Barnesdd81c432011-11-16 11:07:35 -05008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Eric Barnesdd81c432011-11-16 11:07:35 -050010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
31 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 3.0.0
Phil Sturgeon9758d842011-02-07 20:39:00 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Phil Sturgeon9758d842011-02-07 20:39:00 +000039
Phil Sturgeon9758d842011-02-07 20:39:00 +000040/**
41 * Migration Class
42 *
43 * All migrations should implement this, forces up() and down() and gives
44 * access to the CI super-global.
45 *
46 * @package CodeIgniter
47 * @subpackage Libraries
48 * @category Libraries
49 * @author Reactor Engineers
50 * @link
51 */
52class CI_Migration {
53
Timothy Warren86611db2012-04-27 10:06:25 -040054 /**
55 * Whether the library is enabled
56 *
57 * @var bool
58 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010059 protected $_migration_enabled = FALSE;
Andrey Andreev39eb8062012-11-13 03:36:40 +020060
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040061 /**
Jonathon Hillb719bfd2012-11-12 09:03:36 -050062 * Migration numbering type
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040063 *
Andrey Andreev39eb8062012-11-13 03:36:40 +020064 * @var bool
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040065 */
Jonathon Hillb719bfd2012-11-12 09:03:36 -050066 protected $_migration_type = 'sequential';
Andrey Andreev56454792012-05-17 14:32:19 +030067
Timothy Warren86611db2012-04-27 10:06:25 -040068 /**
69 * Path to migration classes
70 *
71 * @var string
72 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010073 protected $_migration_path = NULL;
Andrey Andreev56454792012-05-17 14:32:19 +030074
Timothy Warren86611db2012-04-27 10:06:25 -040075 /**
76 * Current migration version
77 *
78 * @var mixed
79 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +010080 protected $_migration_version = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030081
Timothy Warren86611db2012-04-27 10:06:25 -040082 /**
83 * Database table with migration info
84 *
85 * @var string
86 */
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -070087 protected $_migration_table = 'migrations';
Andrey Andreev56454792012-05-17 14:32:19 +030088
Timothy Warren86611db2012-04-27 10:06:25 -040089 /**
90 * Whether to automatically run migrations
91 *
Andrey Andreev39eb8062012-11-13 03:36:40 +020092 * @var bool
Timothy Warren86611db2012-04-27 10:06:25 -040093 */
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -070094 protected $_migration_auto_latest = FALSE;
Andrey Andreev39eb8062012-11-13 03:36:40 +020095
Jonathon Hill34c8b9c2012-10-31 14:02:35 -040096 /**
97 * Migration basename regex
98 *
99 * @var bool
100 */
101 protected $_migration_regex = NULL;
Eric Barnesdd81c432011-11-16 11:07:35 -0500102
Timothy Warren86611db2012-04-27 10:06:25 -0400103 /**
104 * Error message
105 *
106 * @var string
107 */
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100108 protected $_error_string = '';
Phil Sturgeon9758d842011-02-07 20:39:00 +0000109
Timothy Warren86611db2012-04-27 10:06:25 -0400110 /**
111 * Initialize Migration Class
112 *
Andrey Andreev39eb8062012-11-13 03:36:40 +0200113 * @param array $config
Andrey Andreev56454792012-05-17 14:32:19 +0300114 * @return void
Timothy Warren86611db2012-04-27 10:06:25 -0400115 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100116 public function __construct($config = array())
Phil Sturgeon9758d842011-02-07 20:39:00 +0000117 {
Andrey Andreev8151cbb2013-01-30 13:57:56 +0200118 // Only run this constructor on main library load
119 if ( ! in_array(get_class($this), array('CI_Migration', config_item('subclass_prefix').'Migration'), TRUE))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000120 {
121 return;
122 }
123
124 foreach ($config as $key => $val)
125 {
Andrey Andreev56454792012-05-17 14:32:19 +0300126 $this->{'_'.$key} = $val;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000127 }
128
Andrey Andreev90726b82015-01-20 12:39:22 +0200129 log_message('info', 'Migrations Class Initialized');
Phil Sturgeon9758d842011-02-07 20:39:00 +0000130
131 // Are they trying to use migrations while it is disabled?
132 if ($this->_migration_enabled !== TRUE)
133 {
134 show_error('Migrations has been loaded but is disabled or set up incorrectly.');
135 }
136
137 // If not set, set it
Alex Bilbied261b1e2012-06-02 11:12:16 +0100138 $this->_migration_path !== '' OR $this->_migration_path = APPPATH.'migrations/';
Phil Sturgeon9758d842011-02-07 20:39:00 +0000139
140 // Add trailing slash if not set
141 $this->_migration_path = rtrim($this->_migration_path, '/').'/';
142
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100143 // Load migration language
144 $this->lang->load('migration');
145
Phil Sturgeon9758d842011-02-07 20:39:00 +0000146 // They'll probably be using dbforge
147 $this->load->dbforge();
148
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700149 // Make sure the migration table name was set.
Cloudmanic Labs, LLC63b61e32011-09-19 09:35:05 -0700150 if (empty($this->_migration_table))
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700151 {
Eric Barnesdd81c432011-11-16 11:07:35 -0500152 show_error('Migrations configuration file (migration.php) must have "migration_table" set.');
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700153 }
Andrey Andreev39eb8062012-11-13 03:36:40 +0200154
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400155 // Migration basename regex
Jonathon Hill4ddf9442012-11-12 18:30:59 -0500156 $this->_migration_regex = ($this->_migration_type === 'timestamp')
Andrey Andreev838a9d62012-12-03 14:37:47 +0200157 ? '/^\d{14}_(\w+)$/'
158 : '/^\d{3}_(\w+)$/';
Andrey Andreev39eb8062012-11-13 03:36:40 +0200159
Jonathon Hillb719bfd2012-11-12 09:03:36 -0500160 // Make sure a valid migration numbering type was set.
161 if ( ! in_array($this->_migration_type, array('sequential', 'timestamp')))
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400162 {
Jonathon Hillb719bfd2012-11-12 09:03:36 -0500163 show_error('An invalid migration numbering type was specified: '.$this->_migration_type);
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400164 }
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700165
Phil Sturgeon9758d842011-02-07 20:39:00 +0000166 // If the migrations table is missing, make it
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700167 if ( ! $this->db->table_exists($this->_migration_table))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000168 {
169 $this->dbforge->add_field(array(
Jonathon Hill275cf272012-11-12 08:42:28 -0500170 'version' => array('type' => 'BIGINT', 'constraint' => 20),
Phil Sturgeon9758d842011-02-07 20:39:00 +0000171 ));
172
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700173 $this->dbforge->create_table($this->_migration_table, TRUE);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000174
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700175 $this->db->insert($this->_migration_table, array('version' => 0));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000176 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500177
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -0700178 // Do we auto migrate to the latest migration?
Andrey Andreev9b1db792012-03-26 19:45:51 +0300179 if ($this->_migration_auto_latest === TRUE && ! $this->latest())
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -0700180 {
Andrey Andreevdd4702f2011-12-24 19:33:44 +0200181 show_error($this->error_string());
Cloudmanic Labs, LLCd1ba8f72011-09-18 12:23:00 -0700182 }
Phil Sturgeon9758d842011-02-07 20:39:00 +0000183 }
184
185 // --------------------------------------------------------------------
186
187 /**
188 * Migrate to a schema version
189 *
190 * Calls each migration step required to get to the schema version of
191 * choice
192 *
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200193 * @param string $target_version Target schema version
194 * @return mixed TRUE if already latest, FALSE if failed, string if upgraded
Phil Sturgeon9758d842011-02-07 20:39:00 +0000195 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100196 public function version($target_version)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000197 {
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200198 // Note: We use strings, so that timestamp versions work on 32-bit systems
Ahmad Anbare2e5c262014-04-01 01:54:39 +0300199 $current_version = $this->_get_version();
Ahmad Anbarb10a6d62014-04-01 02:00:50 +0300200
Ahmad Anbar59739012014-04-01 01:40:37 +0300201 if ($this->_migration_type === 'sequential')
Ahmad Anbar9b198872014-04-01 01:13:05 +0300202 {
Andrey Andreev46e216c2014-04-01 12:05:02 +0300203 $target_version = sprintf('%03d', $target_version);
Ahmad Anbar9b198872014-04-01 01:13:05 +0300204 }
Ahmad Anbar59739012014-04-01 01:40:37 +0300205 else
206 {
207 $target_version = (string) $target_version;
208 }
Andrey Andreev39eb8062012-11-13 03:36:40 +0200209
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400210 $migrations = $this->find_migrations();
Andrey Andreev39eb8062012-11-13 03:36:40 +0200211
Jonathon Hill02ea66e2012-11-12 17:26:36 -0500212 if ($target_version > 0 && ! isset($migrations[$target_version]))
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400213 {
214 $this->_error_string = sprintf($this->lang->line('migration_not_found'), $target_version);
215 return FALSE;
216 }
Andrey Andreev39eb8062012-11-13 03:36:40 +0200217
Phil Sturgeon9758d842011-02-07 20:39:00 +0000218 if ($target_version > $current_version)
219 {
220 // Moving Up
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400221 $method = 'up';
Phil Sturgeon9758d842011-02-07 20:39:00 +0000222 }
Phil Sturgeon9758d842011-02-07 20:39:00 +0000223 else
224 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400225 // Moving Down, apply in reverse order
226 $method = 'down';
227 krsort($migrations);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000228 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500229
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400230 if (empty($migrations))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000231 {
232 return TRUE;
233 }
Andrey Andreev39eb8062012-11-13 03:36:40 +0200234
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400235 $previous = FALSE;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000236
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400237 // Validate all available migrations, and run the ones within our target range
238 foreach ($migrations as $number => $file)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000239 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400240 // Check for sequence gaps
Jonathon Hill02ea66e2012-11-12 17:26:36 -0500241 if ($this->_migration_type === 'sequential' && $previous !== FALSE && abs($number - $previous) > 1)
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400242 {
243 $this->_error_string = sprintf($this->lang->line('migration_sequence_gap'), $number);
244 return FALSE;
245 }
Andrey Andreev39eb8062012-11-13 03:36:40 +0200246
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200247 include_once($file);
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400248 $class = 'Migration_'.ucfirst(strtolower($this->_get_migration_name(basename($file, '.php'))));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000249
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400250 // Validate the migration file structure
Andrey Andreev49e68de2013-02-21 16:30:55 +0200251 if ( ! class_exists($class, FALSE))
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400252 {
253 $this->_error_string = sprintf($this->lang->line('migration_class_doesnt_exist'), $class);
254 return FALSE;
255 }
Andrey Andreev39eb8062012-11-13 03:36:40 +0200256
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400257 $previous = $number;
258
259 // Run migrations that are inside the target range
260 if (
Jonathon Hill02ea66e2012-11-12 17:26:36 -0500261 ($method === 'up' && $number > $current_version && $number <= $target_version) OR
262 ($method === 'down' && $number <= $current_version && $number > $target_version)
263 )
264 {
Andrey Andreeve1d6c462012-11-28 19:06:20 +0200265 $instance = new $class();
266 if ( ! is_callable(array($instance, $method)))
267 {
268 $this->_error_string = sprintf($this->lang->line('migration_missing_'.$method.'_method'), $class);
269 return FALSE;
270 }
271
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400272 log_message('debug', 'Migrating '.$method.' from version '.$current_version.' to version '.$number);
Andrey Andreeve1d6c462012-11-28 19:06:20 +0200273 call_user_func(array($instance, $method));
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400274 $current_version = $number;
275 $this->_update_version($current_version);
276 }
277 }
Andrey Andreev39eb8062012-11-13 03:36:40 +0200278
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400279 // This is necessary when moving down, since the the last migration applied
280 // will be the down() method for the next migration up from the target
281 if ($current_version <> $target_version)
282 {
283 $current_version = $target_version;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000284 $this->_update_version($current_version);
285 }
286
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100287 log_message('debug', 'Finished migrating to '.$current_version);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000288
289 return $current_version;
290 }
291
292 // --------------------------------------------------------------------
293
294 /**
Jacob Tabak35f6a542014-02-19 15:38:26 -0600295 * Sets the schema to the latest migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000296 *
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200297 * @return mixed TRUE if already latest, FALSE if failed, string if upgraded
Phil Sturgeon9758d842011-02-07 20:39:00 +0000298 */
299 public function latest()
300 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400301 $migrations = $this->find_migrations();
Andrey Andreev39eb8062012-11-13 03:36:40 +0200302
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400303 if (empty($migrations))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000304 {
Cloudmanic Labs, LLC3d036e32011-11-11 22:46:21 -0800305 $this->_error_string = $this->lang->line('migration_none_found');
Alex Bilbieafee2262012-07-15 18:59:01 +0100306 return FALSE;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000307 }
308
309 $last_migration = basename(end($migrations));
Andrey Andreev39eb8062012-11-13 03:36:40 +0200310
Phil Sturgeon9758d842011-02-07 20:39:00 +0000311 // Calculate the last migration step from existing migration
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200312 // filenames and proceed to the standard version migration
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400313 return $this->version($this->_get_migration_number($last_migration));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000314 }
315
316 // --------------------------------------------------------------------
317
318 /**
Jacob Tabak35f6a542014-02-19 15:38:26 -0600319 * Sets the schema to the migration version set in config
Phil Sturgeon9758d842011-02-07 20:39:00 +0000320 *
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200321 * @return mixed TRUE if already current, FALSE if failed, string if upgraded
Phil Sturgeon9758d842011-02-07 20:39:00 +0000322 */
323 public function current()
324 {
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100325 return $this->version($this->_migration_version);
326 }
327
328 // --------------------------------------------------------------------
329
330 /**
331 * Error string
332 *
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100333 * @return string Error message returned as a string
334 */
335 public function error_string()
336 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100337 return $this->_error_string;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000338 }
339
340 // --------------------------------------------------------------------
341
342 /**
Dumk05db48272012-07-05 02:58:10 +0300343 * Retrieves list of available migration scripts
Phil Sturgeon9758d842011-02-07 20:39:00 +0000344 *
Dumk05db48272012-07-05 02:58:10 +0300345 * @return array list of migration file paths sorted by version
Phil Sturgeon9758d842011-02-07 20:39:00 +0000346 */
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400347 public function find_migrations()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000348 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400349 $migrations = array();
Andrey Andreev39eb8062012-11-13 03:36:40 +0200350
Phil Sturgeon9758d842011-02-07 20:39:00 +0000351 // Load all *_*.php files in the migrations path
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400352 foreach (glob($this->_migration_path.'*_*.php') as $file)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000353 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400354 $name = basename($file, '.php');
Andrey Andreev39eb8062012-11-13 03:36:40 +0200355
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400356 // Filter out non-migration files
357 if (preg_match($this->_migration_regex, $name))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000358 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400359 $number = $this->_get_migration_number($name);
Andrey Andreev39eb8062012-11-13 03:36:40 +0200360
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400361 // There cannot be duplicate migration numbers
362 if (isset($migrations[$number]))
363 {
364 $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $number);
365 show_error($this->_error_string);
366 }
Andrey Andreev39eb8062012-11-13 03:36:40 +0200367
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400368 $migrations[$number] = $file;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000369 }
370 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500371
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400372 ksort($migrations);
373 return $migrations;
374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * Extracts the migration number from a filename
380 *
Andrey Andreev39eb8062012-11-13 03:36:40 +0200381 * @param string $migration
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200382 * @return string Numeric portion of a migration filename
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400383 */
384 protected function _get_migration_number($migration)
385 {
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200386 return sscanf($migration, '%[0-9]+', $number)
387 ? $number : '0';
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * Extracts the migration class name from a filename
394 *
Andrey Andreev39eb8062012-11-13 03:36:40 +0200395 * @param string $migration
396 * @return string text portion of a migration filename
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400397 */
398 protected function _get_migration_name($migration)
399 {
400 $parts = explode('_', $migration);
401 array_shift($parts);
402 return implode('_', $parts);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * Retrieves current schema version
409 *
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200410 * @return string Current migration version
Phil Sturgeon9758d842011-02-07 20:39:00 +0000411 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100412 protected function _get_version()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000413 {
Phil Sturgeond268eda2011-12-31 16:20:11 +0000414 $row = $this->db->select('version')->get($this->_migration_table)->row();
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200415 return $row ? $row->version : '0';
Phil Sturgeon9758d842011-02-07 20:39:00 +0000416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Stores the current schema version
422 *
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200423 * @param string $migration Migration reached
Andrey Andreev9b1db792012-03-26 19:45:51 +0300424 * @return void Outputs a report of the migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000425 */
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400426 protected function _update_version($migration)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000427 {
Cloudmanic Labs, LLC539dcb02011-09-18 12:08:56 -0700428 return $this->db->update($this->_migration_table, array(
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400429 'version' => $migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000430 ));
431 }
432
433 // --------------------------------------------------------------------
434
435 /**
Phil Sturgeon9758d842011-02-07 20:39:00 +0000436 * Enable the use of CI super-global
437 *
Andrey Andreev39eb8062012-11-13 03:36:40 +0200438 * @param string $var
Phil Sturgeon9758d842011-02-07 20:39:00 +0000439 * @return mixed
440 */
441 public function __get($var)
442 {
443 return get_instance()->$var;
444 }
Andrey Andreev56454792012-05-17 14:32:19 +0300445
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100446}
447
448/* End of file Migration.php */
Andrey Andreev9b1db792012-03-26 19:45:51 +0300449/* Location: ./system/libraries/Migration.php */