/** * Extends migration mechanism to create backup before running migrations and remove it on success, but keep on failure * (non-PHPdoc) * @see CI_Migration::version() */ public function version($target_version) { // Make DB backup $backupFile = $this->_make_backup(); if ($backupFile === FALSE) { log_message('DEBUG', 'Making backup before migrating failed.'); } // Run migrationgs $migrationsResult = parent::version($target_version); // If everything went well - remove backup if ($migrationsResult !== FALSE && $migrationsResult == $target_version) { if ($backupFile !== FALSE && !$this->_delete_backup($backupFile)) { log_message('debug', 'There was an error when removing backup file.'); } } else { if ($backupFile !== FALSE) { log_message('error', 'Migrating to version ' . $target_version . ' failed. Backup from before migration stored in ' . $backupFile); } else { log_message('error', 'Migrating to version ' . $target_version . ' failed, but no valid backup saved.'); } } return $migrationsResult; }