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