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