Andrey Andreev | dd4702f | 2011-12-24 19:33:44 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
| 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2006 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 24 | * @since Version 3.0 |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 28 | /** |
| 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 | */ |
| 40 | class CI_Migration { |
| 41 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 42 | /** |
| 43 | * Whether the library is enabled |
| 44 | * |
| 45 | * @var bool |
| 46 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 47 | protected $_migration_enabled = FALSE; |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 48 | |
| 49 | /** |
| 50 | * Migration numbering style |
| 51 | * |
| 52 | * @var bool |
| 53 | */ |
| 54 | protected $_migration_style = 'sequential'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 55 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 56 | /** |
| 57 | * Path to migration classes |
| 58 | * |
| 59 | * @var string |
| 60 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 61 | protected $_migration_path = NULL; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 62 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 63 | /** |
| 64 | * Current migration version |
| 65 | * |
| 66 | * @var mixed |
| 67 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 68 | protected $_migration_version = 0; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 69 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 70 | /** |
| 71 | * Database table with migration info |
| 72 | * |
| 73 | * @var string |
| 74 | */ |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 75 | protected $_migration_table = 'migrations'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 76 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 77 | /** |
| 78 | * Whether to automatically run migrations |
| 79 | * |
| 80 | * @var bool |
| 81 | */ |
Cloudmanic Labs, LLC | d1ba8f7 | 2011-09-18 12:23:00 -0700 | [diff] [blame] | 82 | protected $_migration_auto_latest = FALSE; |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 83 | |
| 84 | /** |
| 85 | * Migration basename regex |
| 86 | * |
| 87 | * @var bool |
| 88 | */ |
| 89 | protected $_migration_regex = NULL; |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 90 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 91 | /** |
| 92 | * Error message |
| 93 | * |
| 94 | * @var string |
| 95 | */ |
Phil Sturgeon | cb06c65 | 2011-05-04 10:50:25 +0100 | [diff] [blame] | 96 | protected $_error_string = ''; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 97 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 98 | /** |
| 99 | * Initialize Migration Class |
| 100 | * |
| 101 | * @param array |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 102 | * @return void |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 103 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 104 | public function __construct($config = array()) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 105 | { |
| 106 | # Only run this constructor on main library load |
| 107 | if (get_parent_class($this) !== FALSE) |
| 108 | { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | foreach ($config as $key => $val) |
| 113 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 114 | $this->{'_'.$key} = $val; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | log_message('debug', 'Migrations class initialized'); |
| 118 | |
| 119 | // Are they trying to use migrations while it is disabled? |
| 120 | if ($this->_migration_enabled !== TRUE) |
| 121 | { |
| 122 | show_error('Migrations has been loaded but is disabled or set up incorrectly.'); |
| 123 | } |
| 124 | |
| 125 | // If not set, set it |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 126 | $this->_migration_path !== '' OR $this->_migration_path = APPPATH.'migrations/'; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 127 | |
| 128 | // Add trailing slash if not set |
| 129 | $this->_migration_path = rtrim($this->_migration_path, '/').'/'; |
| 130 | |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 131 | // Load migration language |
| 132 | $this->lang->load('migration'); |
| 133 | |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 134 | // They'll probably be using dbforge |
| 135 | $this->load->dbforge(); |
| 136 | |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 137 | // Make sure the migration table name was set. |
Cloudmanic Labs, LLC | 63b61e3 | 2011-09-19 09:35:05 -0700 | [diff] [blame] | 138 | if (empty($this->_migration_table)) |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 139 | { |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 140 | show_error('Migrations configuration file (migration.php) must have "migration_table" set.'); |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 141 | } |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 142 | |
| 143 | // Migration basename regex |
| 144 | $this->_migration_regex = $this->_migration_style === 'timestamp' ? '/^\d{14}_(\w+)$/' : '/^\d{3}_(\w+)$/'; |
| 145 | |
| 146 | // Make sure a valid migration numbering style was set. |
| 147 | if ( ! in_array($this->_migration_style, array('sequential', 'timestamp'))) |
| 148 | { |
| 149 | show_error('An invalid migration numbering style was specified: '.$this->_migration_style); |
| 150 | } |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 151 | |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 152 | // If the migrations table is missing, make it |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 153 | if ( ! $this->db->table_exists($this->_migration_table)) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 154 | { |
| 155 | $this->dbforge->add_field(array( |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 156 | 'version' => array('type' => 'BIGINT', 'constraint' => 3), |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 157 | )); |
| 158 | |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 159 | $this->dbforge->create_table($this->_migration_table, TRUE); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 160 | |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 161 | $this->db->insert($this->_migration_table, array('version' => 0)); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 162 | } |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 163 | |
Cloudmanic Labs, LLC | d1ba8f7 | 2011-09-18 12:23:00 -0700 | [diff] [blame] | 164 | // Do we auto migrate to the latest migration? |
Andrey Andreev | 9b1db79 | 2012-03-26 19:45:51 +0300 | [diff] [blame] | 165 | if ($this->_migration_auto_latest === TRUE && ! $this->latest()) |
Cloudmanic Labs, LLC | d1ba8f7 | 2011-09-18 12:23:00 -0700 | [diff] [blame] | 166 | { |
Andrey Andreev | dd4702f | 2011-12-24 19:33:44 +0200 | [diff] [blame] | 167 | show_error($this->error_string()); |
Cloudmanic Labs, LLC | d1ba8f7 | 2011-09-18 12:23:00 -0700 | [diff] [blame] | 168 | } |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // -------------------------------------------------------------------- |
| 172 | |
| 173 | /** |
| 174 | * Migrate to a schema version |
| 175 | * |
| 176 | * Calls each migration step required to get to the schema version of |
| 177 | * choice |
| 178 | * |
Andrey Andreev | 9b1db79 | 2012-03-26 19:45:51 +0300 | [diff] [blame] | 179 | * @param int Target schema version |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 180 | * @return mixed TRUE if already latest, FALSE if failed, int if upgraded |
| 181 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 182 | public function version($target_version) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 183 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 184 | $current_version = (int) $this->_get_version(); |
| 185 | $target_version = (int) $target_version; |
| 186 | |
| 187 | $migrations = $this->find_migrations(); |
| 188 | |
| 189 | if ($target_version > 0 AND ! isset($migrations[$target_version])) |
| 190 | { |
| 191 | $this->_error_string = sprintf($this->lang->line('migration_not_found'), $target_version); |
| 192 | return FALSE; |
| 193 | } |
| 194 | |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 195 | if ($target_version > $current_version) |
| 196 | { |
| 197 | // Moving Up |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 198 | $method = 'up'; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 199 | } |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 200 | else |
| 201 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 202 | // Moving Down, apply in reverse order |
| 203 | $method = 'down'; |
| 204 | krsort($migrations); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 205 | } |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 206 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 207 | if (empty($migrations)) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 208 | { |
| 209 | return TRUE; |
| 210 | } |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 211 | |
| 212 | $previous = FALSE; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 213 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 214 | // Validate all available migrations, and run the ones within our target range |
| 215 | foreach ($migrations as $number => $file) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 216 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 217 | // Check for sequence gaps |
| 218 | if ($this->_migration_style === 'sequential' AND $previous !== FALSE AND abs($number - $previous) > 1) |
| 219 | { |
| 220 | $this->_error_string = sprintf($this->lang->line('migration_sequence_gap'), $number); |
| 221 | return FALSE; |
| 222 | } |
| 223 | |
| 224 | include $file; |
| 225 | $class = 'Migration_'.ucfirst(strtolower($this->_get_migration_name(basename($file, '.php')))); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 226 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 227 | // Validate the migration file structure |
| 228 | if ( ! class_exists($class)) |
| 229 | { |
| 230 | $this->_error_string = sprintf($this->lang->line('migration_class_doesnt_exist'), $class); |
| 231 | return FALSE; |
| 232 | } |
| 233 | elseif ( ! is_callable(array($class, $method))) |
| 234 | { |
| 235 | $this->_error_string = sprintf($this->lang->line('migration_missing_'.$method.'_method'), $class); |
| 236 | return FALSE; |
| 237 | } |
| 238 | |
| 239 | $previous = $number; |
| 240 | |
| 241 | // Run migrations that are inside the target range |
| 242 | if ( |
| 243 | ($method === 'up' AND $number > $current_version AND $number <= $target_version) OR |
| 244 | ($method === 'down' AND $number <= $current_version AND $number > $target_version) |
| 245 | ) { |
| 246 | log_message('debug', 'Migrating '.$method.' from version '.$current_version.' to version '.$number); |
| 247 | call_user_func(array(new $class, $method)); |
| 248 | $current_version = $number; |
| 249 | $this->_update_version($current_version); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // This is necessary when moving down, since the the last migration applied |
| 254 | // will be the down() method for the next migration up from the target |
| 255 | if ($current_version <> $target_version) |
| 256 | { |
| 257 | $current_version = $target_version; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 258 | $this->_update_version($current_version); |
| 259 | } |
| 260 | |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 261 | log_message('debug', 'Finished migrating to '.$current_version); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 262 | |
| 263 | return $current_version; |
| 264 | } |
| 265 | |
| 266 | // -------------------------------------------------------------------- |
| 267 | |
| 268 | /** |
| 269 | * Set's the schema to the latest migration |
| 270 | * |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 271 | * @return mixed true if already latest, false if failed, int if upgraded |
| 272 | */ |
| 273 | public function latest() |
| 274 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 275 | $migrations = $this->find_migrations(); |
| 276 | |
| 277 | if (empty($migrations)) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 278 | { |
Cloudmanic Labs, LLC | 3d036e3 | 2011-11-11 22:46:21 -0800 | [diff] [blame] | 279 | $this->_error_string = $this->lang->line('migration_none_found'); |
Alex Bilbie | afee226 | 2012-07-15 18:59:01 +0100 | [diff] [blame] | 280 | return FALSE; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | $last_migration = basename(end($migrations)); |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 284 | |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 285 | // Calculate the last migration step from existing migration |
| 286 | // filenames and procceed to the standard version migration |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 287 | return $this->version($this->_get_migration_number($last_migration)); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // -------------------------------------------------------------------- |
| 291 | |
| 292 | /** |
| 293 | * Set's the schema to the migration version set in config |
| 294 | * |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 295 | * @return mixed true if already current, false if failed, int if upgraded |
| 296 | */ |
| 297 | public function current() |
| 298 | { |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 299 | return $this->version($this->_migration_version); |
| 300 | } |
| 301 | |
| 302 | // -------------------------------------------------------------------- |
| 303 | |
| 304 | /** |
| 305 | * Error string |
| 306 | * |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 307 | * @return string Error message returned as a string |
| 308 | */ |
| 309 | public function error_string() |
| 310 | { |
Phil Sturgeon | cb06c65 | 2011-05-04 10:50:25 +0100 | [diff] [blame] | 311 | return $this->_error_string; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | // -------------------------------------------------------------------- |
| 315 | |
| 316 | /** |
Dumk0 | 5db4827 | 2012-07-05 02:58:10 +0300 | [diff] [blame] | 317 | * Retrieves list of available migration scripts |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 318 | * |
Dumk0 | 5db4827 | 2012-07-05 02:58:10 +0300 | [diff] [blame] | 319 | * @return array list of migration file paths sorted by version |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 320 | */ |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 321 | public function find_migrations() |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 322 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 323 | $migrations = array(); |
| 324 | |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 325 | // Load all *_*.php files in the migrations path |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 326 | foreach (glob($this->_migration_path.'*_*.php') as $file) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 327 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 328 | $name = basename($file, '.php'); |
| 329 | |
| 330 | // Filter out non-migration files |
| 331 | if (preg_match($this->_migration_regex, $name)) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 332 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 333 | $number = $this->_get_migration_number($name); |
| 334 | |
| 335 | // There cannot be duplicate migration numbers |
| 336 | if (isset($migrations[$number])) |
| 337 | { |
| 338 | $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $number); |
| 339 | show_error($this->_error_string); |
| 340 | } |
| 341 | |
| 342 | $migrations[$number] = $file; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 345 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 346 | ksort($migrations); |
| 347 | return $migrations; |
| 348 | } |
| 349 | |
| 350 | // -------------------------------------------------------------------- |
| 351 | |
| 352 | /** |
| 353 | * Extracts the migration number from a filename |
| 354 | * |
| 355 | * @return int Numeric portion of a migration filename |
| 356 | */ |
| 357 | protected function _get_migration_number($migration) |
| 358 | { |
| 359 | $parts = explode('_', $migration); |
| 360 | return (int) $parts[0]; |
| 361 | } |
| 362 | |
| 363 | // -------------------------------------------------------------------- |
| 364 | |
| 365 | /** |
| 366 | * Extracts the migration class name from a filename |
| 367 | * |
| 368 | * @return string text portion of a migration filename |
| 369 | */ |
| 370 | protected function _get_migration_name($migration) |
| 371 | { |
| 372 | $parts = explode('_', $migration); |
| 373 | array_shift($parts); |
| 374 | return implode('_', $parts); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // -------------------------------------------------------------------- |
| 378 | |
| 379 | /** |
| 380 | * Retrieves current schema version |
| 381 | * |
Andrey Andreev | 9b1db79 | 2012-03-26 19:45:51 +0300 | [diff] [blame] | 382 | * @return int Current Migration |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 383 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 384 | protected function _get_version() |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 385 | { |
Phil Sturgeon | d268eda | 2011-12-31 16:20:11 +0000 | [diff] [blame] | 386 | $row = $this->db->select('version')->get($this->_migration_table)->row(); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 387 | return $row ? $row->version : 0; |
| 388 | } |
| 389 | |
| 390 | // -------------------------------------------------------------------- |
| 391 | |
| 392 | /** |
| 393 | * Stores the current schema version |
| 394 | * |
Andrey Andreev | 9b1db79 | 2012-03-26 19:45:51 +0300 | [diff] [blame] | 395 | * @param int Migration reached |
| 396 | * @return void Outputs a report of the migration |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 397 | */ |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 398 | protected function _update_version($migration) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 399 | { |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 400 | return $this->db->update($this->_migration_table, array( |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame^] | 401 | 'version' => $migration |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 402 | )); |
| 403 | } |
| 404 | |
| 405 | // -------------------------------------------------------------------- |
| 406 | |
| 407 | /** |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 408 | * Enable the use of CI super-global |
| 409 | * |
Andrey Andreev | 9b1db79 | 2012-03-26 19:45:51 +0300 | [diff] [blame] | 410 | * @param $var |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 411 | * @return mixed |
| 412 | */ |
| 413 | public function __get($var) |
| 414 | { |
| 415 | return get_instance()->$var; |
| 416 | } |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 417 | |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* End of file Migration.php */ |
Andrey Andreev | 9b1db79 | 2012-03-26 19:45:51 +0300 | [diff] [blame] | 421 | /* Location: ./system/libraries/Migration.php */ |