From e050cf5c1163dc18e948ac0706b86743243d72f5 Mon Sep 17 00:00:00 2001 From: Oliver Schramm Date: Sat, 20 Aug 2016 03:35:38 +0200 Subject: [PATCH 1/6] [ticket/14742] Fix comments in migrator PHPBB3-14742 --- phpBB/phpbb/db/migration/tool/config.php | 2 +- phpBB/phpbb/db/migration/tool/config_text.php | 2 +- phpBB/phpbb/db/migration/tool/module.php | 2 +- phpBB/phpbb/db/migration/tool/permission.php | 2 +- phpBB/phpbb/db/migrator.php | 3 ++- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/phpBB/phpbb/db/migration/tool/config.php b/phpBB/phpbb/db/migration/tool/config.php index 2a76409db5..33aa8ff026 100644 --- a/phpBB/phpbb/db/migration/tool/config.php +++ b/phpBB/phpbb/db/migration/tool/config.php @@ -152,7 +152,7 @@ class config implements \phpbb\db\migration\tool\tool_interface break; case 'reverse': - // It's like double negative + // Reversing a reverse is just the call itself $call = array_shift($arguments); break; } diff --git a/phpBB/phpbb/db/migration/tool/config_text.php b/phpBB/phpbb/db/migration/tool/config_text.php index 21b8a8b3a0..54b45f6f6d 100644 --- a/phpBB/phpbb/db/migration/tool/config_text.php +++ b/phpBB/phpbb/db/migration/tool/config_text.php @@ -117,7 +117,7 @@ class config_text implements \phpbb\db\migration\tool\tool_interface break; case 'reverse': - // It's like double negative + // Reversing a reverse is just the call itself $call = array_shift($arguments); break; } diff --git a/phpBB/phpbb/db/migration/tool/module.php b/phpBB/phpbb/db/migration/tool/module.php index a6baf40dc1..59719db9a4 100644 --- a/phpBB/phpbb/db/migration/tool/module.php +++ b/phpBB/phpbb/db/migration/tool/module.php @@ -456,7 +456,7 @@ class module implements \phpbb\db\migration\tool\tool_interface break; case 'reverse': - // It's like double negative + // Reversing a reverse is just the call itself $call = array_shift($arguments); break; } diff --git a/phpBB/phpbb/db/migration/tool/permission.php b/phpBB/phpbb/db/migration/tool/permission.php index 3a1e2e344b..9688420025 100644 --- a/phpBB/phpbb/db/migration/tool/permission.php +++ b/phpBB/phpbb/db/migration/tool/permission.php @@ -639,7 +639,7 @@ class permission implements \phpbb\db\migration\tool\tool_interface break; case 'reverse': - // It's like double negative + // Reversing a reverse is just the call itself $call = array_shift($arguments); break; } diff --git a/phpBB/phpbb/db/migrator.php b/phpBB/phpbb/db/migrator.php index 65db4b74bf..9bfafd4d3f 100644 --- a/phpBB/phpbb/db/migrator.php +++ b/phpBB/phpbb/db/migrator.php @@ -530,7 +530,8 @@ 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 the last result is null or true, this means + // the last method call was finished and we can move on if ($last_result === null || $last_result === true) { continue; From 775d1c855a393b2b4f3d900608943c8fc453c44f Mon Sep 17 00:00:00 2001 From: Oliver Schramm Date: Sat, 20 Aug 2016 05:42:40 +0200 Subject: [PATCH 2/6] [ticket/14742] Improve readability of the code PHPBB3-14742 --- phpBB/phpbb/db/migrator.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phpBB/phpbb/db/migrator.php b/phpBB/phpbb/db/migrator.php index 9bfafd4d3f..2b7353d2b9 100644 --- a/phpBB/phpbb/db/migrator.php +++ b/phpBB/phpbb/db/migrator.php @@ -320,8 +320,10 @@ class migrator $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; @@ -355,7 +357,9 @@ class migrator $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; From 7c99fcf782b02431a4121b08f5a3cdb16477d200 Mon Sep 17 00:00:00 2001 From: Oliver Schramm Date: Sat, 20 Aug 2016 05:39:45 +0200 Subject: [PATCH 3/6] [ticket/14742] Pause after each update_data step too Rewriting process_data_step() to remove the now useless foreach() loop. PHPBB3-14742 --- phpBB/phpbb/db/migrator.php | 90 +++++++++++++++---------------------- 1 file changed, 37 insertions(+), 53 deletions(-) diff --git a/phpBB/phpbb/db/migrator.php b/phpBB/phpbb/db/migrator.php index 2b7353d2b9..edc132e546 100644 --- a/phpBB/phpbb/db/migrator.php +++ b/phpBB/phpbb/db/migrator.php @@ -506,6 +506,11 @@ class migrator */ protected function process_data_step($steps, $state, $revert = false) { + if (sizeof($steps) === 0) + { + return true; + } + $state = is_array($state) ? $state : false; // reverse order of steps if reverting @@ -514,66 +519,45 @@ class migrator $steps = array_reverse($steps); } - end($steps); - $last_step_identifier = key($steps); - - foreach ($steps as $step_identifier => $step) + $step = $last_result = 0; + if ($state) { - $last_result = 0; - if ($state) + $step = $state['step']; + + // We send the result from last time to the callable function + $last_result = $state['result']; + } + + try + { + // Result will be null or true if everything completed correctly + // Stop after each update step, to let the updater control the script runtime + $result = $this->run_step($steps[$step], $last_result, $revert); + if (($result !== null && $result !== true) || $step + 1 < sizeof($steps)) { - // Continue until we reach the step that matches the last step called - if ($state['step'] != $step_identifier) + return array( + 'result' => $result, + // Move on if the last call finished + 'step' => ($result !== null && $result !== true) ? $step : $step + 1, + ); + } + } + catch (\phpbb\db\migration\exception $e) + { + // We should try rolling back here + foreach ($steps as $reverse_step_identifier => $reverse_step) + { + // If we've reached the current step we can break because we reversed everything that was run + if ($reverse_step_identifier == $step) { - continue; + break; } - // We send the result from last time to the callable function - $last_result = $state['result']; - - // Set state to false since we reached the point we were at - $state = false; - - // If the last result is null or true, this means - // the last method call was finished and we can move on - if ($last_result === null || $last_result === true) - { - continue; - } + // Reverse the step that was run + $result = $this->run_step($reverse_step, false, !$revert); } - 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) || - (strpos($step[0], 'dbtools') === 0 && $step_identifier !== $last_step_identifier)) - { - return array( - 'result' => $result, - 'step' => $step_identifier, - ); - } - } - catch (\phpbb\db\migration\exception $e) - { - // We should try rolling back here - foreach ($steps as $reverse_step_identifier => $reverse_step) - { - // If we've reached the current step we can break because we reversed everything that was run - if ($reverse_step_identifier == $step_identifier) - { - break; - } - - // Reverse the step that was run - $result = $this->run_step($reverse_step, false, !$revert); - } - - throw $e; - } + throw $e; } return true; From 463e8e4b13373f771395d21c28b1522e302adcd7 Mon Sep 17 00:00:00 2001 From: Oliver Schramm Date: Sat, 20 Aug 2016 22:28:29 +0200 Subject: [PATCH 4/6] [ticket/14742] Change constants to use Symfony values This is to avoid errors when comparing verbosity levels in a CLI output handler that is using Symfony's OutputInterface for example. PHPBB3-14742 --- phpBB/phpbb/db/migrator_output_handler_interface.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/phpBB/phpbb/db/migrator_output_handler_interface.php b/phpBB/phpbb/db/migrator_output_handler_interface.php index a923af99f6..9947b51dcc 100644 --- a/phpBB/phpbb/db/migrator_output_handler_interface.php +++ b/phpBB/phpbb/db/migrator_output_handler_interface.php @@ -15,11 +15,11 @@ namespace phpbb\db; interface migrator_output_handler_interface { - const VERBOSITY_QUIET = 0; - const VERBOSITY_NORMAL = 1; - const VERBOSITY_VERBOSE = 2; - const VERBOSITY_VERY_VERBOSE = 3; - const VERBOSITY_DEBUG = 4; + const VERBOSITY_QUIET = 16; + const VERBOSITY_NORMAL = 32; + const VERBOSITY_VERBOSE = 64; + const VERBOSITY_VERY_VERBOSE = 128; + const VERBOSITY_DEBUG = 256; /** * Write output using the configured closure. From 773f6d08a5f039eb0829886a6283a7ce1d97051e Mon Sep 17 00:00:00 2001 From: Oliver Schramm Date: Sat, 20 Aug 2016 22:31:08 +0200 Subject: [PATCH 5/6] [ticket/14742] Reset migration_data_state before reverting PHPBB3-14742 --- phpBB/phpbb/db/migrator.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/migrator.php b/phpBB/phpbb/db/migrator.php index edc132e546..4c4c0a8672 100644 --- a/phpBB/phpbb/db/migrator.php +++ b/phpBB/phpbb/db/migrator.php @@ -383,7 +383,10 @@ class migrator } catch (\phpbb\db\migration\exception $e) { - // Revert the schema changes + // Reset data state and revert the schema changes + $state['migration_data_state'] = ''; + $this->set_migration_state($name, $state); + $this->revert_do($name); throw $e; From a37f10ae0951f16b115f4a175cc546a515cf7937 Mon Sep 17 00:00:00 2001 From: Oliver Schramm Date: Sat, 20 Aug 2016 22:40:37 +0200 Subject: [PATCH 6/6] [ticket/14742] Increase user feedback by improving progress bar We now count and display each step that was done by increasing the task count. PHPBB3-14742 --- .../install/helper/iohandler/cli_iohandler.php | 11 ++++++++++- .../module/update_database/task/update.php | 16 +++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/phpBB/phpbb/install/helper/iohandler/cli_iohandler.php b/phpBB/phpbb/install/helper/iohandler/cli_iohandler.php index 2a41cb10ba..196cdcdaab 100644 --- a/phpBB/phpbb/install/helper/iohandler/cli_iohandler.php +++ b/phpBB/phpbb/install/helper/iohandler/cli_iohandler.php @@ -198,6 +198,16 @@ class cli_iohandler extends iohandler_base if ($this->output->getVerbosity() === OutputInterface::VERBOSITY_NORMAL) { + if ($this->progress_bar !== null) + { + // Symfony's ProgressBar is immutable regarding task_count, so delete the old and create a new one. + $this->progress_bar->clear(); + } + else + { + $this->io->newLine(2); + } + $this->progress_bar = $this->io->createProgressBar($task_count); $this->progress_bar->setFormat( " %current:3s%/%max:-3s% %bar% %percent:3s%%\n" . @@ -212,7 +222,6 @@ class cli_iohandler extends iohandler_base } $this->progress_bar->setMessage(''); - $this->io->newLine(2); $this->progress_bar->start(); } } diff --git a/phpBB/phpbb/install/module/update_database/task/update.php b/phpBB/phpbb/install/module/update_database/task/update.php index c69dafaa10..9d7ba2f919 100644 --- a/phpBB/phpbb/install/module/update_database/task/update.php +++ b/phpBB/phpbb/install/module/update_database/task/update.php @@ -158,18 +158,23 @@ class update extends task_base try { $this->migrator->update(); + $progress_count++; $last_run_migration = $this->migrator->get_last_run_migration(); if (isset($last_run_migration['effectively_installed']) && $last_run_migration['effectively_installed']) { - $progress_count += 2; - } - else if (($last_run_migration['task'] === 'process_schema_step' && $last_run_migration['state']['migration_schema_done']) || - ($last_run_migration['task'] === 'process_data_step' && $last_run_migration['state']['migration_data_done'])) - { + // We skipped two step, so increment $progress_count by another one $progress_count++; } + else if (($last_run_migration['task'] === 'process_schema_step' && !$last_run_migration['state']['migration_schema_done']) || + ($last_run_migration['task'] === 'process_data_step' && !$last_run_migration['state']['migration_data_done'])) + { + // We just run a step that wasn't counted yet so make it count + $migration_step_count++; + } + $this->iohandler->set_task_count($migration_step_count); + $this->installer_config->set_task_progress_count($migration_step_count); $this->iohandler->set_progress('STAGE_UPDATE_DATABASE', $progress_count); } catch (exception $e) @@ -184,6 +189,7 @@ class update extends task_base if ($this->installer_config->get_time_remaining() <= 0 || $this->installer_config->get_memory_remaining() <= 0) { $this->installer_config->set('database_update_count', $progress_count); + $this->installer_config->set('database_update_migration_steps', $migration_step_count); throw new resource_limit_reached_exception(); } }