| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * CodeIgniter | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  |  * An open source application development framework for PHP | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  |  * This content is released under the MIT License (MIT) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2014 - 2017, British Columbia Institute of Technology | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
					
						
							|  |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
					
						
							|  |  |  |  * in the Software without restriction, including without limitation the rights | 
					
						
							|  |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
					
						
							|  |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
					
						
							|  |  |  |  * furnished to do so, subject to the following conditions: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice shall be included in | 
					
						
							|  |  |  |  * all copies or substantial portions of the Software. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
					
						
							|  |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
					
						
							|  |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
					
						
							|  |  |  |  * THE SOFTWARE. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package	CodeIgniter | 
					
						
							|  |  |  |  * @author	EllisLab Dev Team | 
					
						
							|  |  |  |  * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) | 
					
						
							|  |  |  |  * @copyright	Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/) | 
					
						
							|  |  |  |  * @license	http://opensource.org/licenses/MIT	MIT License | 
					
						
							|  |  |  |  * @link	https://codeigniter.com | 
					
						
							|  |  |  |  * @since	Version 3.0.0 | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  |  * @filesource | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | defined('BASEPATH') OR exit('No direct script access allowed'); | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Migration Class | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * All migrations should implement this, forces up() and down() and gives | 
					
						
							|  |  |  |  * access to the CI super-global. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package		CodeIgniter | 
					
						
							|  |  |  |  * @subpackage	Libraries | 
					
						
							|  |  |  |  * @category	Libraries | 
					
						
							|  |  |  |  * @author		Reactor Engineers | 
					
						
							|  |  |  |  * @link | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class CI_Migration { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Whether the library is enabled | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var bool | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	protected $_migration_enabled = FALSE; | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Migration numbering type | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var	bool | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	protected $_migration_type = 'sequential'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Path to migration classes | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var string | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	protected $_migration_path = NULL; | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Current migration version | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var mixed | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	protected $_migration_version = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Database table with migration info | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var string | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	protected $_migration_table = 'migrations'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Whether to automatically run migrations | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var	bool | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	protected $_migration_auto_latest = FALSE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Migration basename regex | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var string | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	protected $_migration_regex; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Error message | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var string | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	protected $_error_string = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Initialize Migration Class | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @param	array	$config | 
					
						
							|  |  |  | 	 * @return	void | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	public function __construct($config = array()) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		// Only run this constructor on main library load
 | 
					
						
							|  |  |  | 		if ( ! in_array(get_class($this), array('CI_Migration', config_item('subclass_prefix').'Migration'), TRUE)) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		foreach ($config as $key => $val) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			$this->{'_'.$key} = $val; | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		log_message('info', 'Migrations Class Initialized'); | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Are they trying to use migrations while it is disabled?
 | 
					
						
							|  |  |  | 		if ($this->_migration_enabled !== TRUE) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			show_error('Migrations has been loaded but is disabled or set up incorrectly.'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// If not set, set it
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$this->_migration_path !== '' OR $this->_migration_path = APPPATH.'migrations/'; | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Add trailing slash if not set
 | 
					
						
							|  |  |  | 		$this->_migration_path = rtrim($this->_migration_path, '/').'/'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Load migration language
 | 
					
						
							|  |  |  | 		$this->lang->load('migration'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// They'll probably be using dbforge
 | 
					
						
							|  |  |  | 		$this->load->dbforge(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		// Make sure the migration table name was set.
 | 
					
						
							|  |  |  | 		if (empty($this->_migration_table)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			show_error('Migrations configuration file (migration.php) must have "migration_table" set.'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Migration basename regex
 | 
					
						
							|  |  |  | 		$this->_migration_regex = ($this->_migration_type === 'timestamp') | 
					
						
							|  |  |  | 			? '/^\d{14}_(\w+)$/' | 
					
						
							|  |  |  | 			: '/^\d{3}_(\w+)$/'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Make sure a valid migration numbering type was set.
 | 
					
						
							|  |  |  | 		if ( ! in_array($this->_migration_type, array('sequential', 'timestamp'))) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			show_error('An invalid migration numbering type was specified: '.$this->_migration_type); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		// If the migrations table is missing, make it
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		if ( ! $this->db->table_exists($this->_migration_table)) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			$this->dbforge->add_field(array( | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 				'version' => array('type' => 'BIGINT', 'constraint' => 20), | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 			)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			$this->dbforge->create_table($this->_migration_table, TRUE); | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			$this->db->insert($this->_migration_table, array('version' => 0)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Do we auto migrate to the latest migration?
 | 
					
						
							|  |  |  | 		if ($this->_migration_auto_latest === TRUE && ! $this->latest()) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			show_error($this->error_string()); | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Migrate to a schema version | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * Calls each migration step required to get to the schema version of | 
					
						
							|  |  |  | 	 * choice | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @param	string	$target_version	Target schema version | 
					
						
							|  |  |  | 	 * @return	mixed	TRUE if no migrations are found, current version string on success, FALSE on failure | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	public function version($target_version) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		// Note: We use strings, so that timestamp versions work on 32-bit systems
 | 
					
						
							|  |  |  | 		$current_version = $this->_get_version(); | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		if ($this->_migration_type === 'sequential') | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			$target_version = sprintf('%03d', $target_version); | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			$target_version = (string) $target_version; | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$migrations = $this->find_migrations(); | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		if ($target_version > 0 && ! isset($migrations[$target_version])) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			$this->_error_string = sprintf($this->lang->line('migration_not_found'), $target_version); | 
					
						
							|  |  |  | 			return FALSE; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		if ($target_version > $current_version) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			$method = 'up'; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		elseif ($target_version < $current_version) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			$method = 'down'; | 
					
						
							|  |  |  | 			// We need this so that migrations are applied in reverse order
 | 
					
						
							|  |  |  | 			krsort($migrations); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// Well, there's nothing to migrate then ...
 | 
					
						
							|  |  |  | 			return TRUE; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		// Validate all available migrations within our target range.
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							|  |  |  | 		// Unfortunately, we'll have to use another loop to run them
 | 
					
						
							|  |  |  | 		// in order to avoid leaving the procedure in a broken state.
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							|  |  |  | 		// See https://github.com/bcit-ci/CodeIgniter/issues/4539
 | 
					
						
							|  |  |  | 		$pending = array(); | 
					
						
							|  |  |  | 		foreach ($migrations as $number => $file) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// Ignore versions out of our range.
 | 
					
						
							|  |  |  | 			//
 | 
					
						
							|  |  |  | 			// Because we've previously sorted the $migrations array depending on the direction,
 | 
					
						
							|  |  |  | 			// we can safely break the loop once we reach $target_version ...
 | 
					
						
							|  |  |  | 			if ($method === 'up') | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 				if ($number <= $current_version) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					continue; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				elseif ($number > $target_version) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 				{ | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			else | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 				if ($number > $current_version) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 					continue; | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 				elseif ($number <= $target_version) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 					break; | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			// Check for sequence gaps
 | 
					
						
							|  |  |  | 			if ($this->_migration_type === 'sequential') | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if (isset($previous) && abs($number - $previous) > 1) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 					$this->_error_string = sprintf($this->lang->line('migration_sequence_gap'), $number); | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 					return FALSE; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 				$previous = $number; | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			include_once($file); | 
					
						
							|  |  |  | 			$class = 'Migration_'.ucfirst(strtolower($this->_get_migration_name(basename($file, '.php')))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Validate the migration file structure
 | 
					
						
							|  |  |  | 			if ( ! class_exists($class, FALSE)) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 				$this->_error_string = sprintf($this->lang->line('migration_class_doesnt_exist'), $class); | 
					
						
							|  |  |  | 				return FALSE; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			elseif ( ! is_callable(array($class, $method))) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				$this->_error_string = sprintf($this->lang->line('migration_missing_'.$method.'_method'), $class); | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 				return FALSE; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			$pending[$number] = array($class, $method); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		// Now just run the necessary migrations
 | 
					
						
							|  |  |  | 		foreach ($pending as $number => $migration) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			log_message('debug', 'Migrating '.$method.' from version '.$current_version.' to version '.$number); | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			$migration[0] = new $migration[0]; | 
					
						
							|  |  |  | 			call_user_func($migration); | 
					
						
							|  |  |  | 			$current_version = $number; | 
					
						
							|  |  |  | 			$this->_update_version($current_version); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		// This is necessary when moving down, since the the last migration applied
 | 
					
						
							|  |  |  | 		// will be the down() method for the next migration up from the target
 | 
					
						
							|  |  |  | 		if ($current_version <> $target_version) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			$current_version = $target_version; | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 			$this->_update_version($current_version); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		log_message('debug', 'Finished migrating to '.$current_version); | 
					
						
							|  |  |  | 		return $current_version; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * Sets the schema to the latest migration | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @return	mixed	Current version string on success, FALSE on failure | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	public function latest() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$migrations = $this->find_migrations(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (empty($migrations)) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			$this->_error_string = $this->lang->line('migration_none_found'); | 
					
						
							|  |  |  | 			return FALSE; | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$last_migration = basename(end($migrations)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Calculate the last migration step from existing migration
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		// filenames and proceed to the standard version migration
 | 
					
						
							|  |  |  | 		return $this->version($this->_get_migration_number($last_migration)); | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * Sets the schema to the migration version set in config | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @return	mixed	TRUE if no migrations are found, current version string on success, FALSE on failure | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	public function current() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return $this->version($this->_migration_version); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Error string | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @return	string	Error message returned as a string | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public function error_string() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return $this->_error_string; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * Retrieves list of available migration scripts | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @return	array	list of migration file paths sorted by version | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	public function find_migrations() | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$migrations = array(); | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		// Load all *_*.php files in the migrations path
 | 
					
						
							|  |  |  | 		foreach (glob($this->_migration_path.'*_*.php') as $file) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			$name = basename($file, '.php'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Filter out non-migration files
 | 
					
						
							|  |  |  | 			if (preg_match($this->_migration_regex, $name)) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 				$number = $this->_get_migration_number($name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// There cannot be duplicate migration numbers
 | 
					
						
							|  |  |  | 				if (isset($migrations[$number])) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					$this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $number); | 
					
						
							|  |  |  | 					show_error($this->_error_string); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				$migrations[$number] = $file; | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		ksort($migrations); | 
					
						
							|  |  |  | 		return $migrations; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Extracts the migration number from a filename | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @param	string	$migration | 
					
						
							|  |  |  | 	 * @return	string	Numeric portion of a migration filename | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	protected function _get_migration_number($migration) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return sscanf($migration, '%[0-9]+', $number) | 
					
						
							|  |  |  | 			? $number : '0'; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Extracts the migration class name from a filename | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @param	string	$migration | 
					
						
							|  |  |  | 	 * @return	string	text portion of a migration filename | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	protected function _get_migration_name($migration) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		$parts = explode('_', $migration); | 
					
						
							|  |  |  | 		array_shift($parts); | 
					
						
							|  |  |  | 		return implode('_', $parts); | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Retrieves current schema version | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @return	string	Current migration version | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	protected function _get_version() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$row = $this->db->select('version')->get($this->_migration_table)->row(); | 
					
						
							|  |  |  | 		return $row ? $row->version : '0'; | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Stores the current schema version | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @param	string	$migration	Migration reached | 
					
						
							|  |  |  | 	 * @return	void | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	protected function _update_version($migration) | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$this->db->update($this->_migration_table, array( | 
					
						
							|  |  |  | 			'version' => $migration | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 		)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Enable the use of CI super-global | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @param	string	$var | 
					
						
							| 
									
										
										
										
											2013-02-25 07:56:37 +08:00
										 |  |  | 	 * @return	mixed | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public function __get($var) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return get_instance()->$var; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | } |