diff options
Diffstat (limited to 'phpBB/includes/db/migrator.php')
-rw-r--r-- | phpBB/includes/db/migrator.php | 137 |
1 files changed, 61 insertions, 76 deletions
diff --git a/phpBB/includes/db/migrator.php b/phpBB/includes/db/migrator.php index 74f71775f3..7b5e8cb2de 100644 --- a/phpBB/includes/db/migrator.php +++ b/phpBB/includes/db/migrator.php @@ -31,6 +31,9 @@ class phpbb_db_migrator /** @var phpbb_db_tools */ protected $db_tools; + /** @var phpbb_extension_manager */ + protected $extension_manager; + /** @var string */ protected $table_prefix; @@ -91,6 +94,16 @@ class phpbb_db_migrator } /** + * Set Extension Manager (required) + * + * Not in constructor to prevent circular reference error + */ + public function set_extension_manager(phpbb_extension_manager $extension_manager) + { + $this->extension_manager = $extension_manager; + } + + /** * Loads all migrations and their application state from the database. * * @return null @@ -161,7 +174,7 @@ class phpbb_db_migrator 'migration_start_time' => time(), 'migration_end_time' => time(), ); - $this->insert_migration($name, $state); + $this->set_migration_state($name, $state); } } @@ -180,55 +193,32 @@ class phpbb_db_migrator * 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). - * @param bool $recursive Set to true to also load data files from subdirectories * @return array Array of migration names */ - public function load_migrations($path, $check_fulfillable = true, $recursive = true) + public function load_migrations($path, $check_fulfillable = true) { if (!is_dir($path)) { throw new phpbb_db_migration_exception('DIRECTORY INVALID', $path); } - $handle = opendir($path); - while (($file = readdir($handle)) !== false) - { - if ($file == '.' || $file == '..') - { - continue; - } + $migrations = array(); - // Recursion through subdirectories - if (is_dir($path . $file) && $recursive) - { - $this->load_migrations($path . $file . '/', $check_fulfillable, $recursive); - } + $finder = $this->extension_manager->get_finder(); + $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); - if (strpos($file, '_') !== 0 && strrpos($file, '.' . $this->php_ext) === (strlen($file) - strlen($this->php_ext) - 1)) + foreach ($migrations as $migration) + { + if (!in_array($migration, $this->migrations)) { - // We try to find what class existed by comparing the classes declared before and after including the file. - $declared_classes = get_declared_classes(); - - include ($path . $file); - - $added_classes = array_diff(get_declared_classes(), $declared_classes); - - if ( - // If two classes have been added and phpbb_db_migration is one of them, we've only added one real migration - !(sizeof($added_classes) == 2 && in_array('phpbb_db_migration', $added_classes)) && - // Otherwise there should only be one class added - sizeof($added_classes) != 1 - ) - { - throw new phpbb_db_migration_exception('MIGRATION DATA FILE INVALID', $path . $file); - } - - $name = array_pop($added_classes); - - if (!in_array($name, $this->migrations)) - { - $this->migrations[] = $name; - } + $this->migrations[] = $migration; } } @@ -316,23 +306,22 @@ class phpbb_db_migrator 'class' => $migration, ); - if ($migration->effectively_installed()) - { - $state = array( - 'migration_depends_on' => $migration->depends_on(), - 'migration_schema_done' => true, - 'migration_data_done' => true, - 'migration_data_state' => '', - 'migration_start_time' => 0, - 'migration_end_time' => 0, - ); - } - else + if (!isset($this->migration_state[$name])) { - if (!isset($this->migration_state[$name])) + if ($migration->effectively_installed()) + { + $state = array( + 'migration_depends_on' => $migration->depends_on(), + 'migration_schema_done' => true, + 'migration_data_done' => true, + 'migration_data_state' => '', + 'migration_start_time' => 0, + 'migration_end_time' => 0, + ); + } + else { $state['migration_start_time'] = time(); - $this->insert_migration($name, $state); } } @@ -361,14 +350,7 @@ class phpbb_db_migrator } } - $insert = $state; - $insert['migration_depends_on'] = serialize($state['migration_depends_on']); - $sql = 'UPDATE ' . $this->migrations_table . ' - SET ' . $this->db->sql_build_array('UPDATE', $insert) . " - WHERE migration_name = '" . $this->db->sql_escape($name) . "'"; - $this->db->sql_query($sql); - - $this->migration_state[$name] = $state; + $this->set_migration_state($name, $state); return true; } @@ -434,20 +416,13 @@ class phpbb_db_migrator } else { - $result = $this->process_data_step($migration->revert_data(), $state['migration_data_state'], false); + $result = $this->process_data_step($migration->revert_data(), '', false); $state['migration_data_state'] = ($result === true) ? '' : $result; $state['migration_data_done'] = ($result === true) ? false : true; } - $insert = $state; - $insert['migration_depends_on'] = serialize($state['migration_depends_on']); - $sql = 'UPDATE ' . $this->migrations_table . ' - SET ' . $this->db->sql_build_array('UPDATE', $insert) . " - WHERE migration_name = '" . $this->db->sql_escape($name) . "'"; - $this->db->sql_query($sql); - - $this->migration_state[$name] = $state; + $this->set_migration_state($name, $state); } else { @@ -660,21 +635,31 @@ class phpbb_db_migrator } /** - * Insert migration row into the database + * Insert/Update migration row into the database * * @param string $name Name of the migration * @param array $state * @return null */ - protected function insert_migration($name, $state) + protected function set_migration_state($name, $state) { $migration_row = $state; - $migration_row['migration_name'] = $name; $migration_row['migration_depends_on'] = serialize($state['migration_depends_on']); - $sql = 'INSERT INTO ' . $this->migrations_table . ' - ' . $this->db->sql_build_array('INSERT', $migration_row); - $this->db->sql_query($sql); + if (isset($this->migration_state[$name])) + { + $sql = 'UPDATE ' . $this->migrations_table . ' + SET ' . $this->db->sql_build_array('UPDATE', $migration_row) . " + WHERE migration_name = '" . $this->db->sql_escape($name) . "'"; + $this->db->sql_query($sql); + } + else + { + $migration_row['migration_name'] = $name; + $sql = 'INSERT INTO ' . $this->migrations_table . ' + ' . $this->db->sql_build_array('INSERT', $migration_row); + $this->db->sql_query($sql); + } $this->migration_state[$name] = $state; } |