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