blob: 2c4f34abc7df76c9f004894ac02a46d846cf9a49 [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 Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, 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 Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, 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 Andreeva8382792016-07-28 16:40:12 +0300291 elseif ( ! is_callable(array($class, $method)))
Jonathon Hill02ea66e2012-11-12 17:26:36 -0500292 {
Andrey Andreevf8490b92016-03-16 16:22:14 +0200293 $this->_error_string = sprintf($this->lang->line('migration_missing_'.$method.'_method'), $class);
294 return FALSE;
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400295 }
Andrey Andreevf8490b92016-03-16 16:22:14 +0200296
297 $pending[$number] = array($class, $method);
298 }
299
300 // Now just run the necessary migrations
301 foreach ($pending as $number => $migration)
302 {
303 log_message('debug', 'Migrating '.$method.' from version '.$current_version.' to version '.$number);
304
305 $migration[0] = new $migration[0];
306 call_user_func($migration);
307 $current_version = $number;
308 $this->_update_version($current_version);
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400309 }
Andrey Andreev39eb8062012-11-13 03:36:40 +0200310
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400311 // This is necessary when moving down, since the the last migration applied
312 // will be the down() method for the next migration up from the target
313 if ($current_version <> $target_version)
314 {
315 $current_version = $target_version;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000316 $this->_update_version($current_version);
317 }
318
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100319 log_message('debug', 'Finished migrating to '.$current_version);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000320 return $current_version;
321 }
322
323 // --------------------------------------------------------------------
324
325 /**
Jacob Tabak35f6a542014-02-19 15:38:26 -0600326 * Sets the schema to the latest migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000327 *
ftwbzhao8d132bb2015-05-21 20:28:54 +0800328 * @return mixed Current version string on success, FALSE on failure
Phil Sturgeon9758d842011-02-07 20:39:00 +0000329 */
330 public function latest()
331 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400332 $migrations = $this->find_migrations();
Andrey Andreev39eb8062012-11-13 03:36:40 +0200333
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400334 if (empty($migrations))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000335 {
Cloudmanic Labs, LLC3d036e32011-11-11 22:46:21 -0800336 $this->_error_string = $this->lang->line('migration_none_found');
Alex Bilbieafee2262012-07-15 18:59:01 +0100337 return FALSE;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000338 }
339
340 $last_migration = basename(end($migrations));
Andrey Andreev39eb8062012-11-13 03:36:40 +0200341
Phil Sturgeon9758d842011-02-07 20:39:00 +0000342 // Calculate the last migration step from existing migration
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200343 // filenames and proceed to the standard version migration
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400344 return $this->version($this->_get_migration_number($last_migration));
Phil Sturgeon9758d842011-02-07 20:39:00 +0000345 }
346
347 // --------------------------------------------------------------------
348
349 /**
Jacob Tabak35f6a542014-02-19 15:38:26 -0600350 * Sets the schema to the migration version set in config
Phil Sturgeon9758d842011-02-07 20:39:00 +0000351 *
ftwbzhaob8978ee2015-05-20 16:10:35 +0800352 * @return mixed TRUE if no migrations are found, current version string on success, FALSE on failure
Phil Sturgeon9758d842011-02-07 20:39:00 +0000353 */
354 public function current()
355 {
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100356 return $this->version($this->_migration_version);
357 }
358
359 // --------------------------------------------------------------------
360
361 /**
362 * Error string
363 *
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100364 * @return string Error message returned as a string
365 */
366 public function error_string()
367 {
Phil Sturgeoncb06c652011-05-04 10:50:25 +0100368 return $this->_error_string;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000369 }
370
371 // --------------------------------------------------------------------
372
373 /**
Dumk05db48272012-07-05 02:58:10 +0300374 * Retrieves list of available migration scripts
Phil Sturgeon9758d842011-02-07 20:39:00 +0000375 *
Dumk05db48272012-07-05 02:58:10 +0300376 * @return array list of migration file paths sorted by version
Phil Sturgeon9758d842011-02-07 20:39:00 +0000377 */
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400378 public function find_migrations()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000379 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400380 $migrations = array();
Andrey Andreev39eb8062012-11-13 03:36:40 +0200381
Phil Sturgeon9758d842011-02-07 20:39:00 +0000382 // Load all *_*.php files in the migrations path
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400383 foreach (glob($this->_migration_path.'*_*.php') as $file)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000384 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400385 $name = basename($file, '.php');
Andrey Andreev39eb8062012-11-13 03:36:40 +0200386
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400387 // Filter out non-migration files
388 if (preg_match($this->_migration_regex, $name))
Phil Sturgeon9758d842011-02-07 20:39:00 +0000389 {
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400390 $number = $this->_get_migration_number($name);
Andrey Andreev39eb8062012-11-13 03:36:40 +0200391
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400392 // There cannot be duplicate migration numbers
393 if (isset($migrations[$number]))
394 {
395 $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $number);
396 show_error($this->_error_string);
397 }
Andrey Andreev39eb8062012-11-13 03:36:40 +0200398
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400399 $migrations[$number] = $file;
Phil Sturgeon9758d842011-02-07 20:39:00 +0000400 }
401 }
Eric Barnesdd81c432011-11-16 11:07:35 -0500402
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400403 ksort($migrations);
404 return $migrations;
405 }
406
407 // --------------------------------------------------------------------
408
409 /**
410 * Extracts the migration number from a filename
411 *
Andrey Andreev39eb8062012-11-13 03:36:40 +0200412 * @param string $migration
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200413 * @return string Numeric portion of a migration filename
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400414 */
415 protected function _get_migration_number($migration)
416 {
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200417 return sscanf($migration, '%[0-9]+', $number)
418 ? $number : '0';
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400419 }
420
421 // --------------------------------------------------------------------
422
423 /**
424 * Extracts the migration class name from a filename
425 *
Andrey Andreev39eb8062012-11-13 03:36:40 +0200426 * @param string $migration
427 * @return string text portion of a migration filename
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400428 */
429 protected function _get_migration_name($migration)
430 {
431 $parts = explode('_', $migration);
432 array_shift($parts);
433 return implode('_', $parts);
Phil Sturgeon9758d842011-02-07 20:39:00 +0000434 }
435
436 // --------------------------------------------------------------------
437
438 /**
439 * Retrieves current schema version
440 *
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200441 * @return string Current migration version
Phil Sturgeon9758d842011-02-07 20:39:00 +0000442 */
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100443 protected function _get_version()
Phil Sturgeon9758d842011-02-07 20:39:00 +0000444 {
Phil Sturgeond268eda2011-12-31 16:20:11 +0000445 $row = $this->db->select('version')->get($this->_migration_table)->row();
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200446 return $row ? $row->version : '0';
Phil Sturgeon9758d842011-02-07 20:39:00 +0000447 }
448
449 // --------------------------------------------------------------------
450
451 /**
452 * Stores the current schema version
453 *
Andrey Andreev3a9f3252014-02-25 11:47:45 +0200454 * @param string $migration Migration reached
Gabriel Potkánycea5fb72015-02-04 08:22:06 +0100455 * @return void
Phil Sturgeon9758d842011-02-07 20:39:00 +0000456 */
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400457 protected function _update_version($migration)
Phil Sturgeon9758d842011-02-07 20:39:00 +0000458 {
Gabriel Potkánycea5fb72015-02-04 08:22:06 +0100459 $this->db->update($this->_migration_table, array(
Jonathon Hill34c8b9c2012-10-31 14:02:35 -0400460 'version' => $migration
Phil Sturgeon9758d842011-02-07 20:39:00 +0000461 ));
462 }
463
464 // --------------------------------------------------------------------
465
466 /**
Phil Sturgeon9758d842011-02-07 20:39:00 +0000467 * Enable the use of CI super-global
468 *
Andrey Andreev39eb8062012-11-13 03:36:40 +0200469 * @param string $var
Phil Sturgeon9758d842011-02-07 20:39:00 +0000470 * @return mixed
471 */
472 public function __get($var)
473 {
474 return get_instance()->$var;
475 }
Andrey Andreev56454792012-05-17 14:32:19 +0300476
Phil Sturgeon96bd33b2011-05-04 01:30:36 +0100477}