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