1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-15 21:24:03 +02:00

[ticket/17525] Fix handling index name prefix logic for renaming

PHPBB-17525
This commit is contained in:
rxu
2025-07-04 00:08:28 +07:00
parent b5c3befa87
commit 5eaabb1c39
24 changed files with 149 additions and 84 deletions

View File

@@ -58,6 +58,7 @@ $db_doctrine = $ref->newInstanceWithoutConstructor();
$factory = new \phpbb\db\tools\factory();
$db_tools = $factory->get($db_doctrine, true);
$db_tools->set_table_prefix($table_prefix);
$tables_data = \Symfony\Component\Yaml\Yaml::parseFile($phpbb_root_path . '/config/default/container/tables.yml');
$tables = [];

View File

@@ -13,6 +13,7 @@
namespace phpbb\db\doctrine;
use InvalidArgumentException;
use phpbb\db\tools\doctrine as doctrine_dbtools;
class table_helper
{
@@ -137,7 +138,7 @@ class table_helper
$short_table_names_map = [];
foreach ($table_names as $table_name)
{
$short_table_names_map[$table_name] = self::generate_shortname(str_replace($table_prefix, '', $table_name));
$short_table_names_map[$table_name] = self::generate_shortname(doctrine_dbtools::remove_prefix($table_name, $table_prefix));
}
return $short_table_names_map;

View File

@@ -15,9 +15,15 @@ namespace phpbb\db\migration\data\v400;
use phpbb\db\migration\migration;
use phpbb\db\doctrine\table_helper;
use phpbb\db\tools\doctrine as doctrine_dbtools;
class rename_duplicated_index_names extends migration
{
/**
* @var array
*/
protected $table_keys = [];
public static function depends_on()
{
return [
@@ -28,24 +34,33 @@ class rename_duplicated_index_names extends migration
public function update_schema()
{
$rename_index = [];
$is_prefixed_index = false;
$tables_index_names = $this->get_tables_index_names();
$short_table_names = table_helper::map_short_table_names(array_keys($tables_index_names), $this->table_prefix);
foreach ($tables_index_names as $table_name => $key_names)
if (empty($this->table_keys))
{
$this->get_tables_index_names();
}
$short_table_names = table_helper::map_short_table_names(array_keys($this->table_keys), $this->table_prefix);
foreach ($this->table_keys as $table_name => $key_names)
{
$prefixless_table_name = doctrine_dbtools::remove_prefix($table_name, $this->table_prefix);
foreach ($key_names as $key_name)
{
$prefixless_table_name = strpos($table_name, $this->table_prefix) === 0 ? substr($table_name, strlen($this->table_prefix)) : $table_name;
// Check if there's at least one index name is prefixed, otherwise we operate on generated database schema
$is_prefixed_index = $is_prefixed_index || (strpos($key_name, $table_name) === 0);
// If key name is prefixed by its table name (with or without tables prefix), remove that key name prefix.
$cleaned_key_name = !$is_prefixed_index ? $key_name : str_replace(strpos($key_name, $table_name) === 0 ? $table_name . '_' : $prefixless_table_name . '_', '', $key_name);
// If 'old' key name is already new format, do not rename it
if (doctrine_dbtools::is_prefixed($key_name, $short_table_names[$table_name]))
{
continue;
}
// If 'old' key name is prefixed by its table name with and/or without table name common prefix
// (f.e. 'phpbb_log_log_time'), remove it to prefix with the relevant table's short name
$cleaned_key_name = $key_name;
foreach ([$table_name, $prefixless_table_name] as $prefix)
{
$cleaned_key_name = doctrine_dbtools::remove_prefix($cleaned_key_name, $prefix);
}
$key_name_new = $short_table_names[$table_name] . '_' . $cleaned_key_name;
$rename_index[$table_name][$key_name !== $cleaned_key_name ? $key_name : $cleaned_key_name] = $key_name_new;
$rename_index[$table_name][$key_name] = $key_name_new;
}
}
@@ -89,7 +104,6 @@ class rename_duplicated_index_names extends migration
public function get_tables_index_names()
{
$table_keys = [];
$schema_manager = $this->db_tools->get_connection()->createSchemaManager();
$table_names = $schema_manager->listTableNames();
@@ -98,32 +112,28 @@ class rename_duplicated_index_names extends migration
foreach ($table_names as $table_name)
{
$indices = $schema_manager->listTableIndexes($table_name);
$index_names = array_keys(
array_filter($indices, function (\Doctrine\DBAL\Schema\Index $index)
$indices = array_keys(array_filter($indices,
function (\Doctrine\DBAL\Schema\Index $index)
{
return !$index->isPrimary();
})
);
if (!empty($index_names))
if (!empty($indices))
{
$table_keys[$table_name] = $index_names;
$this->table_keys[$table_name] = $indices;
}
}
}
else
{
$db_table_schema = $this->get_schema();
foreach ($db_table_schema as $table_name => $table_data)
foreach ($this->get_schema() as $table_name => $table_data)
{
if (isset($table_data['KEYS']))
{
$table_keys[$table_name] = array_keys($table_data['KEYS']);
$this->table_keys[$table_name] = array_keys($table_data['KEYS']);
}
}
}
return $table_keys;
}
}

View File

@@ -49,6 +49,11 @@ class doctrine implements tools_interface
*/
private $return_statements;
/**
* @var string
*/
private $table_prefix;
/**
* Database tools constructors.
*
@@ -94,6 +99,14 @@ class doctrine implements tools_interface
return $this->get_schema_manager()->createSchema();
}
/**
* {@inheritDoc}
*/
public function set_table_prefix($table_prefix): void
{
$this->table_prefix = $table_prefix;
}
/**
* {@inheritDoc}
*/
@@ -168,7 +181,6 @@ class doctrine implements tools_interface
*/
public function sql_index_exists(string $table_name, string $index_name): bool
{
$index_name = self::normalize_index_name($table_name, $index_name);
return $this->asset_exists($index_name, $this->get_filtered_index_list($table_name, true));
}
@@ -177,7 +189,6 @@ class doctrine implements tools_interface
*/
public function sql_unique_index_exists(string $table_name, string $index_name): bool
{
$index_name = self::normalize_index_name($table_name, $index_name);
return $this->asset_exists($index_name, $this->get_filtered_index_list($table_name, false));
}
@@ -332,7 +343,6 @@ class doctrine implements tools_interface
*/
public function sql_create_index(string $table_name, string $index_name, $column)
{
$index_name = self::normalize_index_name($table_name, $index_name);
return $this->alter_schema(
function (Schema $schema) use ($table_name, $index_name, $column): void
{
@@ -346,8 +356,6 @@ class doctrine implements tools_interface
*/
public function sql_rename_index(string $table_name, string $index_name_old, string $index_name_new)
{
$index_name_old = self::normalize_index_name($table_name, $index_name_old);
$index_name_new = self::normalize_index_name($table_name, $index_name_new);
return $this->alter_schema(
function (Schema $schema) use ($table_name, $index_name_old, $index_name_new): void
{
@@ -361,7 +369,6 @@ class doctrine implements tools_interface
*/
public function sql_index_drop(string $table_name, string $index_name)
{
$index_name = self::normalize_index_name($table_name, $index_name);
return $this->alter_schema(
function (Schema $schema) use ($table_name, $index_name): void
{
@@ -375,7 +382,6 @@ class doctrine implements tools_interface
*/
public function sql_create_unique_index(string $table_name, string $index_name, $column)
{
$index_name = self::normalize_index_name($table_name, $index_name);
return $this->alter_schema(
function (Schema $schema) use ($table_name, $index_name, $column): void
{
@@ -415,34 +421,18 @@ class doctrine implements tools_interface
/**
* {@inheritDoc}
*/
public static function normalize_index_name(string $table_name, string $index_name, bool $remove_prefix = false): string
public static function add_prefix(string $name, string $prefix): string
{
$prefixless_table_name = self::remove_prefix($table_name);
$short_table_name = table_helper::generate_shortname($prefixless_table_name);
if (!self::is_prefixed($index_name, $short_table_name . '_')
&& !self::is_prefixed($index_name, $prefixless_table_name . '_')
&& !self::is_prefixed($index_name, $table_name . '_')
)
{
$index_name = $short_table_name . '_' . $index_name;
}
else if ($remove_prefix)
{
$index_name = self::remove_prefix($index_name);
}
return $index_name;
return strpos($prefix, '_', -1) ? $prefix . $name : $prefix . '_' . $name;
}
/**
* {@inheritDoc}
*/
public static function remove_prefix(string $name): string
public static function remove_prefix(string $name, string $prefix = ''): string
{
$prefix_end_pos = strpos($name, '_');
$prefixless_name = substr($name, $prefix_end_pos + 1);
return $prefixless_name;
$prefix = strpos($prefix, '_', -1) ? $prefix : $prefix . '_';
return $prefix && self::is_prefixed($name, $prefix) ? substr($name, strlen($prefix)) : $name;
}
/**
@@ -676,6 +666,7 @@ class doctrine implements tools_interface
}
$table = $schema->createTable($table_name);
$short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix));
$dbms_name = $this->get_schema_manager()->getDatabasePlatform()->getName();
foreach ($table_data['COLUMNS'] as $column_name => $column_data)
@@ -701,7 +692,7 @@ class doctrine implements tools_interface
foreach ($table_data['KEYS'] as $key_name => $key_data)
{
$columns = (is_array($key_data[1])) ? $key_data[1] : [$key_data[1]];
$key_name = self::normalize_index_name($table_name, $key_name);
$key_name = !self::is_prefixed($key_name, $short_table_name) ? self::add_prefix($key_name, $short_table_name) : $key_name;
// Supports key columns defined with there length
$columns = array_map(function (string $column)
@@ -897,7 +888,8 @@ class doctrine implements tools_interface
{
$columns = (is_array($column)) ? $column : [$column];
$table = $schema->getTable($table_name);
$index_name = self::normalize_index_name($table_name, $index_name);
$short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix));
$index_name = !self::is_prefixed($index_name, $short_table_name) ? self::add_prefix($index_name, $short_table_name) : $index_name;
if ($safe_check && $table->hasIndex($index_name))
{
@@ -919,7 +911,13 @@ class doctrine implements tools_interface
protected function schema_rename_index(Schema $schema, string $table_name, string $index_name_old, string $index_name_new, bool $safe_check = false): void
{
$table = $schema->getTable($table_name);
$index_name_new = self::normalize_index_name($table_name, $index_name_new);
$short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix));
if (!$table->hasIndex($index_name_old))
{
$index_name_old = !self::is_prefixed($index_name_old, $short_table_name) ? self::add_prefix($index_name_old, $short_table_name) : self::remove_prefix($index_name_old, $short_table_name);
}
$index_name_new = !self::is_prefixed($index_name_new, $short_table_name) ? self::add_prefix($index_name_new, $short_table_name) : $index_name_new;
if ($safe_check && !$table->hasIndex($index_name_old))
{
@@ -942,7 +940,8 @@ class doctrine implements tools_interface
{
$columns = (is_array($column)) ? $column : [$column];
$table = $schema->getTable($table_name);
$index_name = self::normalize_index_name($table_name, $index_name);
$short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix));
$index_name = !self::is_prefixed($index_name, $short_table_name) ? self::add_prefix($index_name, $short_table_name) : $index_name;
if ($safe_check && $table->hasIndex($index_name))
{
@@ -963,7 +962,12 @@ class doctrine implements tools_interface
protected function schema_index_drop(Schema $schema, string $table_name, string $index_name, bool $safe_check = false): void
{
$table = $schema->getTable($table_name);
$index_name = self::normalize_index_name($table_name, $index_name);
$short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix));
if (!$table->hasIndex($index_name))
{
$index_name = !self::is_prefixed($index_name, $short_table_name) ? self::add_prefix($index_name, $short_table_name) : self::remove_prefix($index_name, $short_table_name);
}
if ($safe_check && !$table->hasIndex($index_name))
{

View File

@@ -235,24 +235,25 @@ interface tools_interface
public function get_connection(): \Doctrine\DBAL\Connection;
/**
* Adds short table name prefix to the index name if needed
* Adds prefix to a string if needed
*
* @param string $table_name Table name with tables prefix
* @param string $index_name Index name
* @param bool $remove_prefix Flag indicating to remove short table name prefix if exists
* @param string $name Table name with tables prefix
* @param string $prefix Prefix to add
*
* @return string Prefixed index name
* @return string Prefixed name
*/
public static function normalize_index_name(string $table_name, string $index_name, bool $remove_prefix = false): string;
public static function add_prefix(string $name, string $prefix): string;
/**
* Removes prefix from string if exists
* Removes prefix from string if exists. If prefix is empty,
* the first part of the string ending with underscore will be removed.
*
* @param string $name String to remove the prefix from
* @param string $name String to remove the prefix from
* @param string $prefix Prefix to remove
*
* @return string Prefixless string
*/
public static function remove_prefix(string $name): string;
public static function remove_prefix(string $name, string $prefix = ''): string;
/**
* Tests if a string is prefixed with the prefix defined
@@ -263,4 +264,13 @@ interface tools_interface
* @return bool True if a string id prefixed with the prefix defined, false otherwise
*/
public static function is_prefixed(string $name, string $prefix): bool;
/**
* Sets table prefix
*
* @param string $table_prefix String to test vs prefix
*
* @return void
*/
public function set_table_prefix(string $table_prefix): void;
}

View File

@@ -393,6 +393,7 @@ class database
$doctrine_db = connection_factory::get_connection_from_params($dbms, $dbhost, $dbuser, $dbpass, $dbname, $dbport);
$db_tools_factory = new \phpbb\db\tools\factory();
$db_tools = $db_tools_factory->get($doctrine_db);
$db_tools->set_table_prefix($table_prefix);
$tables = $db_tools->sql_list_tables();
$tables = array_map('strtolower', $tables);
$table_intersect = array_intersect($tables, $table_ary);

View File

@@ -98,6 +98,7 @@ class add_tables extends task_base
$this->schema_file_path = $phpbb_root_path . 'store/schema.json';
$this->table_prefix = $this->config->get('table_prefix');
$this->change_prefix = $this->config->get('change_table_prefix', true);
$this->db_tools->set_table_prefix($this->table_prefix);
parent::__construct(true);
}

View File

@@ -145,6 +145,7 @@ class create_schema_file extends \phpbb\install\task_base
$migrator_classes = $finder->core_path('phpbb/db/migration/data/')->get_classes();
$factory = new \phpbb\db\tools\factory();
$db_tools = $factory->get($this->db_doctrine, true);
$db_tools->set_table_prefix($table_prefix);
$tables_data = \Symfony\Component\Yaml\Yaml::parseFile($this->phpbb_root_path . '/config/default/container/tables.yml');
$tables = [];
foreach ($tables_data['parameters'] as $parameter => $table)