diff --git a/system/libraries/Migration.php b/system/libraries/Migration.php index 915d4e453..18214e63a 100644 --- a/system/libraries/Migration.php +++ b/system/libraries/Migration.php @@ -86,6 +86,13 @@ class CI_Migration { */ protected $_migration_table = 'migrations'; + /** + * Name and Path of the migration lockfile + * + * @var string + */ + protected $_migration_lockfile = APPPATH . 'cache/.migration_running'; + /** * Whether to automatically run migrations * @@ -297,15 +304,32 @@ class CI_Migration { $pending[$number] = array($class, $method); } - // Now just run the necessary migrations - foreach ($pending as $number => $migration) - { - log_message('debug', 'Migrating '.$method.' from version '.$current_version.' to version '.$number); + // Let's check if a migration is currently running + if (!file_exists($this->_migration_lockfile)) { - $migration[0] = new $migration[0]; - call_user_func($migration); - $current_version = $number; - $this->_update_version($current_version); + // Create the lockfile + if (touch($this->_migration_lockfile)) { + + // Now just run the necessary migrations + foreach ($pending as $number => $migration) + { + log_message('debug', 'Migrating '.$method.' from version '.$current_version.' to version '.$number); + + $migration[0] = new $migration[0]; + call_user_func($migration); + $current_version = $number; + $this->_update_version($current_version); + } + + // After the migrations we can remove the lockfile + unlink($this->_migration_lockfile); + + } else { + log_message('error', 'Failed to create Migration Lockfile. Check directory permissions.'); + } + + } else { + log_message('debug', 'Migration process is currently locked. Second migration attempt ignored.'); } // This is necessary when moving down, since the the last migration applied