blob: 316c94ae3a2856087418add32ab95d034158a53b [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 Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @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 *
Andrey Andreevf8490b92016-03-16 16:22:14 +020099 * @var string
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400100 */
Andrey Andreevf8490b92016-03-16 16:22:14 +0200101 protected $_migration_regex;
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
ftwbzhaob8978ee2015-05-20 16:10:35 +0800194 * @return mixed TRUE if no migrations are found, current version string on success, FALSE on failure
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 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400220 $method = 'up';
Phil Sturgeon9758d842011-02-07 20:39:00 +0000221 }
Andrey Andreev3df07722016-03-16 21:15:52 +0200222 elseif ($target_version < $current_version)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000223 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400224 $method = 'down';
Andrey Andreevf8490b92016-03-16 16:22:14 +0200225 // We need this so that migrations are applied in reverse order
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400226 krsort($migrations);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000227 }
Andrey Andreev3df07722016-03-16 21:15:52 +0200228 else
229 {
230 // Well, there's nothing to migrate then ...
231 return TRUE;
232 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500233
Andrey Andreevf8490b92016-03-16 16:22:14 +0200234 // Validate all available migrations within our target range.
235 //
236 // Unfortunately, we'll have to use another loop to run them
237 // in order to avoid leaving the procedure in a broken state.
238 //
239 // See https://github.com/bcit-ci/CodeIgniter/issues/4539
240 $pending = array();
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400241 foreach ($migrations as $number => $file)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000242 {
Andrey Andreevf8490b92016-03-16 16:22:14 +0200243 // Ignore versions out of our range.
244 //
245 // Because we've previously sorted the $migrations array depending on the direction,
246 // we can safely break the loop once we reach $target_version ...
247 if ($method === 'up')
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400248 {
Andrey Andreevf8490b92016-03-16 16:22:14 +0200249 if ($number <= $current_version)
250 {
251 continue;
252 }
253 elseif ($number > $target_version)
254 {
255 break;
256 }
257 }
258 else
259 {
260 if ($number > $current_version)
261 {
262 continue;
263 }
264 elseif ($number <= $target_version)
265 {
266 break;
267 }
268 }
269
270 // Check for sequence gaps
271 if ($this->_migration_type === 'sequential')
272 {
273 if (isset($previous) && abs($number - $previous) > 1)
274 {
275 $this->_error_string = sprintf($this->lang->line('migration_sequence_gap'), $number);
276 return FALSE;
277 }
278
279 $previous = $number;
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400280 }
Andrey Andreev39eb8062012-11-13 03:36:40 +0200281
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200282 include_once($file);
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400283 $class = 'Migration_'.ucfirst(strtolower($this->_get_migration_name(basename($file, '.php'))));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000284
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400285 // Validate the migration file structure
Andrey Andreev49e68de2013-02-21 16:30:55 +0200286 if ( ! class_exists($class, FALSE))
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400287 {
288 $this->_error_string = sprintf($this->lang->line('migration_class_doesnt_exist'), $class);
289 return FALSE;
290 }
Andrey Andreevf8490b92016-03-16 16:22:14 +0200291 // method_exists() returns true for non-public methods,
292 // while is_callable() can't be used without instantiating.
293 // Only get_class_methods() satisfies both conditions.
294 elseif ( ! in_array($method, array_map('strtolower', get_class_methods($class))))
Jonathon Hill02ea66e2012-11-12 17:26:36 -0500295 {
Andrey Andreevf8490b92016-03-16 16:22:14 +0200296 $this->_error_string = sprintf($this->lang->line('migration_missing_'.$method.'_method'), $class);
297 return FALSE;
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400298 }
Andrey Andreevf8490b92016-03-16 16:22:14 +0200299
300 $pending[$number] = array($class, $method);
301 }
302
303 // Now just run the necessary migrations
304 foreach ($pending as $number => $migration)
305 {
306 log_message('debug', 'Migrating '.$method.' from version '.$current_version.' to version '.$number);
307
308 $migration[0] = new $migration[0];
309 call_user_func($migration);
310 $current_version = $number;
311 $this->_update_version($current_version);
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400312 }
Andrey Andreev39eb8062012-11-13 03:36:40 +0200313
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400314 // This is necessary when moving down, since the the last migration applied
315 // will be the down() method for the next migration up from the target
316 if ($current_version <> $target_version)
317 {
318 $current_version = $target_version;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000319 $this->_update_version($current_version);
320 }
321
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100322 log_message('debug', 'Finished migrating to '.$current_version);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000323 return $current_version;
324 }
325
326 // --------------------------------------------------------------------
327
328 /**
Jacob Tabak35f6a542014-02-19 15:38:26 -0600329 * Sets the schema to the latest migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000330 *
ftwbzhao8d132bb2015-05-21 20:28:54 +0800331 * @return mixed Current version string on success, FALSE on failure
Phil Sturgeon9758d842011-02-07 20:39:00 +0000332 */
333 public function latest()
334 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400335 $migrations = $this->find_migrations();
Andrey Andreev39eb8062012-11-13 03:36:40 +0200336
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400337 if (empty($migrations))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000338 {
Cloudmanic Labs, LLC3d036e32011-11-11 22:46:21 -0800339 $this->_error_string = $this->lang->line('migration_none_found');
Alex Bilbieafee2262012-07-15 18:59:01 +0100340 return FALSE;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000341 }
342
343 $last_migration = basename(end($migrations));
Andrey Andreev39eb8062012-11-13 03:36:40 +0200344
Phil Sturgeon9758d842011-02-07 20:39:00 +0000345 // Calculate the last migration step from existing migration
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200346 // filenames and proceed to the standard version migration
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400347 return $this->version($this->_get_migration_number($last_migration));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000348 }
349
350 // --------------------------------------------------------------------
351
352 /**
Jacob Tabak35f6a542014-02-19 15:38:26 -0600353 * Sets the schema to the migration version set in config
Phil Sturgeon9758d842011-02-07 20:39:00 +0000354 *
ftwbzhaob8978ee2015-05-20 16:10:35 +0800355 * @return mixed TRUE if no migrations are found, current version string on success, FALSE on failure
Phil Sturgeon9758d842011-02-07 20:39:00 +0000356 */
357 public function current()
358 {
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100359 return $this->version($this->_migration_version);
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
365 * Error string
366 *
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100367 * @return string Error message returned as a string
368 */
369 public function error_string()
370 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100371 return $this->_error_string;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000372 }
373
374 // --------------------------------------------------------------------
375
376 /**
Dumk05db48272012-07-05 02:58:10 +0300377 * Retrieves list of available migration scripts
Phil Sturgeon9758d842011-02-07 20:39:00 +0000378 *
Dumk05db48272012-07-05 02:58:10 +0300379 * @return array list of migration file paths sorted by version
Phil Sturgeon9758d842011-02-07 20:39:00 +0000380 */
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400381 public function find_migrations()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000382 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400383 $migrations = array();
Andrey Andreev39eb8062012-11-13 03:36:40 +0200384
Phil Sturgeon9758d842011-02-07 20:39:00 +0000385 // Load all *_*.php files in the migrations path
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400386 foreach (glob($this->_migration_path.'*_*.php') as $file)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000387 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400388 $name = basename($file, '.php');
Andrey Andreev39eb8062012-11-13 03:36:40 +0200389
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400390 // Filter out non-migration files
391 if (preg_match($this->_migration_regex, $name))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000392 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400393 $number = $this->_get_migration_number($name);
Andrey Andreev39eb8062012-11-13 03:36:40 +0200394
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400395 // There cannot be duplicate migration numbers
396 if (isset($migrations[$number]))
397 {
398 $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $number);
399 show_error($this->_error_string);
400 }
Andrey Andreev39eb8062012-11-13 03:36:40 +0200401
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400402 $migrations[$number] = $file;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000403 }
404 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500405
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400406 ksort($migrations);
407 return $migrations;
408 }
409
410 // --------------------------------------------------------------------
411
412 /**
413 * Extracts the migration number from a filename
414 *
Andrey Andreev39eb8062012-11-13 03:36:40 +0200415 * @param string $migration
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200416 * @return string Numeric portion of a migration filename
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400417 */
418 protected function _get_migration_number($migration)
419 {
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200420 return sscanf($migration, '%[0-9]+', $number)
421 ? $number : '0';
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * Extracts the migration class name from a filename
428 *
Andrey Andreev39eb8062012-11-13 03:36:40 +0200429 * @param string $migration
430 * @return string text portion of a migration filename
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400431 */
432 protected function _get_migration_name($migration)
433 {
434 $parts = explode('_', $migration);
435 array_shift($parts);
436 return implode('_', $parts);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000437 }
438
439 // --------------------------------------------------------------------
440
441 /**
442 * Retrieves current schema version
443 *
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200444 * @return string Current migration version
Phil Sturgeon9758d842011-02-07 20:39:00 +0000445 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100446 protected function _get_version()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000447 {
Phil Sturgeond268eda2011-12-31 16:20:11 +0000448 $row = $this->db->select('version')->get($this->_migration_table)->row();
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200449 return $row ? $row->version : '0';
Phil Sturgeon9758d842011-02-07 20:39:00 +0000450 }
451
452 // --------------------------------------------------------------------
453
454 /**
455 * Stores the current schema version
456 *
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200457 * @param string $migration Migration reached
Gabriel Potkánycea5fb72015-02-04 08:22:06 +0100458 * @return void
Phil Sturgeon9758d842011-02-07 20:39:00 +0000459 */
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400460 protected function _update_version($migration)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000461 {
Gabriel Potkánycea5fb72015-02-04 08:22:06 +0100462 $this->db->update($this->_migration_table, array(
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400463 'version' => $migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000464 ));
465 }
466
467 // --------------------------------------------------------------------
468
469 /**
Phil Sturgeon9758d842011-02-07 20:39:00 +0000470 * Enable the use of CI super-global
471 *
Andrey Andreev39eb8062012-11-13 03:36:40 +0200472 * @param string $var
Phil Sturgeon9758d842011-02-07 20:39:00 +0000473 * @return mixed
474 */
475 public function __get($var)
476 {
477 return get_instance()->$var;
478 }
Andrey Andreev56454792012-05-17 14:32:19 +0300479
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100480}