/**
  * {@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);
     }
 }
Example #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 \phpbb\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 \phpbb\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 (\phpbb\db\migration\exception $e) {
                 // Revert the schema changes
                 $this->revert_do($name);
                 // Rethrow exception
                 throw $e;
             }
         }
     }
     $this->set_migration_state($name, $state);
     return true;
 }
Example #3
0
	/**
	* Attempts to revert 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
	*/
	protected function try_revert($name)
	{
		if (!class_exists($name))
		{
			return false;
		}

		$migration = $this->get_migration($name);

		$state = $this->migration_state[$name];

		$this->last_run_migration = array(
			'name'	=> $name,
			'class'	=> $migration,
			'task'	=> '',
		);

		if ($state['migration_data_done'])
		{
			$this->output_handler->write(array('MIGRATION_REVERT_DATA_RUNNING', $name), migrator_output_handler_interface::VERBOSITY_VERBOSE);
			$elapsed_time = microtime(true);

			if ($state['migration_data_state'] !== 'revert_data')
			{
				$result = $this->process_data_step($migration->update_data(), $state['migration_data_state'], true);

				$state['migration_data_state'] = ($result === true) ? 'revert_data' : $result;
			}
			else
			{
				$result = $this->process_data_step($migration->revert_data(), '', false);

				$state['migration_data_state'] = ($result === true) ? '' : $result;
				$state['migration_data_done'] = ($result === true) ? false : true;
			}

			$this->set_migration_state($name, $state);

			$elapsed_time = microtime(true) - $elapsed_time;
			if ($state['migration_data_done'])
			{
				$this->output_handler->write(array('MIGRATION_REVERT_DATA_DONE', $name, $elapsed_time), migrator_output_handler_interface::VERBOSITY_NORMAL);
			}
			else
			{
				$this->output_handler->write(array('MIGRATION_REVERT_DATA_IN_PROGRESS', $name, $elapsed_time), migrator_output_handler_interface::VERBOSITY_VERY_VERBOSE);
			}
		}
		else if ($state['migration_schema_done'])
		{
			$this->output_handler->write(array('MIGRATION_REVERT_SCHEMA_RUNNING', $name), migrator_output_handler_interface::VERBOSITY_VERBOSE);
			$elapsed_time = microtime(true);

			$steps = $this->helper->get_schema_steps($migration->revert_schema());
			$result = $this->process_data_step($steps, $state['migration_data_state']);

			$state['migration_data_state'] = ($result === true) ? '' : $result;
			$state['migration_schema_done'] = ($result === true) ? false : true;

			if (!$state['migration_schema_done'])
			{
				$sql = 'DELETE FROM ' . $this->migrations_table . "
					WHERE migration_name = '" . $this->db->sql_escape($name) . "'";
				$this->db->sql_query($sql);

				unset($this->migration_state[$name]);
			}

			$elapsed_time = microtime(true) - $elapsed_time;
			$this->output_handler->write(array('MIGRATION_REVERT_SCHEMA_DONE', $name, $elapsed_time), migrator_output_handler_interface::VERBOSITY_NORMAL);
		}

		return true;
	}