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 | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, 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 |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, 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 |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 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 | * |
Andrey Andreev | f8490b9 | 2016-03-16 16:22:14 +0200 | [diff] [blame] | 99 | * @var string |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 100 | */ |
Andrey Andreev | f8490b9 | 2016-03-16 16:22:14 +0200 | [diff] [blame] | 101 | protected $_migration_regex; |
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 | |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 129 | log_message('info', 'Migrations Class Initialized'); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 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 |
ftwbzhao | b8978ee | 2015-05-20 16:10:35 +0800 | [diff] [blame] | 194 | * @return mixed TRUE if no migrations are found, current version string on success, FALSE on failure |
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 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 220 | $method = 'up'; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 221 | } |
Andrey Andreev | 3df0772 | 2016-03-16 21:15:52 +0200 | [diff] [blame] | 222 | elseif ($target_version < $current_version) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 223 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 224 | $method = 'down'; |
Andrey Andreev | f8490b9 | 2016-03-16 16:22:14 +0200 | [diff] [blame] | 225 | // We need this so that migrations are applied in reverse order |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 226 | krsort($migrations); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 227 | } |
Andrey Andreev | 3df0772 | 2016-03-16 21:15:52 +0200 | [diff] [blame] | 228 | else |
| 229 | { |
| 230 | // Well, there's nothing to migrate then ... |
| 231 | return TRUE; |
| 232 | } |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 233 | |
Andrey Andreev | f8490b9 | 2016-03-16 16:22:14 +0200 | [diff] [blame] | 234 | // Validate all available migrations within our target range. |
| 235 | // |
| 236 | // Unfortunately, we'll have to use another loop to run them |
| 237 | // in order to avoid leaving the procedure in a broken state. |
| 238 | // |
| 239 | // See https://github.com/bcit-ci/CodeIgniter/issues/4539 |
| 240 | $pending = array(); |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 241 | foreach ($migrations as $number => $file) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 242 | { |
Andrey Andreev | f8490b9 | 2016-03-16 16:22:14 +0200 | [diff] [blame] | 243 | // Ignore versions out of our range. |
| 244 | // |
| 245 | // Because we've previously sorted the $migrations array depending on the direction, |
| 246 | // we can safely break the loop once we reach $target_version ... |
| 247 | if ($method === 'up') |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 248 | { |
Andrey Andreev | f8490b9 | 2016-03-16 16:22:14 +0200 | [diff] [blame] | 249 | if ($number <= $current_version) |
| 250 | { |
| 251 | continue; |
| 252 | } |
| 253 | elseif ($number > $target_version) |
| 254 | { |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | if ($number > $current_version) |
| 261 | { |
| 262 | continue; |
| 263 | } |
| 264 | elseif ($number <= $target_version) |
| 265 | { |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // Check for sequence gaps |
| 271 | if ($this->_migration_type === 'sequential') |
| 272 | { |
| 273 | if (isset($previous) && abs($number - $previous) > 1) |
| 274 | { |
| 275 | $this->_error_string = sprintf($this->lang->line('migration_sequence_gap'), $number); |
| 276 | return FALSE; |
| 277 | } |
| 278 | |
| 279 | $previous = $number; |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 280 | } |
Andrey Andreev | 39eb806 | 2012-11-13 03:36:40 +0200 | [diff] [blame] | 281 | |
Andrey Andreev | 3a9f325 | 2014-02-25 11:47:45 +0200 | [diff] [blame] | 282 | include_once($file); |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 283 | $class = 'Migration_'.ucfirst(strtolower($this->_get_migration_name(basename($file, '.php')))); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 284 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 285 | // Validate the migration file structure |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 286 | if ( ! class_exists($class, FALSE)) |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 287 | { |
| 288 | $this->_error_string = sprintf($this->lang->line('migration_class_doesnt_exist'), $class); |
| 289 | return FALSE; |
| 290 | } |
Andrey Andreev | a838279 | 2016-07-28 16:40:12 +0300 | [diff] [blame] | 291 | elseif ( ! is_callable(array($class, $method))) |
Jonathon Hill | 02ea66e | 2012-11-12 17:26:36 -0500 | [diff] [blame] | 292 | { |
Andrey Andreev | f8490b9 | 2016-03-16 16:22:14 +0200 | [diff] [blame] | 293 | $this->_error_string = sprintf($this->lang->line('migration_missing_'.$method.'_method'), $class); |
| 294 | return FALSE; |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 295 | } |
Andrey Andreev | f8490b9 | 2016-03-16 16:22:14 +0200 | [diff] [blame] | 296 | |
| 297 | $pending[$number] = array($class, $method); |
| 298 | } |
| 299 | |
| 300 | // Now just run the necessary migrations |
| 301 | foreach ($pending as $number => $migration) |
| 302 | { |
| 303 | log_message('debug', 'Migrating '.$method.' from version '.$current_version.' to version '.$number); |
| 304 | |
| 305 | $migration[0] = new $migration[0]; |
| 306 | call_user_func($migration); |
| 307 | $current_version = $number; |
| 308 | $this->_update_version($current_version); |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 309 | } |
Andrey Andreev | 39eb806 | 2012-11-13 03:36:40 +0200 | [diff] [blame] | 310 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 311 | // This is necessary when moving down, since the the last migration applied |
| 312 | // will be the down() method for the next migration up from the target |
| 313 | if ($current_version <> $target_version) |
| 314 | { |
| 315 | $current_version = $target_version; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 316 | $this->_update_version($current_version); |
| 317 | } |
| 318 | |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 319 | log_message('debug', 'Finished migrating to '.$current_version); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 320 | return $current_version; |
| 321 | } |
| 322 | |
| 323 | // -------------------------------------------------------------------- |
| 324 | |
| 325 | /** |
Jacob Tabak | 35f6a54 | 2014-02-19 15:38:26 -0600 | [diff] [blame] | 326 | * Sets the schema to the latest migration |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 327 | * |
ftwbzhao | 8d132bb | 2015-05-21 20:28:54 +0800 | [diff] [blame] | 328 | * @return mixed Current version string on success, FALSE on failure |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 329 | */ |
| 330 | public function latest() |
| 331 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 332 | $migrations = $this->find_migrations(); |
Andrey Andreev | 39eb806 | 2012-11-13 03:36:40 +0200 | [diff] [blame] | 333 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 334 | if (empty($migrations)) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 335 | { |
Cloudmanic Labs, LLC | 3d036e3 | 2011-11-11 22:46:21 -0800 | [diff] [blame] | 336 | $this->_error_string = $this->lang->line('migration_none_found'); |
Alex Bilbie | afee226 | 2012-07-15 18:59:01 +0100 | [diff] [blame] | 337 | return FALSE; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | $last_migration = basename(end($migrations)); |
Andrey Andreev | 39eb806 | 2012-11-13 03:36:40 +0200 | [diff] [blame] | 341 | |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 342 | // Calculate the last migration step from existing migration |
Andrey Andreev | 3a9f325 | 2014-02-25 11:47:45 +0200 | [diff] [blame] | 343 | // filenames and proceed to the standard version migration |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 344 | return $this->version($this->_get_migration_number($last_migration)); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | // -------------------------------------------------------------------- |
| 348 | |
| 349 | /** |
Jacob Tabak | 35f6a54 | 2014-02-19 15:38:26 -0600 | [diff] [blame] | 350 | * Sets the schema to the migration version set in config |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 351 | * |
ftwbzhao | b8978ee | 2015-05-20 16:10:35 +0800 | [diff] [blame] | 352 | * @return mixed TRUE if no migrations are found, current version string on success, FALSE on failure |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 353 | */ |
| 354 | public function current() |
| 355 | { |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 356 | return $this->version($this->_migration_version); |
| 357 | } |
| 358 | |
| 359 | // -------------------------------------------------------------------- |
| 360 | |
| 361 | /** |
| 362 | * Error string |
| 363 | * |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 364 | * @return string Error message returned as a string |
| 365 | */ |
| 366 | public function error_string() |
| 367 | { |
Phil Sturgeon | cb06c65 | 2011-05-04 10:50:25 +0100 | [diff] [blame] | 368 | return $this->_error_string; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // -------------------------------------------------------------------- |
| 372 | |
| 373 | /** |
Dumk0 | 5db4827 | 2012-07-05 02:58:10 +0300 | [diff] [blame] | 374 | * Retrieves list of available migration scripts |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 375 | * |
Dumk0 | 5db4827 | 2012-07-05 02:58:10 +0300 | [diff] [blame] | 376 | * @return array list of migration file paths sorted by version |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 377 | */ |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 378 | public function find_migrations() |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 379 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 380 | $migrations = array(); |
Andrey Andreev | 39eb806 | 2012-11-13 03:36:40 +0200 | [diff] [blame] | 381 | |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 382 | // Load all *_*.php files in the migrations path |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 383 | foreach (glob($this->_migration_path.'*_*.php') as $file) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 384 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 385 | $name = basename($file, '.php'); |
Andrey Andreev | 39eb806 | 2012-11-13 03:36:40 +0200 | [diff] [blame] | 386 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 387 | // Filter out non-migration files |
| 388 | if (preg_match($this->_migration_regex, $name)) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 389 | { |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 390 | $number = $this->_get_migration_number($name); |
Andrey Andreev | 39eb806 | 2012-11-13 03:36:40 +0200 | [diff] [blame] | 391 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 392 | // There cannot be duplicate migration numbers |
| 393 | if (isset($migrations[$number])) |
| 394 | { |
| 395 | $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $number); |
| 396 | show_error($this->_error_string); |
| 397 | } |
Andrey Andreev | 39eb806 | 2012-11-13 03:36:40 +0200 | [diff] [blame] | 398 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 399 | $migrations[$number] = $file; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
Eric Barnes | dd81c43 | 2011-11-16 11:07:35 -0500 | [diff] [blame] | 402 | |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 403 | ksort($migrations); |
| 404 | return $migrations; |
| 405 | } |
| 406 | |
| 407 | // -------------------------------------------------------------------- |
| 408 | |
| 409 | /** |
| 410 | * Extracts the migration number from a filename |
| 411 | * |
Andrey Andreev | 39eb806 | 2012-11-13 03:36:40 +0200 | [diff] [blame] | 412 | * @param string $migration |
Andrey Andreev | 3a9f325 | 2014-02-25 11:47:45 +0200 | [diff] [blame] | 413 | * @return string Numeric portion of a migration filename |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 414 | */ |
| 415 | protected function _get_migration_number($migration) |
| 416 | { |
Andrey Andreev | 3a9f325 | 2014-02-25 11:47:45 +0200 | [diff] [blame] | 417 | return sscanf($migration, '%[0-9]+', $number) |
| 418 | ? $number : '0'; |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | // -------------------------------------------------------------------- |
| 422 | |
| 423 | /** |
| 424 | * Extracts the migration class name from a filename |
| 425 | * |
Andrey Andreev | 39eb806 | 2012-11-13 03:36:40 +0200 | [diff] [blame] | 426 | * @param string $migration |
| 427 | * @return string text portion of a migration filename |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 428 | */ |
| 429 | protected function _get_migration_name($migration) |
| 430 | { |
| 431 | $parts = explode('_', $migration); |
| 432 | array_shift($parts); |
| 433 | return implode('_', $parts); |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | // -------------------------------------------------------------------- |
| 437 | |
| 438 | /** |
| 439 | * Retrieves current schema version |
| 440 | * |
Andrey Andreev | 3a9f325 | 2014-02-25 11:47:45 +0200 | [diff] [blame] | 441 | * @return string Current migration version |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 442 | */ |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 443 | protected function _get_version() |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 444 | { |
Phil Sturgeon | d268eda | 2011-12-31 16:20:11 +0000 | [diff] [blame] | 445 | $row = $this->db->select('version')->get($this->_migration_table)->row(); |
Andrey Andreev | 3a9f325 | 2014-02-25 11:47:45 +0200 | [diff] [blame] | 446 | return $row ? $row->version : '0'; |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // -------------------------------------------------------------------- |
| 450 | |
| 451 | /** |
| 452 | * Stores the current schema version |
| 453 | * |
Andrey Andreev | 3a9f325 | 2014-02-25 11:47:45 +0200 | [diff] [blame] | 454 | * @param string $migration Migration reached |
Gabriel Potkány | cea5fb7 | 2015-02-04 08:22:06 +0100 | [diff] [blame] | 455 | * @return void |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 456 | */ |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 457 | protected function _update_version($migration) |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 458 | { |
Gabriel Potkány | cea5fb7 | 2015-02-04 08:22:06 +0100 | [diff] [blame] | 459 | $this->db->update($this->_migration_table, array( |
Jonathon Hill | 34c8b9c | 2012-10-31 14:02:35 -0400 | [diff] [blame] | 460 | 'version' => $migration |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 461 | )); |
| 462 | } |
| 463 | |
| 464 | // -------------------------------------------------------------------- |
| 465 | |
| 466 | /** |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 467 | * Enable the use of CI super-global |
| 468 | * |
Andrey Andreev | 39eb806 | 2012-11-13 03:36:40 +0200 | [diff] [blame] | 469 | * @param string $var |
Phil Sturgeon | 9758d84 | 2011-02-07 20:39:00 +0000 | [diff] [blame] | 470 | * @return mixed |
| 471 | */ |
| 472 | public function __get($var) |
| 473 | { |
| 474 | return get_instance()->$var; |
| 475 | } |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 476 | |
Phil Sturgeon | 96bd33b | 2011-05-04 01:30:36 +0100 | [diff] [blame] | 477 | } |