mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-11 22:08:54 +00:00
Merge branch '3.2.x'
* 3.2.x: [ticket/14831] Rename migration and replace preg_replace() with simpler methods [ticket/14831] Compare depends_on for migrations and remove prefixless names [ticket/14831] Add migration for deduplicating entries and fix typo [ticket/14831] Add method for getting valid migration name [ticket/14831] Fall back to possible migration names instead of adding prefix [ticket/14831] Make sure migrations always start with backslash
This commit is contained in:
commit
361ec09956
2 changed files with 113 additions and 0 deletions
|
@ -0,0 +1,77 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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\migration\data\v31x;
|
||||||
|
|
||||||
|
class remove_duplicate_migrations extends \phpbb\db\migration\migration
|
||||||
|
{
|
||||||
|
static public function depends_on()
|
||||||
|
{
|
||||||
|
return array('\phpbb\db\migration\data\v31x\v3110');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update_data()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('custom', array(array($this, 'deduplicate_entries'))),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deduplicate_entries()
|
||||||
|
{
|
||||||
|
$migration_state = array();
|
||||||
|
$duplicate_migrations = array();
|
||||||
|
|
||||||
|
$sql = "SELECT *
|
||||||
|
FROM " . $this->table_prefix . 'migrations';
|
||||||
|
$result = $this->db->sql_query($sql);
|
||||||
|
|
||||||
|
if (!$this->db->get_sql_error_triggered())
|
||||||
|
{
|
||||||
|
while ($migration = $this->db->sql_fetchrow($result))
|
||||||
|
{
|
||||||
|
$migration_state[$migration['migration_name']] = $migration;
|
||||||
|
|
||||||
|
$migration_state[$migration['migration_name']]['migration_depends_on'] = unserialize($migration['migration_depends_on']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->db->sql_freeresult($result);
|
||||||
|
|
||||||
|
foreach ($migration_state as $name => $migration)
|
||||||
|
{
|
||||||
|
$prepended_name = ($name[0] == '\\' ? '' : '\\') . $name;
|
||||||
|
$prefixless_name = $name[0] == '\\' ? substr($name, 1) : $name;
|
||||||
|
|
||||||
|
if ($prepended_name != $name && isset($migration_state[$prepended_name]) && $migration_state[$prepended_name]['migration_depends_on'] == $migration_state[$name]['migration_depends_on'])
|
||||||
|
{
|
||||||
|
$duplicate_migrations[] = $name;
|
||||||
|
unset($migration_state[$prepended_name]);
|
||||||
|
}
|
||||||
|
else if ($prefixless_name != $name && isset($migration_state[$prefixless_name]) && $migration_state[$prefixless_name]['migration_depends_on'] == $migration_state[$name]['migration_depends_on'])
|
||||||
|
{
|
||||||
|
$duplicate_migrations[] = $prefixless_name;
|
||||||
|
unset($migration_state[$prefixless_name]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($duplicate_migrations))
|
||||||
|
{
|
||||||
|
$sql = 'DELETE
|
||||||
|
FROM ' . $this->table_prefix . 'migrations
|
||||||
|
WHERE ' . $this->db->sql_in_set('migration_name', $duplicate_migrations);
|
||||||
|
$this->db->sql_query($sql);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -242,6 +242,34 @@ class migrator
|
||||||
$this->container->get('dispatcher')->enable();
|
$this->container->get('dispatcher')->enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a valid migration name from the migration state array in case the
|
||||||
|
* supplied name is not in the migration state list.
|
||||||
|
*
|
||||||
|
* @param string $name Migration name
|
||||||
|
* @return string Migration name
|
||||||
|
*/
|
||||||
|
protected function get_valid_name($name)
|
||||||
|
{
|
||||||
|
// Try falling back to a valid migration name with or without leading backslash
|
||||||
|
if (!isset($this->migration_state[$name]))
|
||||||
|
{
|
||||||
|
$prepended_name = ($name[0] == '\\' ? '' : '\\') . $name;
|
||||||
|
$prefixless_name = $name[0] == '\\' ? substr($name, 1) : $name;
|
||||||
|
|
||||||
|
if (isset($this->migration_state[$prepended_name]))
|
||||||
|
{
|
||||||
|
$name = $prepended_name;
|
||||||
|
}
|
||||||
|
else if (isset($this->migration_state[$prefixless_name]))
|
||||||
|
{
|
||||||
|
$name = $prefixless_name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $name;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Effectively runs a single update step from the next migration to be applied.
|
* Effectively runs a single update step from the next migration to be applied.
|
||||||
*
|
*
|
||||||
|
@ -251,6 +279,8 @@ class migrator
|
||||||
{
|
{
|
||||||
foreach ($this->migrations as $name)
|
foreach ($this->migrations as $name)
|
||||||
{
|
{
|
||||||
|
$name = $this->get_valid_name($name);
|
||||||
|
|
||||||
if (!isset($this->migration_state[$name]) ||
|
if (!isset($this->migration_state[$name]) ||
|
||||||
!$this->migration_state[$name]['migration_schema_done'] ||
|
!$this->migration_state[$name]['migration_schema_done'] ||
|
||||||
!$this->migration_state[$name]['migration_data_done'])
|
!$this->migration_state[$name]['migration_data_done'])
|
||||||
|
@ -306,6 +336,9 @@ class migrator
|
||||||
|
|
||||||
foreach ($state['migration_depends_on'] as $depend)
|
foreach ($state['migration_depends_on'] as $depend)
|
||||||
{
|
{
|
||||||
|
$depend = $this->get_valid_name($depend);
|
||||||
|
|
||||||
|
// Test all possible namings before throwing exception
|
||||||
if ($this->unfulfillable($depend) !== false)
|
if ($this->unfulfillable($depend) !== false)
|
||||||
{
|
{
|
||||||
throw new \phpbb\db\migration\exception('MIGRATION_NOT_FULFILLABLE', $name, $depend);
|
throw new \phpbb\db\migration\exception('MIGRATION_NOT_FULFILLABLE', $name, $depend);
|
||||||
|
@ -829,6 +862,8 @@ class migrator
|
||||||
*/
|
*/
|
||||||
public function unfulfillable($name)
|
public function unfulfillable($name)
|
||||||
{
|
{
|
||||||
|
$name = $this->get_valid_name($name);
|
||||||
|
|
||||||
if (isset($this->migration_state[$name]) || isset($this->fulfillable_migrations[$name]))
|
if (isset($this->migration_state[$name]) || isset($this->fulfillable_migrations[$name]))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
@ -844,6 +879,7 @@ class migrator
|
||||||
|
|
||||||
foreach ($depends as $depend)
|
foreach ($depends as $depend)
|
||||||
{
|
{
|
||||||
|
$depend = $this->get_valid_name($depend);
|
||||||
$unfulfillable = $this->unfulfillable($depend);
|
$unfulfillable = $this->unfulfillable($depend);
|
||||||
if ($unfulfillable !== false)
|
if ($unfulfillable !== false)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue