aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/db/migrator.php
diff options
context:
space:
mode:
Diffstat (limited to 'phpBB/phpbb/db/migrator.php')
-rw-r--r--phpBB/phpbb/db/migrator.php237
1 files changed, 170 insertions, 67 deletions
diff --git a/phpBB/phpbb/db/migrator.php b/phpBB/phpbb/db/migrator.php
index 4fb3f1a241..d91860949a 100644
--- a/phpBB/phpbb/db/migrator.php
+++ b/phpBB/phpbb/db/migrator.php
@@ -1,28 +1,40 @@
<?php
/**
*
-* @package db
-* @copyright (c) 2011 phpBB Group
-* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+* This file is part of the phpBB Forum Software package.
+*
+* @copyright (c) phpBB Limited <https://www.phpbb.com>
+* @license GNU General Public License, version 2 (GPL-2.0)
+*
+* For full copyright and license information, please see
+* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db;
+use phpbb\db\output_handler\migrator_output_handler_interface;
+use phpbb\db\output_handler\null_migrator_output_handler;
+use Symfony\Component\DependencyInjection\ContainerAwareInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
/**
* The migrator is responsible for applying new migrations in the correct order.
-*
-* @package db
*/
class migrator
{
+ /**
+ * @var ContainerInterface
+ */
+ protected $container;
+
/** @var \phpbb\config\config */
protected $config;
- /** @var \phpbb\db\driver\driver */
+ /** @var \phpbb\db\driver\driver_interface */
protected $db;
- /** @var \phpbb\db\tools */
+ /** @var \phpbb\db\tools\tools_interface */
protected $db_tools;
/** @var \phpbb\db\migration\helper */
@@ -57,6 +69,13 @@ class migrator
protected $migrations = array();
/**
+ * Array of migrations that have been determined to be fulfillable
+ *
+ * @var array
+ */
+ protected $fulfillable_migrations = array();
+
+ /**
* 'name,' 'class,' and 'state' of the last migration run
*
* 'effectively_installed' set and set to true if the migration was effectively_installed
@@ -66,10 +85,18 @@ class migrator
public $last_run_migration = false;
/**
+ * The output handler. A null handler is configured by default.
+ *
+ * @var migrator_output_handler_interface
+ */
+ public $output_handler;
+
+ /**
* Constructor of the database migrator
*/
- public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver $db, \phpbb\db\tools $db_tools, $migrations_table, $phpbb_root_path, $php_ext, $table_prefix, $tools, \phpbb\db\migration\helper $helper)
+ public function __construct(ContainerInterface $container, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\db\tools\tools_interface $db_tools, $migrations_table, $phpbb_root_path, $php_ext, $table_prefix, $tools, \phpbb\db\migration\helper $helper)
{
+ $this->container = $container;
$this->config = $config;
$this->db = $db;
$this->db_tools = $db_tools;
@@ -82,6 +109,8 @@ class migrator
$this->table_prefix = $table_prefix;
+ $this->output_handler = new null_migrator_output_handler();
+
foreach ($tools as $tool)
{
$this->tools[$tool->get_name()] = $tool;
@@ -93,6 +122,16 @@ class migrator
}
/**
+ * Set the output handler.
+ *
+ * @param migrator_output_handler_interface $handler The output handler
+ */
+ public function set_output_handler(migrator_output_handler_interface $handler)
+ {
+ $this->output_handler = $handler;
+ }
+
+ /**
* Loads all migrations and their application state from the database.
*
* @return null
@@ -108,7 +147,7 @@ class migrator
FROM " . $this->migrations_table;
$result = $this->db->sql_query($sql);
- if (!$this->db->sql_error_triggered)
+ if (!$this->db->get_sql_error_triggered())
{
while ($migration = $this->db->sql_fetchrow($result))
{
@@ -144,6 +183,18 @@ class migrator
*/
public function update()
{
+ $this->container->get('dispatcher')->disable();
+ $this->update_do();
+ $this->container->get('dispatcher')->enable();
+ }
+
+ /**
+ * Effectively runs a single update step from the next migration to be applied.
+ *
+ * @return null
+ */
+ protected function update_do()
+ {
foreach ($this->migrations as $name)
{
if (!isset($this->migration_state[$name]) ||
@@ -159,19 +210,25 @@ class migrator
return;
}
}
+ else
+ {
+ $this->output_handler->write(array('MIGRATION_EFFECTIVELY_INSTALLED', $name), migrator_output_handler_interface::VERBOSITY_DEBUG);
+ }
}
}
/**
* Attempts to apply a step of the given migration or one of its dependencies
*
- * @param string The class name of the migration
+ * @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;
}
@@ -188,6 +245,11 @@ class migrator
'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)
@@ -224,6 +286,8 @@ class migrator
);
$this->last_run_migration['effectively_installed'] = true;
+
+ $this->output_handler->write(array('MIGRATION_EFFECTIVELY_INSTALLED', $name), migrator_output_handler_interface::VERBOSITY_VERBOSE);
}
else
{
@@ -235,28 +299,48 @@ class migrator
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($name);
+ $this->revert_do($name);
// Rethrow exception
throw $e;
@@ -276,10 +360,22 @@ class migrator
* check if revert() needs to be called again use the migration_state() method.
*
* @param string $migration String migration name to revert (including any that depend on this migration)
- * @return null
*/
public function revert($migration)
{
+ $this->container->get('dispatcher')->disable();
+ $this->revert_do($migration);
+ $this->container->get('dispatcher')->enable();
+ }
+
+ /**
+ * Effectively runs a single revert step from the last migration installed
+ *
+ * @param string $migration String migration name to revert (including any that depend on this migration)
+ * @return null
+ */
+ protected function revert_do($migration)
+ {
if (!isset($this->migration_state[$migration]))
{
// Not installed
@@ -290,7 +386,7 @@ class migrator
{
if (!empty($state['migration_depends_on']) && in_array($migration, $state['migration_depends_on']))
{
- $this->revert($name);
+ $this->revert_do($name);
}
}
@@ -300,7 +396,7 @@ class migrator
/**
* Attempts to revert a step of the given migration or one of its dependencies
*
- * @param string The class name of the migration
+ * @param string $name The class name of the migration
* @return bool Whether any update step was successfully run
*/
protected function try_revert($name)
@@ -322,6 +418,9 @@ class migrator
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);
@@ -337,9 +436,22 @@ class migrator
}
$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']);
@@ -354,6 +466,9 @@ class migrator
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;
@@ -366,6 +481,7 @@ class migrator
* @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
+ * @throws \phpbb\db\migration\exception
*/
protected function process_data_step($steps, $state, $revert = false)
{
@@ -419,7 +535,7 @@ class migrator
}
// Reverse the step that was run
- $result = $this->run_step($reverse_step, false, !$revert);
+ $this->run_step($reverse_step, false, !$revert);
}
// rethrow the exception
@@ -462,6 +578,7 @@ class migrator
* @param mixed $last_result Result to pass to the callable (only for 'custom' method)
* @param bool $reverse False to install, True to attempt uninstallation by reversing the call
* @return array Array with parameters for call_user_func_array(), 0 is the callable, 1 is parameters
+ * @throws \phpbb\db\migration\exception
*/
protected function get_callable_from_step(array $step, $last_result = 0, $reverse = false)
{
@@ -509,10 +626,17 @@ class migrator
throw new \phpbb\db\migration\exception('MIGRATION_INVALID_DATA_CUSTOM_NOT_CALLABLE', $step);
}
- return array(
- $parameters[0],
- array($last_result),
- );
+ if ($reverse)
+ {
+ return false;
+ }
+ else
+ {
+ return array(
+ $parameters[0],
+ array($last_result),
+ );
+ }
break;
default:
@@ -590,7 +714,7 @@ class migrator
*/
public function unfulfillable($name)
{
- if (isset($this->migration_state[$name]))
+ if (isset($this->migration_state[$name]) || isset($this->fulfillable_migrations[$name]))
{
return false;
}
@@ -611,6 +735,7 @@ class migrator
return $unfulfillable;
}
}
+ $this->fulfillable_migrations[$name] = true;
return false;
}
@@ -671,7 +796,14 @@ class migrator
*/
protected function get_migration($name)
{
- return new $name($this->config, $this->db, $this->db_tools, $this->phpbb_root_path, $this->php_ext, $this->table_prefix);
+ $migration = new $name($this->config, $this->db, $this->db_tools, $this->phpbb_root_path, $this->php_ext, $this->table_prefix);
+
+ if ($migration instanceof ContainerAwareInterface)
+ {
+ $migration->setContainer($this->container);
+ }
+
+ return $migration;
}
/**
@@ -703,55 +835,26 @@ class migrator
}
/**
- * Load migration data files from a directory
- *
- * @param \phpbb\extension\finder $finder
- * @param string $path Path to migration data files
- * @param bool $check_fulfillable If TRUE (default), we will check
- * if all of the migrations are fulfillable after loading them.
- * If FALSE, we will not check. You SHOULD check at least once
- * to prevent errors (if including multiple directories, check
- * with the last call to prevent throwing errors unnecessarily).
- * @return array Array of migration names
+ * Creates the migrations table if it does not exist.
+ * @return null
*/
- public function load_migrations(\phpbb\extension\finder $finder, $path, $check_fulfillable = true)
+ public function create_migrations_table()
{
- if (!is_dir($path))
- {
- throw new \phpbb\db\migration\exception('DIRECTORY INVALID', $path);
- }
-
- $migrations = array();
-
- $files = $finder
- ->extension_directory("/")
- ->find_from_paths(array('/' => $path));
- foreach ($files as $file)
- {
- $migrations[$file['path'] . $file['filename']] = '';
- }
- $migrations = $finder->get_classes_from_files($migrations);
-
- foreach ($migrations as $migration)
+ // Make sure migrations have been installed.
+ if (!$this->db_tools->sql_table_exists($this->table_prefix . 'migrations'))
{
- if (!in_array($migration, $this->migrations))
- {
- $this->migrations[] = $migration;
- }
+ $this->db_tools->sql_create_table($this->table_prefix . 'migrations', array(
+ 'COLUMNS' => array(
+ 'migration_name' => array('VCHAR', ''),
+ 'migration_depends_on' => array('TEXT', ''),
+ 'migration_schema_done' => array('BOOL', 0),
+ 'migration_data_done' => array('BOOL', 0),
+ 'migration_data_state' => array('TEXT', ''),
+ 'migration_start_time' => array('TIMESTAMP', 0),
+ 'migration_end_time' => array('TIMESTAMP', 0),
+ ),
+ 'PRIMARY_KEY' => 'migration_name',
+ ));
}
-
- if ($check_fulfillable)
- {
- foreach ($this->migrations as $name)
- {
- $unfulfillable = $this->unfulfillable($name);
- if ($unfulfillable !== false)
- {
- throw new \phpbb\db\migration\exception('MIGRATION_NOT_FULFILLABLE', $name, $unfulfillable);
- }
- }
- }
-
- return $this->migrations;
}
}