Commits

Phil Sturgeon committed 37491ee

Added Migration class and language file.

Comments (0)

Files changed (3)

application/config/migration.php

 | be upgraded / downgraded to.
 |
 */
-$config['migration_version'] = 1;
+$config['migration_version'] = 0;
 
 
 /*
 |
 */
 $config['migration_path'] = APPPATH . 'migrations/';
+
+
+/* End of file migration.php */
+/* Location: ./application/config/migration.php */

system/language/english/migration_lang.php

+<?php
+
+$lang['migration_none_found']			= "No migrations were found.";
+$lang['migration_not_found']			= "This migration could not be found.";
+$lang['migration_multiple_version']		= "This are multiple migrations with the same version number: %d.";
+$lang['migration_class_doesnt_exist']	= "The migration class \"%s\" could not be found.";
+$lang['migration_missing_up_method']	= "The migration class \"%s\" is missing an 'up' method.";
+$lang['migration_missing_down_method']	= "The migration class \"%s\" is missing an 'up' method.";
+$lang['migration_invalid_filename']		= "Migration \"%s\" has an invalid filename.";
+
+
+/* End of file migration_lang.php */
+/* Location: ./system/language/english/migration_lang.php */

system/libraries/Migration.php

  */
 class CI_Migration {
 
-	private $_migration_enabled = FALSE;
-	private $_migration_path = NULL;
-	private $_migration_version = 0;
+	protected $_migration_enabled = FALSE;
+	protected $_migration_path = NULL;
+	protected $_migration_version = 0;
 
 	public $error = '';
 
-	function __construct($config = array())
+	public function __construct($config = array())
 	{
 		# Only run this constructor on main library load
 		if (get_parent_class($this) !== FALSE)
 		// 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();
 
 	 * @param $version integer	Target schema version
 	 * @return	mixed	TRUE if already latest, FALSE if failed, int if upgraded
 	 */
-	function version($target_version)
+	public function version($target_version)
 	{
 		$start = $current_version = $this->_get_version();
 		$stop = $target_version;
 			// Moving Down
 			$step = -1;
 		}
-
+		
 		$method = $step === 1 ? 'up' : 'down';
 		$migrations = array();
 
 			// Only one migration per step is permitted
 			if (count($f) > 1)
 			{
-				$this->error = sprintf($this->lang->line('multiple_migration_version'), $i);
+				$this->error = sprintf($this->lang->line('migration_multiple_version'), $i);
 				return FALSE;
 			}
 
 				// Cannot repeat a migration at different steps
 				if (in_array($match[1], $migrations))
 				{
-					$this->error = sprintf($this->lang->line('multiple_migrations_name'), $match[1]);
+					$this->error = sprintf($this->lang->line('migration_multiple_version'), $match[1]);
 					return FALSE;
 				}
 
 					return FALSE;
 				}
 
-				if ( ! is_callable(array($class, 'up')) || ! is_callable(array($class, 'down')))
+				if ( ! is_callable(array($class, $method)))
 				{
-					$this->error = sprintf($this->lang->line('wrong_migration_interface'), $class);
+					$this->error = sprintf($this->lang->line('migration_missing_'.$method.'_method'), $class);
 					return FALSE;
 				}
 
 			}
 			else
 			{
-				$this->error = sprintf($this->lang->line('invalid_migration_filename'), $file);
+				exit('313');
+				$this->error = sprintf($this->lang->line('migration_invalid_filename'), $file);
 				return FALSE;
 			}
 		}
 
-		$this->log('Current schema version: ' . $current_version);
+		log_message('debug', 'Current migration: ' . $current_version);
 
 		$version = $i + ($step == 1 ? -1 : 0);
 
 			return TRUE;
 		}
 
-		$this->log('Moving ' . $method . ' to version ' . $version);
+		log_message('debug', 'Migrating from ' . $method . ' to version ' . $version);
 
 		// Loop through the migrations
 		foreach ($migrations AS $migration)
 			$this->_update_version($current_version);
 		}
 
-		$this->log('All done. Schema is at version '.$current_version);
+		log_message('debug', 'Finished migrating to '.$current_version);
 
 		return $current_version;
 	}
 	{
 		if ( ! $migrations = $this->find_migrations())
 		{
-			throw new Exception('no_migrations_found');
+			$this->error = $this->line->lang('migration_none_found');
 			return false;
 		}
 
 		$last_migration = basename(end($migrations));
-
+		
 		// Calculate the last migration step from existing migration
 		// filenames and procceed to the standard version migration
-		$last_version = intval(substr($last_migration, 0, 3));
-		return $this->version($last_version);
+		return $this->version((int) substr($last_migration, 0, 3));
 	}
 
 	// --------------------------------------------------------------------
 	 */
 	public function current()
 	{
-		$version = $this->_migration_version;
-		return $this->version($version);
+		return $this->version($this->_migration_version);
+	}
+
+	// --------------------------------------------------------------------
+
+	/**
+	 * Error string
+	 *
+	 * @access	public
+	 * @return	string	Error message returned as a string
+	 */
+	public function error_string()
+	{
+		return $this->error;
 	}
 
 	// --------------------------------------------------------------------
 	/**
 	 * Set's the schema to the latest migration
 	 *
-	 * @access	public
+	 * @access	protected
 	 * @return	mixed	true if already latest, false if failed, int if upgraded
 	 */
-
-	protected static function find_migrations()
+	protected function find_migrations()
 	{
 		// Load all *_*.php files in the migrations path
 		$files = glob($this->_migration_path . '*_*.php');
 		$file_count = count($files);
-
+		
 		for ($i = 0; $i < $file_count; $i++)
 		{
 			// Mark wrongly formatted files as false for later filtering
 				$files[$i] = FALSE;
 			}
 		}
-
+		
 		sort($files);
 
 		return $files;
 	/**
 	 * Retrieves current schema version
 	 *
-	 * @access	private
-	 * @return	integer	Current Schema version
+	 * @access	protected
+	 * @return	integer	Current Migration
 	 */
-	private function _get_version()
+	protected function _get_version()
 	{
 		$row = $this->db->get('migrations')->row();
 		return $row ? $row->version : 0;
 	/**
 	 * Stores the current schema version
 	 *
-	 * @access	private
-	 * @param $migrations integer	Schema version reached
+	 * @access	protected
+	 * @param $migrations integer	Migration reached
 	 * @return	void					Outputs a report of the migration
 	 */
-	private function _update_version($migrations)
+	protected function _update_version($migrations)
 	{
 		return $this->db->update('migrations', array(
 			'version' => $migrations
 	// --------------------------------------------------------------------
 
 	/**
-	 * Stores the current schema version
-	 *
-	 * @access	private
-	 * @param $migrations integer	Schema version reached
-	 * @return	void					Outputs a report of the migration
-	 */
-	private function log($text)
-	{
-		echo $text.'<br/>';
-	}
-
-	// --------------------------------------------------------------------
-
-	/**
 	 * Enable the use of CI super-global
 	 *
 	 * @access	public
 	{
 		return get_instance()->$var;
 	}
-}
+}
+
+/* End of file Migration.php */
+/* Location: ./system/libraries/Migration.php */