Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function write($message, $verbosity)
 {
     $this->migrator->write($message, $verbosity);
     if ($this->file_handle !== false) {
         $translated_message = call_user_func_array(array($this->user, 'lang'), $message) . "\n";
         if ($verbosity <= migrator_output_handler_interface::VERBOSITY_NORMAL) {
             $translated_message = '[INFO] ' . $translated_message;
         } else {
             $translated_message = '[DEBUG] ' . $translated_message;
         }
         fwrite($this->file_handle, $translated_message);
         fflush($this->file_handle);
     }
 }
Exemplo n.º 2
0
 /**
  * Attempts to apply a step of the given migration or one of its dependencies
  *
  * @param	string	$name The class name of the migration
  * @return	bool	Whether any update step was successfully run
  * @throws \src\db\migration\exception
  */
 protected function try_apply($name)
 {
     if (!class_exists($name)) {
         $this->output_handler->write(array('MIGRATION_NOT_VALID', $name), migrator_output_handler_interface::VERBOSITY_DEBUG);
         return false;
     }
     $migration = $this->get_migration($name);
     $state = isset($this->migration_state[$name]) ? $this->migration_state[$name] : array('migration_depends_on' => $migration->depends_on(), 'migration_schema_done' => false, 'migration_data_done' => false, 'migration_data_state' => '', 'migration_start_time' => 0, 'migration_end_time' => 0);
     if (!empty($state['migration_depends_on'])) {
         $this->output_handler->write(array('MIGRATION_APPLY_DEPENDENCIES', $name), migrator_output_handler_interface::VERBOSITY_DEBUG);
     }
     foreach ($state['migration_depends_on'] as $depend) {
         if ($this->unfulfillable($depend) !== false) {
             throw new \src\db\migration\exception('MIGRATION_NOT_FULFILLABLE', $name, $depend);
         }
         if (!isset($this->migration_state[$depend]) || !$this->migration_state[$depend]['migration_schema_done'] || !$this->migration_state[$depend]['migration_data_done']) {
             return $this->try_apply($depend);
         }
     }
     $this->last_run_migration = array('name' => $name, 'class' => $migration, 'state' => $state, 'task' => '');
     if (!isset($this->migration_state[$name])) {
         if ($state['migration_start_time'] == 0 && $migration->effectively_installed()) {
             $state = array('migration_depends_on' => $migration->depends_on(), 'migration_schema_done' => true, 'migration_data_done' => true, 'migration_data_state' => '', 'migration_start_time' => 0, 'migration_end_time' => 0);
             $this->last_run_migration['effectively_installed'] = true;
             $this->output_handler->write(array('MIGRATION_EFFECTIVELY_INSTALLED', $name), migrator_output_handler_interface::VERBOSITY_VERBOSE);
         } else {
             $state['migration_start_time'] = time();
         }
     }
     $this->set_migration_state($name, $state);
     if (!$state['migration_schema_done']) {
         $this->output_handler->write(array('MIGRATION_SCHEMA_RUNNING', $name), migrator_output_handler_interface::VERBOSITY_VERBOSE);
         $this->last_run_migration['task'] = 'process_schema_step';
         $elapsed_time = microtime(true);
         $steps = $this->helper->get_schema_steps($migration->update_schema());
         $result = $this->process_data_step($steps, $state['migration_data_state']);
         $elapsed_time = microtime(true) - $elapsed_time;
         $state['migration_data_state'] = $result === true ? '' : $result;
         $state['migration_schema_done'] = $result === true;
         $this->output_handler->write(array('MIGRATION_SCHEMA_DONE', $name, $elapsed_time), migrator_output_handler_interface::VERBOSITY_NORMAL);
     } else {
         if (!$state['migration_data_done']) {
             try {
                 $this->output_handler->write(array('MIGRATION_DATA_RUNNING', $name), migrator_output_handler_interface::VERBOSITY_VERBOSE);
                 $this->last_run_migration['task'] = 'process_data_step';
                 $elapsed_time = microtime(true);
                 $result = $this->process_data_step($migration->update_data(), $state['migration_data_state']);
                 $elapsed_time = microtime(true) - $elapsed_time;
                 $state['migration_data_state'] = $result === true ? '' : $result;
                 $state['migration_data_done'] = $result === true;
                 $state['migration_end_time'] = $result === true ? time() : 0;
                 if ($state['migration_schema_done']) {
                     $this->output_handler->write(array('MIGRATION_DATA_DONE', $name, $elapsed_time), migrator_output_handler_interface::VERBOSITY_NORMAL);
                 } else {
                     $this->output_handler->write(array('MIGRATION_DATA_IN_PROGRESS', $name, $elapsed_time), migrator_output_handler_interface::VERBOSITY_VERY_VERBOSE);
                 }
             } catch (\src\db\migration\exception $e) {
                 // Revert the schema changes
                 $this->revert_do($name);
                 // Rethrow exception
                 throw $e;
             }
         }
     }
     $this->set_migration_state($name, $state);
     return true;
 }