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; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 48 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 49 | /** |
| 50 | * Path to migration classes |
| 51 | * |
| 52 | * @var string |
| 53 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 54 | protected $_migration_path = NULL; |
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 | * Current migration version |
| 58 | * |
| 59 | * @var mixed |
| 60 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 61 | protected $_migration_version = 0; |
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 | * Database table with migration info |
| 65 | * |
| 66 | * @var string |
| 67 | */ |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 68 | protected $_migration_table = 'migrations'; |
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 | * Whether to automatically run migrations |
| 72 | * |
| 73 | * @var bool |
| 74 | */ |
Cloudmanic Labs, LLC | d1ba8f7 | 2011-09-18 12:23:00 -0700 | [diff] [blame] | 75 | protected $_migration_auto_latest = FALSE; |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 76 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 77 | /** |
| 78 | * Error message |
| 79 | * |
| 80 | * @var string |
| 81 | */ |
Phil Sturgeon | cb06c65 | 2011-05-04 10:50:25 +0100 | [diff] [blame] | 82 | protected $_error_string = ''; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 83 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 84 | /** |
| 85 | * Initialize Migration Class |
| 86 | * |
| 87 | * @param array |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 88 | * @return void |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 89 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 90 | public function __construct($config = array()) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 91 | { |
| 92 | # Only run this constructor on main library load |
| 93 | if (get_parent_class($this) !== FALSE) |
| 94 | { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | foreach ($config as $key => $val) |
| 99 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 100 | $this->{'_'.$key} = $val; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | log_message('debug', 'Migrations class initialized'); |
| 104 | |
| 105 | // Are they trying to use migrations while it is disabled? |
| 106 | if ($this->_migration_enabled !== TRUE) |
| 107 | { |
| 108 | show_error('Migrations has been loaded but is disabled or set up incorrectly.'); |
| 109 | } |
| 110 | |
| 111 | // If not set, set it |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 112 | $this->_migration_path !== '' OR $this->_migration_path = APPPATH.'migrations/'; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 113 | |
| 114 | // Add trailing slash if not set |
| 115 | $this->_migration_path = rtrim($this->_migration_path, '/').'/'; |
| 116 | |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 117 | // Load migration language |
| 118 | $this->lang->load('migration'); |
| 119 | |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 120 | // They'll probably be using dbforge |
| 121 | $this->load->dbforge(); |
| 122 | |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 123 | // Make sure the migration table name was set. |
Cloudmanic Labs, LLC | 63b61e3 | 2011-09-19 09:35:05 -0700 | [diff] [blame] | 124 | if (empty($this->_migration_table)) |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 125 | { |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 126 | 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] | 127 | } |
| 128 | |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 129 | // If the migrations table is missing, make it |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 130 | if ( ! $this->db->table_exists($this->_migration_table)) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 131 | { |
| 132 | $this->dbforge->add_field(array( |
| 133 | 'version' => array('type' => 'INT', 'constraint' => 3), |
| 134 | )); |
| 135 | |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 136 | $this->dbforge->create_table($this->_migration_table, TRUE); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 137 | |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 138 | $this->db->insert($this->_migration_table, array('version' => 0)); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 139 | } |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 140 | |
Cloudmanic Labs, LLC | d1ba8f7 | 2011-09-18 12:23:00 -0700 | [diff] [blame] | 141 | // Do we auto migrate to the latest migration? |
Andrey Andreev | 9b1db79 | 2012-03-26 19:45:51 +0300 | [diff] [blame] | 142 | if ($this->_migration_auto_latest === TRUE && ! $this->latest()) |
Cloudmanic Labs, LLC | d1ba8f7 | 2011-09-18 12:23:00 -0700 | [diff] [blame] | 143 | { |
Andrey Andreev | dd4702f | 2011-12-24 19:33:44 +0200 | [diff] [blame] | 144 | show_error($this->error_string()); |
Cloudmanic Labs, LLC | d1ba8f7 | 2011-09-18 12:23:00 -0700 | [diff] [blame] | 145 | } |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // -------------------------------------------------------------------- |
| 149 | |
| 150 | /** |
| 151 | * Migrate to a schema version |
| 152 | * |
| 153 | * Calls each migration step required to get to the schema version of |
| 154 | * choice |
| 155 | * |
Andrey Andreev | 9b1db79 | 2012-03-26 19:45:51 +0300 | [diff] [blame] | 156 | * @param int Target schema version |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 157 | * @return mixed TRUE if already latest, FALSE if failed, int if upgraded |
| 158 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 159 | public function version($target_version) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 160 | { |
| 161 | $start = $current_version = $this->_get_version(); |
| 162 | $stop = $target_version; |
| 163 | |
| 164 | if ($target_version > $current_version) |
| 165 | { |
| 166 | // Moving Up |
| 167 | ++$start; |
| 168 | ++$stop; |
| 169 | $step = 1; |
| 170 | } |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 171 | else |
| 172 | { |
| 173 | // Moving Down |
| 174 | $step = -1; |
| 175 | } |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 176 | |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 177 | $method = $step === 1 ? 'up' : 'down'; |
| 178 | $migrations = array(); |
| 179 | |
| 180 | // We now prepare to actually DO the migrations |
| 181 | // But first let's make sure that everything is the way it should be |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 182 | for ($i = $start; $i !== $stop; $i += $step) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 183 | { |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 184 | $f = glob(sprintf($this->_migration_path.'%03d_*.php', $i)); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 185 | |
| 186 | // Only one migration per step is permitted |
| 187 | if (count($f) > 1) |
| 188 | { |
Phil Sturgeon | cb06c65 | 2011-05-04 10:50:25 +0100 | [diff] [blame] | 189 | $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $i); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 190 | return FALSE; |
| 191 | } |
| 192 | |
| 193 | // Migration step not found |
Andrey Andreev | dd4702f | 2011-12-24 19:33:44 +0200 | [diff] [blame] | 194 | if (count($f) === 0) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 195 | { |
| 196 | // If trying to migrate up to a version greater than the last |
| 197 | // existing one, migrate to the last one. |
Andrey Andreev | dd4702f | 2011-12-24 19:33:44 +0200 | [diff] [blame] | 198 | if ($step === 1) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 199 | { |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | // If trying to migrate down but we're missing a step, |
| 204 | // something must definitely be wrong. |
Phil Sturgeon | cb06c65 | 2011-05-04 10:50:25 +0100 | [diff] [blame] | 205 | $this->_error_string = sprintf($this->lang->line('migration_not_found'), $i); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 206 | return FALSE; |
| 207 | } |
| 208 | |
| 209 | $file = basename($f[0]); |
| 210 | $name = basename($f[0], '.php'); |
| 211 | |
| 212 | // Filename validations |
| 213 | if (preg_match('/^\d{3}_(\w+)$/', $name, $match)) |
| 214 | { |
| 215 | $match[1] = strtolower($match[1]); |
| 216 | |
| 217 | // Cannot repeat a migration at different steps |
| 218 | if (in_array($match[1], $migrations)) |
| 219 | { |
Phil Sturgeon | cb06c65 | 2011-05-04 10:50:25 +0100 | [diff] [blame] | 220 | $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $match[1]); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 221 | return FALSE; |
| 222 | } |
| 223 | |
| 224 | include $f[0]; |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 225 | $class = 'Migration_'.ucfirst($match[1]); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 226 | |
| 227 | if ( ! class_exists($class)) |
| 228 | { |
Phil Sturgeon | cb06c65 | 2011-05-04 10:50:25 +0100 | [diff] [blame] | 229 | $this->_error_string = sprintf($this->lang->line('migration_class_doesnt_exist'), $class); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 230 | return FALSE; |
| 231 | } |
| 232 | |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 233 | if ( ! is_callable(array($class, $method))) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 234 | { |
Phil Sturgeon | cb06c65 | 2011-05-04 10:50:25 +0100 | [diff] [blame] | 235 | $this->_error_string = sprintf($this->lang->line('migration_missing_'.$method.'_method'), $class); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 236 | return FALSE; |
| 237 | } |
| 238 | |
| 239 | $migrations[] = $match[1]; |
| 240 | } |
| 241 | else |
| 242 | { |
Phil Sturgeon | cb06c65 | 2011-05-04 10:50:25 +0100 | [diff] [blame] | 243 | $this->_error_string = sprintf($this->lang->line('migration_invalid_filename'), $file); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 244 | return FALSE; |
| 245 | } |
| 246 | } |
| 247 | |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 248 | log_message('debug', 'Current migration: '.$current_version); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 249 | |
Andrey Andreev | dd4702f | 2011-12-24 19:33:44 +0200 | [diff] [blame] | 250 | $version = $i + ($step === 1 ? -1 : 0); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 251 | |
| 252 | // If there is nothing to do so quit |
| 253 | if ($migrations === array()) |
| 254 | { |
| 255 | return TRUE; |
| 256 | } |
| 257 | |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 258 | log_message('debug', 'Migrating from '.$method.' to version '.$version); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 259 | |
| 260 | // Loop through the migrations |
| 261 | foreach ($migrations AS $migration) |
| 262 | { |
| 263 | // Run the migration class |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 264 | $class = 'Migration_'.ucfirst(strtolower($migration)); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 265 | call_user_func(array(new $class, $method)); |
| 266 | |
| 267 | $current_version += $step; |
| 268 | $this->_update_version($current_version); |
| 269 | } |
| 270 | |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 271 | log_message('debug', 'Finished migrating to '.$current_version); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 272 | |
| 273 | return $current_version; |
| 274 | } |
| 275 | |
| 276 | // -------------------------------------------------------------------- |
| 277 | |
| 278 | /** |
| 279 | * Set's the schema to the latest migration |
| 280 | * |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 281 | * @return mixed true if already latest, false if failed, int if upgraded |
| 282 | */ |
| 283 | public function latest() |
| 284 | { |
| 285 | if ( ! $migrations = $this->find_migrations()) |
| 286 | { |
Cloudmanic Labs, LLC | 3d036e3 | 2011-11-11 22:46:21 -0800 | [diff] [blame] | 287 | $this->_error_string = $this->lang->line('migration_none_found'); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | |
| 291 | $last_migration = basename(end($migrations)); |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 292 | |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 293 | // Calculate the last migration step from existing migration |
| 294 | // filenames and procceed to the standard version migration |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 295 | return $this->version((int) substr($last_migration, 0, 3)); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // -------------------------------------------------------------------- |
| 299 | |
| 300 | /** |
| 301 | * Set's the schema to the migration version set in config |
| 302 | * |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 303 | * @return mixed true if already current, false if failed, int if upgraded |
| 304 | */ |
| 305 | public function current() |
| 306 | { |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 307 | return $this->version($this->_migration_version); |
| 308 | } |
| 309 | |
| 310 | // -------------------------------------------------------------------- |
| 311 | |
| 312 | /** |
| 313 | * Error string |
| 314 | * |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 315 | * @return string Error message returned as a string |
| 316 | */ |
| 317 | public function error_string() |
| 318 | { |
Phil Sturgeon | cb06c65 | 2011-05-04 10:50:25 +0100 | [diff] [blame] | 319 | return $this->_error_string; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // -------------------------------------------------------------------- |
| 323 | |
| 324 | /** |
| 325 | * Set's the schema to the latest migration |
| 326 | * |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 327 | * @return mixed true if already latest, false if failed, int if upgraded |
| 328 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 329 | protected function find_migrations() |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 330 | { |
| 331 | // Load all *_*.php files in the migrations path |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 332 | $files = glob($this->_migration_path.'*_*.php'); |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 333 | |
Andrey Andreev | dd4702f | 2011-12-24 19:33:44 +0200 | [diff] [blame] | 334 | for ($i = 0, $c = count($files); $i < $c; $i++) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 335 | { |
| 336 | // Mark wrongly formatted files as false for later filtering |
Andrey Andreev | dd4702f | 2011-12-24 19:33:44 +0200 | [diff] [blame] | 337 | if ( ! preg_match('/^\d{3}_(\w+)$/', basename($files[$i], '.php'))) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 338 | { |
| 339 | $files[$i] = FALSE; |
| 340 | } |
| 341 | } |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 342 | |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 343 | sort($files); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 344 | return $files; |
| 345 | } |
| 346 | |
| 347 | // -------------------------------------------------------------------- |
| 348 | |
| 349 | /** |
| 350 | * Retrieves current schema version |
| 351 | * |
Andrey Andreev | 9b1db79 | 2012-03-26 19:45:51 +0300 | [diff] [blame] | 352 | * @return int Current Migration |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 353 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 354 | protected function _get_version() |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 355 | { |
Phil Sturgeon | d268eda | 2011-12-31 16:20:11 +0000 | [diff] [blame] | 356 | $row = $this->db->select('version')->get($this->_migration_table)->row(); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 357 | return $row ? $row->version : 0; |
| 358 | } |
| 359 | |
| 360 | // -------------------------------------------------------------------- |
| 361 | |
| 362 | /** |
| 363 | * Stores the current schema version |
| 364 | * |
Andrey Andreev | 9b1db79 | 2012-03-26 19:45:51 +0300 | [diff] [blame] | 365 | * @param int Migration reached |
| 366 | * @return void Outputs a report of the migration |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 367 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 368 | protected function _update_version($migrations) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 369 | { |
Cloudmanic Labs, LLC | 539dcb0 | 2011-09-18 12:08:56 -0700 | [diff] [blame] | 370 | return $this->db->update($this->_migration_table, array( |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 371 | 'version' => $migrations |
| 372 | )); |
| 373 | } |
| 374 | |
| 375 | // -------------------------------------------------------------------- |
| 376 | |
| 377 | /** |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 378 | * Enable the use of CI super-global |
| 379 | * |
Andrey Andreev | 9b1db79 | 2012-03-26 19:45:51 +0300 | [diff] [blame] | 380 | * @param $var |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 381 | * @return mixed |
| 382 | */ |
| 383 | public function __get($var) |
| 384 | { |
| 385 | return get_instance()->$var; |
| 386 | } |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 387 | |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* End of file Migration.php */ |
Andrey Andreev | 9b1db79 | 2012-03-26 19:45:51 +0300 | [diff] [blame] | 391 | /* Location: ./system/libraries/Migration.php */ |