1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-13 19:15:20 +02:00

[feature/migrations] Creating revert method to attempt reverting a migration

This code is in progress

PHPBB3-9737
This commit is contained in:
Nathan Guse 2013-01-09 18:59:15 -06:00
parent 595246f9bf
commit 44c10f661e
2 changed files with 123 additions and 0 deletions

View File

@ -93,6 +93,16 @@ abstract class phpbb_db_migration
return array();
}
/**
* Reverts the database schema by providing a set of change instructions
*
* @return array Array of schema changes (compatible with db_tools->perform_schema_changes())
*/
public function revert_schema()
{
return array();
}
/**
* Updates data by returning a list of instructions to be executed
*
@ -103,6 +113,19 @@ abstract class phpbb_db_migration
return array();
}
/**
* Reverts data by returning a list of instructions to be executed
*
* @return array Array of data instructions that will be performed on revert
* NOTE: calls to tools (such as config.add) are automatically reverted when
* possible, so you should not attempt to revert those, this is mostly for
* otherwise unrevertable calls (custom functions for example)
*/
public function revert_data()
{
return array();
}
/**
* Wrapper for running queries to generate user feedback on updates
*

View File

@ -289,6 +289,91 @@ class phpbb_db_migrator
return true;
}
/**
* Runs a single revert step from the last migration installed
*
* The revert step can either be a schema or a (partial) data revert. To
* check if revert() needs to be called again use the migration_installed() method.
*
* @param string $migration String migration name to revert (including any that depend on this migration)
* @return null
*/
public function revert($migration)
{
if (!isset($this->migration_state[$name]))
{
// Not installed
return;
}
// Iterate through all installed migrations and make sure any dependencies are removed first
foreach ($this->migration_state as $name => $state)
{
$migration_class = $this->get_migration($name);
if (in_array($migration, $migration_class->depends_on()))
{
$this->revert($name);
}
}
$this->try_revert($migration);
}
/**
* Attempts to apply a step of the given migration or one of its dependencies
*
* @param string 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,
);
// Left off here
if (!isset($this->migration_state[$name]))
{
$state['migration_start_time'] = time();
$this->insert_migration($name, $state);
}
if (!$state['migration_schema_done'])
{
$this->apply_schema_changes($migration->update_schema());
$state['migration_schema_done'] = true;
}
else
{
$result = $this->process_data_step($migration, $state['migration_data_state']);
$state['migration_data_state'] = ($result === true) ? '' : $result;
$state['migration_data_done'] = ($result === true);
$state['migration_end_time'] = ($result === true) ? time() : 0;
}
$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;
}
/**
* Apply schema changes from a migration
*
@ -359,6 +444,7 @@ class phpbb_db_migrator
}
}
/** TODO Revert Schema **/
var_dump($step);
echo $e;
die();
@ -567,6 +653,20 @@ class phpbb_db_migrator
return true;
}
/**
* Checks whether a migration is installed
*
* @param string $migration String migration name to check if it is installed
* @return bool Whether the migrations have been applied
*/
public function migration_installed($migration)
{
if (isset($this->migration_state[$migration]))
{
return true;
}
}
/**
* Helper to get a migration
*