Skip to content

Commit

Permalink
[ticket/11880] Move get_schema_steps function to a migrator helper class
Browse files Browse the repository at this point in the history
PHPBB3-11880
  • Loading branch information
EXreaction committed Feb 5, 2014
1 parent 2c878ea commit 38eb6b0
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 72 deletions.
4 changes: 4 additions & 0 deletions phpBB/config/migrator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ services:
- %core.php_ext%
- %core.table_prefix%
- @migrator.tool_collection
- @migrator.helper

migrator.helper:
class: phpbb\db\migration\helper

migrator.tool_collection:
class: phpbb\di\service_collection
Expand Down
74 changes: 7 additions & 67 deletions phpBB/phpbb/db/migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class migrator
/** @var \phpbb\db\tools */
protected $db_tools;

/** @var \phpbb\db\migration\helper */
protected $helper;

/** @var string */
protected $table_prefix;

Expand Down Expand Up @@ -65,11 +68,12 @@ class migrator
/**
* 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)
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)
{
$this->config = $config;
$this->db = $db;
$this->db_tools = $db_tools;
$this->helper = $helper;

$this->migrations_table = $migrations_table;

Expand Down Expand Up @@ -232,7 +236,7 @@ protected function try_apply($name)
if (!$state['migration_schema_done'])
{
$this->last_run_migration['task'] = 'process_schema_step';
$steps = self::get_schema_steps($migration->update_schema());
$steps = $this->helper->get_schema_steps($migration->update_schema());
$result = $this->process_data_step($steps, $state['migration_data_state']);

$state['migration_data_state'] = ($result === true) ? '' : $result;
Expand Down Expand Up @@ -336,7 +340,7 @@ protected function try_revert($name)
}
else if ($state['migration_schema_done'])
{
$steps = self::get_schema_steps($migration->revert_schema());
$steps = $this->helper->get_schema_steps($migration->revert_schema());
$result = $this->process_data_step($steps, $state['migration_data_state']);

$state['migration_data_state'] = ($result === true) ? '' : $result;
Expand Down Expand Up @@ -750,68 +754,4 @@ public function load_migrations(\phpbb\extension\finder $finder, $path, $check_f

return $this->migrations;
}

/**
* Get the schema steps from an array of schema changes
*
* This splits up $schema_changes into individual changes so that the
* changes can be chunked
*
* @param array $schema_changes from migration
* @return array
*/
static public function get_schema_steps($schema_changes)
{
$steps = array();

// Nested level of data (only supports 1/2 currently)
$nested_level = array(
'drop_tables' => 1,
'add_tables' => 1,
'change_columns' => 2,
'add_columns' => 2,
'drop_keys' => 2,
'drop_columns' => 2,
'add_primary_keys' => 2, // perform_schema_changes only uses one level, but second is in the function
'add_unique_index' => 2,
'add_index' => 2,
);

foreach ($nested_level as $change_type => $data_depth)
{
if (!empty($schema_changes[$change_type]))
{
foreach ($schema_changes[$change_type] as $key => $value)
{
if ($data_depth === 1)
{
$steps[] = array(
'dbtools.perform_schema_changes', array(array(
$change_type => array(
$value,
),
)),
);
}
else if ($data_depth === 2)
{
foreach ($value as $key2 => $value2)
{
$steps[] = array(
'dbtools.perform_schema_changes', array(array(
$change_type => array(
$key => array(
$key2 => $value2,
),
),
)),
);
}
}
}
}
}

return $steps;
}
}
3 changes: 2 additions & 1 deletion tests/dbal/migrator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function setUp()
dirname(__FILE__) . '/../../phpBB/',
'php',
'phpbb_',
$tools
$tools,
new \phpbb\db\migration\helper()
);

$container = new phpbb_mock_container_builder();
Expand Down
3 changes: 2 additions & 1 deletion tests/extension/manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ protected function create_extension_manager($with_cache = true)
$phpbb_root_path,
$php_ext,
$table_prefix,
array()
array(),
new \phpbb\db\migration\helper()
);
$container = new phpbb_mock_container_builder();
$container->set('migrator', $migrator);
Expand Down
3 changes: 2 additions & 1 deletion tests/extension/metadata_manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ protected function setUp()
$this->phpbb_root_path,
'php',
$this->table_prefix,
array()
array(),
new \phpbb\db\migration\helper()
);
$container = new phpbb_mock_container_builder();
$container->set('migrator', $migrator);
Expand Down
9 changes: 8 additions & 1 deletion tests/migrator/get_schema_steps_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

class get_schema_steps_test extends phpbb_test_case
{
public function setUp()
{
parent::setUp();

$this->helper = new \phpbb\db\migration\helper();
}

public function schema_provider()
{
return array(
Expand Down Expand Up @@ -158,6 +165,6 @@ public function schema_provider()
*/
public function test_get_schema_steps($schema_changes, $expected)
{
$this->assertEquals($expected, \phpbb\db\migrator::get_schema_steps($schema_changes));
$this->assertEquals($expected, $this->helper->get_schema_steps($schema_changes));
}
}
3 changes: 2 additions & 1 deletion tests/test_framework/phpbb_functional_test_case.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ protected function get_extension_manager()
$phpbb_root_path,
$php_ext,
self::$config['table_prefix'],
array()
array(),
new \phpbb\db\migration\helper()
);
$container = new phpbb_mock_container_builder();
$container->set('migrator', $migrator);
Expand Down

0 comments on commit 38eb6b0

Please sign in to comment.