From 2c878ead311b8b71cabb4070ce848d5b84470ce8 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Mon, 3 Feb 2014 15:06:43 -0600 Subject: [ticket/11880] Break up schema changes in the migrator PHPBB3-11880 --- phpBB/phpbb/db/migrator.php | 107 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 86 insertions(+), 21 deletions(-) (limited to 'phpBB') diff --git a/phpBB/phpbb/db/migrator.php b/phpBB/phpbb/db/migrator.php index 8186493800..7dc113204e 100644 --- a/phpBB/phpbb/db/migrator.php +++ b/phpBB/phpbb/db/migrator.php @@ -83,6 +83,8 @@ class migrator $this->tools[$tool->get_name()] = $tool; } + $this->tools['dbtools'] = $this->db_tools; + $this->load_migration_state(); } @@ -229,9 +231,12 @@ class migrator if (!$state['migration_schema_done']) { - $this->last_run_migration['task'] = 'apply_schema_changes'; - $this->apply_schema_changes($migration->update_schema()); - $state['migration_schema_done'] = true; + $this->last_run_migration['task'] = 'process_schema_step'; + $steps = self::get_schema_steps($migration->update_schema()); + $result = $this->process_data_step($steps, $state['migration_data_state']); + + $state['migration_data_state'] = ($result === true) ? '' : $result; + $state['migration_schema_done'] = ($result === true); } else if (!$state['migration_data_done']) { @@ -329,32 +334,27 @@ class migrator $this->set_migration_state($name, $state); } - else + else if ($state['migration_schema_done']) { - $this->apply_schema_changes($migration->revert_schema()); + $steps = self::get_schema_steps($migration->revert_schema()); + $result = $this->process_data_step($steps, $state['migration_data_state']); - $sql = 'DELETE FROM ' . $this->migrations_table . " - WHERE migration_name = '" . $this->db->sql_escape($name) . "'"; - $this->db->sql_query($sql); + $state['migration_data_state'] = ($result === true) ? '' : $result; + $state['migration_schema_done'] = ($result === true) ? false : true; - unset($this->migration_state[$name]); + if (!$state['migration_schema_done']) + { + $sql = 'DELETE FROM ' . $this->migrations_table . " + WHERE migration_name = '" . $this->db->sql_escape($name) . "'"; + $this->db->sql_query($sql); + + unset($this->migration_state[$name]); + } } return true; } - /** - * Apply schema changes from a migration - * - * Just calls db_tools->perform_schema_changes - * - * @param array $schema_changes from migration - */ - protected function apply_schema_changes($schema_changes) - { - $this->db_tools->perform_schema_changes($schema_changes); - } - /** * Process the data step of the migration * @@ -498,6 +498,7 @@ class migrator return $this->get_callable_from_step($step); break; + case 'custom': if (!is_callable($parameters[0])) { @@ -749,4 +750,68 @@ class migrator 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; + } } -- cgit v1.2.1 From 38eb6b0e135293d51cc100937a6072b32e214c34 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 5 Feb 2014 09:57:36 -0600 Subject: [ticket/11880] Move get_schema_steps function to a migrator helper class PHPBB3-11880 --- phpBB/config/migrator.yml | 4 +++ phpBB/phpbb/db/migrator.php | 74 +++++---------------------------------------- 2 files changed, 11 insertions(+), 67 deletions(-) (limited to 'phpBB') diff --git a/phpBB/config/migrator.yml b/phpBB/config/migrator.yml index a94609418f..202421c09f 100644 --- a/phpBB/config/migrator.yml +++ b/phpBB/config/migrator.yml @@ -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 diff --git a/phpBB/phpbb/db/migrator.php b/phpBB/phpbb/db/migrator.php index 7dc113204e..4fb3f1a241 100644 --- a/phpBB/phpbb/db/migrator.php +++ b/phpBB/phpbb/db/migrator.php @@ -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; @@ -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; @@ -232,7 +236,7 @@ class migrator 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; @@ -336,7 +340,7 @@ class migrator } 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; @@ -750,68 +754,4 @@ class migrator 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; - } } -- cgit v1.2.1 From be28445b66c8c74f92a50b086fd6b472ecaa963b Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 5 Feb 2014 10:00:01 -0600 Subject: [ticket/11880] migration helper file PHPBB3-11880 --- phpBB/phpbb/db/migration/helper.php | 82 +++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 phpBB/phpbb/db/migration/helper.php (limited to 'phpBB') diff --git a/phpBB/phpbb/db/migration/helper.php b/phpBB/phpbb/db/migration/helper.php new file mode 100644 index 0000000000..28a6a56511 --- /dev/null +++ b/phpBB/phpbb/db/migration/helper.php @@ -0,0 +1,82 @@ + 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; + } +} -- cgit v1.2.1