1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-14 19:45:21 +02:00

[feature/migrations] Revert method completed

PHPBB3-9737
This commit is contained in:
Nathan Guse 2013-01-10 13:53:09 -06:00
parent 00385aa742
commit dbe71bb170

View File

@ -272,7 +272,7 @@ class phpbb_db_migrator
} }
else else
{ {
$result = $this->process_data_step($migration, $state['migration_data_state']); $result = $this->process_data_step($migration->update_data(), $state['migration_data_state']);
$state['migration_data_state'] = ($result === true) ? '' : $result; $state['migration_data_state'] = ($result === true) ? '' : $result;
$state['migration_data_done'] = ($result === true); $state['migration_data_done'] = ($result === true);
@ -300,7 +300,7 @@ class phpbb_db_migrator
*/ */
public function revert($migration) public function revert($migration)
{ {
if (!isset($this->migration_state[$name])) if (!isset($this->migration_state[$migration]))
{ {
// Not installed // Not installed
return; return;
@ -342,35 +342,40 @@ class phpbb_db_migrator
'class' => $migration, 'class' => $migration,
); );
// Left off here if ($state['migration_data_done'])
if (!isset($this->migration_state[$name]))
{ {
$state['migration_start_time'] = time(); if ($state['migration_data_state'] !== 'revert_data')
$this->insert_migration($name, $state); {
} $result = $this->process_data_step($migration->update_data(), $state['migration_data_state'], true);
if (!$state['migration_schema_done']) $state['migration_data_state'] = ($result === true) ? 'revert_data' : $result;
{ }
$this->apply_schema_changes($migration->update_schema()); else
$state['migration_schema_done'] = true; {
$result = $this->process_data_step($migration->revert_data(), $state['migration_data_state'], false);
$state['migration_data_state'] = ($result === true) ? '' : $result;
$state['migration_data_done'] = ($result === true) ? false : true;
}
$sql = 'UPDATE ' . $this->migrations_table . '
SET ' . $this->db->sql_build_array('UPDATE', $state) . "
WHERE migration_name = '" . $this->db->sql_escape($name) . "'";
$this->db->sql_query($sql);
$this->migration_state[$name] = $state;
} }
else else
{ {
$result = $this->process_data_step($migration, $state['migration_data_state']); $this->apply_schema_changes($migration->revert_schema());
$state['migration_data_state'] = ($result === true) ? '' : $result; $sql = 'DELETE FROM ' . $this->migrations_table . "
$state['migration_data_done'] = ($result === true); WHERE migration_name = '" . $this->db->sql_escape($name) . "'";
$state['migration_end_time'] = ($result === true) ? time() : 0; $this->db->sql_query($sql);
unset($this->migration_state[$name]);
} }
$sql = 'UPDATE ' . $this->migrations_table . '
SET ' . $this->db->sql_build_array('UPDATE', $state) . "
WHERE migration_name = '" . $this->db->sql_escape($name) . "'";
$this->db->sql_query($sql);
$this->migration_state[$name] = $state;
return true; return true;
} }
@ -389,23 +394,22 @@ class phpbb_db_migrator
/** /**
* Process the data step of the migration * Process the data step of the migration
* *
* @param phpbb_db_migration $migration * @param array $steps The steps to run
* @param bool|string $state Current state of the migration * @param bool|string $state Current state of the migration
* @param bool $revert true to revert a data step
* @return bool|string migration state. True if completed, serialized array if not finished * @return bool|string migration state. True if completed, serialized array if not finished
*/ */
protected function process_data_step($migration, $state) protected function process_data_step($steps, $state, $revert = false)
{ {
$state = ($state) ? unserialize($state) : false; $state = ($state) ? unserialize($state) : false;
$steps = $migration->update_data(); foreach ($steps as $step_identifier => $step)
foreach ($steps as $step)
{ {
$last_result = false; $last_result = false;
if ($state) if ($state)
{ {
// Continue until we reach the step that matches the last step called // Continue until we reach the step that matches the last step called
if ($state['step'] != $step) if ($state['step'] != $step_identifier)
{ {
continue; continue;
} }
@ -420,12 +424,12 @@ class phpbb_db_migrator
try try
{ {
// Result will be null or true if everything completed correctly // Result will be null or true if everything completed correctly
$result = $this->run_step($step, $last_result); $result = $this->run_step($step, $last_result, $revert);
if ($result !== null && $result !== true) if ($result !== null && $result !== true)
{ {
return serialize(array( return serialize(array(
'result' => $result, 'result' => $result,
'step' => $step, 'step' => $step_identifier,
)); ));
} }
} }
@ -435,7 +439,7 @@ class phpbb_db_migrator
foreach ($steps as $reverse_step) foreach ($steps as $reverse_step)
{ {
// Reverse the step that was run // Reverse the step that was run
$result = $this->run_step($step, false, true); $result = $this->run_step($step, false, !$revert);
// If we've reached the current step we can break because we reversed everything that was run // If we've reached the current step we can break because we reversed everything that was run
if ($reverse_step === $step) if ($reverse_step === $step)
@ -557,9 +561,11 @@ class phpbb_db_migrator
// Attempt to reverse operations // Attempt to reverse operations
if ($reverse) if ($reverse)
{ {
array_unshift($parameters, $method);
return array( return array(
array($this->tools[$class], 'reverse'), array($this->tools[$class], 'reverse'),
array_unshift($parameters, $method), $parameters,
); );
} }
@ -665,6 +671,8 @@ class phpbb_db_migrator
{ {
return true; return true;
} }
return false;
} }
/** /**