Merge pull request #462 from cloudmanic/develop

Migrations: Made it so you can set the table name in the migration config.
diff --git a/application/config/migration.php b/application/config/migration.php
index dba8700..1f532f1 100644
--- a/application/config/migration.php
+++ b/application/config/migration.php
@@ -11,6 +11,35 @@
 */

 $config['migration_enabled'] = FALSE;

 

+/*

+|--------------------------------------------------------------------------

+| Migrations table

+|--------------------------------------------------------------------------

+|

+| This is the name of the table that will store the current migrations state.

+| When migrations runs it will store in a database table which migration 

+| level the system is at. It then compares the migration level in the this

+| table to the $config['migration_version'] if they are not the same it

+| will migrate up. This must be set.

+|

+*/

+$config['migration_table'] = 'migrations';

+

+

+/*

+|--------------------------------------------------------------------------

+| Auto Migrate To Latest

+|--------------------------------------------------------------------------

+|

+| If this is set to TRUE when you load the migrations class and have 

+| $config['migration_enabled'] set to TRUE the system will auto migrate

+| to your latest migration (whatever $config['migration_version'] is

+| set to). This way you do not have to call migrations anywhere else

+| in your code to have the latest migration.

+|

+*/

+$config['migration_auto_latest'] = FALSE;

+

 

 /*

 |--------------------------------------------------------------------------

diff --git a/system/libraries/Migration.php b/system/libraries/Migration.php
index 3734e18..840cefe 100644
--- a/system/libraries/Migration.php
+++ b/system/libraries/Migration.php
@@ -32,7 +32,9 @@
 	protected $_migration_enabled = FALSE;
 	protected $_migration_path = NULL;
 	protected $_migration_version = 0;
-
+	protected $_migration_table = 'migrations';
+	protected $_migration_auto_latest = FALSE;
+	
 	protected $_error_string = '';
 
 	public function __construct($config = array())
@@ -68,16 +70,31 @@
 		// They'll probably be using dbforge
 		$this->load->dbforge();
 
+		// 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.');			
+		}
+
 		// If the migrations table is missing, make it
-		if ( ! $this->db->table_exists('migrations'))
+		if ( ! $this->db->table_exists($this->_migration_table))
 		{
 			$this->dbforge->add_field(array(
 				'version' => array('type' => 'INT', 'constraint' => 3),
 			));
 
-			$this->dbforge->create_table('migrations', TRUE);
+			$this->dbforge->create_table($this->_migration_table, TRUE);
 
-			$this->db->insert('migrations', array('version' => 0));
+			$this->db->insert($this->_migration_table, array('version' => 0));
+		}
+		
+		// Do we auto migrate to the latest migration?
+		if ( $this->_migration_auto_latest == TRUE )
+		{
+			if ( ! $this->latest() )
+			{
+				show_error($this->error_string());
+			}
 		}
 	}
 
@@ -299,7 +316,7 @@
 	 */
 	protected function _get_version()
 	{
-		$row = $this->db->get('migrations')->row();
+		$row = $this->db->get($this->_migration_table)->row();
 		return $row ? $row->version : 0;
 	}
 
@@ -314,7 +331,7 @@
 	 */
 	protected function _update_version($migrations)
 	{
-		return $this->db->update('migrations', array(
+		return $this->db->update($this->_migration_table, array(
 			'version' => $migrations
 		));
 	}