aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb
diff options
context:
space:
mode:
Diffstat (limited to 'phpBB/phpbb')
-rw-r--r--phpBB/phpbb/db/migration/helper.php32
-rw-r--r--phpBB/phpbb/db/migration/tool/config.php5
-rw-r--r--phpBB/phpbb/db/migration/tool/config_text.php5
-rw-r--r--phpBB/phpbb/db/migration/tool/module.php5
-rw-r--r--phpBB/phpbb/db/migration/tool/permission.php5
-rw-r--r--phpBB/phpbb/db/migrator.php107
6 files changed, 134 insertions, 25 deletions
diff --git a/phpBB/phpbb/db/migration/helper.php b/phpBB/phpbb/db/migration/helper.php
index e40deeb37b..bce2efff51 100644
--- a/phpBB/phpbb/db/migration/helper.php
+++ b/phpBB/phpbb/db/migration/helper.php
@@ -81,4 +81,36 @@ class helper
return $steps;
}
+
+ /**
+ * Reverse the update steps from an array of data changes
+ *
+ * 'If' statements and custom methods will be skipped, for all
+ * other calls the reverse method of the tool class will be called
+ *
+ * @param array $steps Update changes from migration
+ *
+ * @return array
+ */
+ public function reverse_update_data($steps)
+ {
+ $reversed_array = array();
+
+ foreach ($steps as $step)
+ {
+ $parts = explode('.', $step[0]);
+ $parameters = $step[1];
+
+ $class = $parts[0];
+ $method = isset($parts[1]) ? $parts[1] : false;
+
+ if ($class !== 'if' && $class !== 'custom')
+ {
+ array_unshift($parameters, $method);
+ $reversed_array[] = array($class . '.reverse', $parameters);
+ }
+ }
+
+ return array_reverse($reversed_array);
+ }
}
diff --git a/phpBB/phpbb/db/migration/tool/config.php b/phpBB/phpbb/db/migration/tool/config.php
index f93e7118c4..2a76409db5 100644
--- a/phpBB/phpbb/db/migration/tool/config.php
+++ b/phpBB/phpbb/db/migration/tool/config.php
@@ -150,6 +150,11 @@ class config implements \phpbb\db\migration\tool\tool_interface
$arguments[0],
);
break;
+
+ case 'reverse':
+ // It's like double negative
+ $call = array_shift($arguments);
+ break;
}
if ($call)
diff --git a/phpBB/phpbb/db/migration/tool/config_text.php b/phpBB/phpbb/db/migration/tool/config_text.php
index bf8ac55023..21b8a8b3a0 100644
--- a/phpBB/phpbb/db/migration/tool/config_text.php
+++ b/phpBB/phpbb/db/migration/tool/config_text.php
@@ -115,6 +115,11 @@ class config_text implements \phpbb\db\migration\tool\tool_interface
$arguments[] = '';
}
break;
+
+ case 'reverse':
+ // It's like double negative
+ $call = array_shift($arguments);
+ break;
}
if ($call)
diff --git a/phpBB/phpbb/db/migration/tool/module.php b/phpBB/phpbb/db/migration/tool/module.php
index 035625b095..a6baf40dc1 100644
--- a/phpBB/phpbb/db/migration/tool/module.php
+++ b/phpBB/phpbb/db/migration/tool/module.php
@@ -454,6 +454,11 @@ class module implements \phpbb\db\migration\tool\tool_interface
case 'remove':
$call = 'add';
break;
+
+ case 'reverse':
+ // It's like double negative
+ $call = array_shift($arguments);
+ break;
}
if ($call)
diff --git a/phpBB/phpbb/db/migration/tool/permission.php b/phpBB/phpbb/db/migration/tool/permission.php
index ceff6d7d5a..3a1e2e344b 100644
--- a/phpBB/phpbb/db/migration/tool/permission.php
+++ b/phpBB/phpbb/db/migration/tool/permission.php
@@ -637,6 +637,11 @@ class permission implements \phpbb\db\migration\tool\tool_interface
$arguments[0],
);
break;
+
+ case 'reverse':
+ // It's like double negative
+ $call = array_shift($arguments);
+ break;
}
if ($call)
diff --git a/phpBB/phpbb/db/migrator.php b/phpBB/phpbb/db/migrator.php
index 7fc3e787e2..65db4b74bf 100644
--- a/phpBB/phpbb/db/migrator.php
+++ b/phpBB/phpbb/db/migrator.php
@@ -80,14 +80,14 @@ class migrator
*
* @var array
*/
- public $last_run_migration = false;
+ protected $last_run_migration = false;
/**
* The output handler. A null handler is configured by default.
*
* @var migrator_output_handler_interface
*/
- public $output_handler;
+ protected $output_handler;
/**
* Constructor of the database migrator
@@ -152,6 +152,7 @@ class migrator
$this->migration_state[$migration['migration_name']] = $migration;
$this->migration_state[$migration['migration_name']]['migration_depends_on'] = unserialize($migration['migration_depends_on']);
+ $this->migration_state[$migration['migration_name']]['migration_data_state'] = !empty($migration['migration_data_state']) ? unserialize($migration['migration_data_state']) : '';
}
}
@@ -161,6 +162,19 @@ class migrator
}
/**
+ * Get an array with information about the last migration run.
+ *
+ * The array contains 'name', 'class' and 'state'. 'effectively_installed' is set
+ * and set to true if the last migration was effectively_installed.
+ *
+ * @return array
+ */
+ public function get_last_run_migration()
+ {
+ return $this->last_run_migration;
+ }
+
+ /**
* Sets the list of available migration class names to the given array.
*
* @param array $class_names An array of migration class names
@@ -297,38 +311,66 @@ class migrator
if (!$state['migration_schema_done'])
{
- $this->output_handler->write(array('MIGRATION_SCHEMA_RUNNING', $name), migrator_output_handler_interface::VERBOSITY_VERBOSE);
+ $verbosity = empty($state['migration_data_state']) ?
+ migrator_output_handler_interface::VERBOSITY_VERBOSE : migrator_output_handler_interface::VERBOSITY_DEBUG;
+ $this->output_handler->write(array('MIGRATION_SCHEMA_RUNNING', $name), $verbosity);
$this->last_run_migration['task'] = 'process_schema_step';
+
+ $total_time = (is_array($state['migration_data_state']) && isset($state['migration_data_state']['_total_time'])) ?
+ $state['migration_data_state']['_total_time'] : 0.0;
$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;
+ $total_time += $elapsed_time;
+
+ if (is_array($result))
+ {
+ $result['_total_time'] = $total_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);
+ if ($state['migration_schema_done'])
+ {
+ $this->output_handler->write(array('MIGRATION_SCHEMA_DONE', $name, $total_time), migrator_output_handler_interface::VERBOSITY_NORMAL);
+ }
+ else
+ {
+ $this->output_handler->write(array('MIGRATION_SCHEMA_IN_PROGRESS', $name, $elapsed_time), migrator_output_handler_interface::VERBOSITY_VERY_VERBOSE);
+ }
}
else if (!$state['migration_data_done'])
{
try
{
- $this->output_handler->write(array('MIGRATION_DATA_RUNNING', $name), migrator_output_handler_interface::VERBOSITY_VERBOSE);
+ $verbosity = empty($state['migration_data_state']) ?
+ migrator_output_handler_interface::VERBOSITY_VERBOSE : migrator_output_handler_interface::VERBOSITY_DEBUG;
+ $this->output_handler->write(array('MIGRATION_DATA_RUNNING', $name), $verbosity);
$this->last_run_migration['task'] = 'process_data_step';
+ $total_time = (is_array($state['migration_data_state']) && isset($state['migration_data_state']['_total_time'])) ?
+ $state['migration_data_state']['_total_time'] : 0.0;
$elapsed_time = microtime(true);
$result = $this->process_data_step($migration->update_data(), $state['migration_data_state']);
$elapsed_time = microtime(true) - $elapsed_time;
+ $total_time += $elapsed_time;
+
+ if (is_array($result))
+ {
+ $result['_total_time'] = $total_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'])
+ if ($state['migration_data_done'])
{
- $this->output_handler->write(array('MIGRATION_DATA_DONE', $name, $elapsed_time), migrator_output_handler_interface::VERBOSITY_NORMAL);
+ $this->output_handler->write(array('MIGRATION_DATA_DONE', $name, $total_time), migrator_output_handler_interface::VERBOSITY_NORMAL);
}
else
{
@@ -340,7 +382,6 @@ class migrator
// Revert the schema changes
$this->revert_do($name);
- // Rethrow exception
throw $e;
}
}
@@ -416,19 +457,11 @@ class migrator
if ($state['migration_data_done'])
{
- 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);
+ $steps = array_merge($this->helper->reverse_update_data($migration->update_data()), $migration->revert_data());
+ $result = $this->process_data_step($steps, $state['migration_data_state']);
- $state['migration_data_state'] = ($result === true) ? '' : $result;
- $state['migration_data_done'] = ($result === true) ? false : true;
- }
+ $state['migration_data_state'] = ($result === true) ? '' : $result;
+ $state['migration_data_done'] = ($result === true) ? false : true;
$this->set_migration_state($name, $state);
}
@@ -446,8 +479,13 @@ class migrator
WHERE migration_name = '" . $this->db->sql_escape($name) . "'";
$this->db->sql_query($sql);
+ $this->last_run_migration = false;
unset($this->migration_state[$name]);
}
+ else
+ {
+ $this->set_migration_state($name, $state);
+ }
}
return true;
@@ -464,7 +502,7 @@ class migrator
*/
protected function process_data_step($steps, $state, $revert = false)
{
- $state = ($state) ? unserialize($state) : false;
+ $state = is_array($state) ? $state : false;
// reverse order of steps if reverting
if ($revert === true)
@@ -472,6 +510,9 @@ class migrator
$steps = array_reverse($steps);
}
+ end($steps);
+ $last_step_identifier = key($steps);
+
foreach ($steps as $step_identifier => $step)
{
$last_result = 0;
@@ -488,18 +529,27 @@ class migrator
// Set state to false since we reached the point we were at
$state = false;
+
+ // There is a tendency to get stuck in some cases
+ if ($last_result === null || $last_result === true)
+ {
+ continue;
+ }
}
try
{
// Result will be null or true if everything completed correctly
+ // After any schema update step we allow to pause, since
+ // database changes can take quite some time
$result = $this->run_step($step, $last_result, $revert);
- if ($result !== null && $result !== true)
+ if (($result !== null && $result !== true) ||
+ (strpos($step[0], 'dbtools') === 0 && $step_identifier !== $last_step_identifier))
{
- return serialize(array(
+ return array(
'result' => $result,
'step' => $step_identifier,
- ));
+ );
}
}
catch (\phpbb\db\migration\exception $e)
@@ -517,7 +567,6 @@ class migrator
$result = $this->run_step($reverse_step, false, !$revert);
}
- // rethrow the exception
throw $e;
}
}
@@ -587,6 +636,13 @@ class migrator
throw new \phpbb\db\migration\exception('MIGRATION_INVALID_DATA_MISSING_STEP', $step);
}
+ if ($reverse)
+ {
+ // We might get unexpected results when trying
+ // to revert this, so just avoid it
+ return false;
+ }
+
$condition = $parameters[0];
if (!$condition)
@@ -664,6 +720,7 @@ class migrator
{
$migration_row = $state;
$migration_row['migration_depends_on'] = serialize($state['migration_depends_on']);
+ $migration_row['migration_data_state'] = !empty($state['migration_data_state']) ? serialize($state['migration_data_state']) : '';
if (isset($this->migration_state[$name]))
{