summaryrefslogtreecommitdiffstats
path: root/system/libraries/Migration.php
diff options
context:
space:
mode:
authorPhil Sturgeon <email@philsturgeon.co.uk>2011-05-04 02:30:36 +0200
committerPhil Sturgeon <email@philsturgeon.co.uk>2011-05-04 02:30:36 +0200
commit96bd33b2edc1b0e6a04cb8e3bcf97e8c7b3adf3e (patch)
tree259233389b50f115fd0b4fa137268f9fe6683be4 /system/libraries/Migration.php
parent57e5336547e80a81e20c08e80703d59af052c043 (diff)
Added Migration class and language file.
Diffstat (limited to 'system/libraries/Migration.php')
-rw-r--r--system/libraries/Migration.php95
1 files changed, 49 insertions, 46 deletions
diff --git a/system/libraries/Migration.php b/system/libraries/Migration.php
index 73c55c346..4bf1d0dc1 100644
--- a/system/libraries/Migration.php
+++ b/system/libraries/Migration.php
@@ -29,13 +29,13 @@
*/
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)
@@ -62,6 +62,9 @@ class CI_Migration {
// 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();
@@ -90,7 +93,7 @@ class CI_Migration {
* @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;
@@ -108,7 +111,7 @@ class CI_Migration {
// Moving Down
$step = -1;
}
-
+
$method = $step === 1 ? 'up' : 'down';
$migrations = array();
@@ -121,7 +124,7 @@ class CI_Migration {
// 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;
}
@@ -152,7 +155,7 @@ class CI_Migration {
// 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;
}
@@ -165,9 +168,9 @@ class CI_Migration {
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;
}
@@ -175,12 +178,13 @@ class CI_Migration {
}
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);
@@ -190,7 +194,7 @@ class CI_Migration {
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)
@@ -203,7 +207,7 @@ class CI_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;
}
@@ -220,16 +224,15 @@ class CI_Migration {
{
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));
}
// --------------------------------------------------------------------
@@ -242,25 +245,36 @@ class CI_Migration {
*/
public function current()
{
- $version = $this->_migration_version;
- return $this->version($version);
+ return $this->version($this->_migration_version);
}
// --------------------------------------------------------------------
/**
- * Set's the schema to the latest migration
+ * Error string
*
* @access public
- * @return mixed true if already latest, false if failed, int if upgraded
+ * @return string Error message returned as a string
*/
+ public function error_string()
+ {
+ return $this->error;
+ }
- protected static function find_migrations()
+ // --------------------------------------------------------------------
+
+ /**
+ * Set's the schema to the latest migration
+ *
+ * @access protected
+ * @return mixed true if already latest, false if failed, int if upgraded
+ */
+ 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
@@ -270,7 +284,7 @@ class CI_Migration {
$files[$i] = FALSE;
}
}
-
+
sort($files);
return $files;
@@ -281,10 +295,10 @@ class CI_Migration {
/**
* 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;
@@ -295,11 +309,11 @@ class CI_Migration {
/**
* 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
@@ -309,20 +323,6 @@ class CI_Migration {
// --------------------------------------------------------------------
/**
- * 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
@@ -333,4 +333,7 @@ class CI_Migration {
{
return get_instance()->$var;
}
-} \ No newline at end of file
+}
+
+/* End of file Migration.php */
+/* Location: ./system/libraries/Migration.php */ \ No newline at end of file