Compare commits

..

4 commits

Author SHA1 Message Date
rxu
8d41478402
Merge 8e0ec1edd2 into 3cc7076513 2025-07-01 14:01:19 +00:00
rxu
8e0ec1edd2
[ticket/17525] Automatically handle index name prefixes
PHPBB-17525
2025-07-01 21:01:09 +07:00
rxu
e9157f4d10
[ticket/17525] Fix tests
PHPBB-17525
2025-07-01 16:53:29 +07:00
rxu
4274faa715
[ticket/17525] Provide Doctrine connection via dbtools
PHPBB-17525
2025-07-01 15:53:19 +07:00
6 changed files with 100 additions and 11 deletions

View file

@ -146,7 +146,7 @@ class table_helper
/** /**
* Generates short table names for the purpose of prefixing tables' index names. * Generates short table names for the purpose of prefixing tables' index names.
* *
* @param string $table_name Table name with prefix to generate its short name. * @param string $table_name Table name without prefix to generate its short name.
* *
* @return string Short table name. * @return string Short table name.
*/ */

View file

@ -90,7 +90,7 @@ class rename_duplicated_index_names extends migration
public function get_tables_index_names() public function get_tables_index_names()
{ {
$table_keys = []; $table_keys = [];
$schema_manager = $this->db_doctrine->createSchemaManager(); $schema_manager = $this->db_tools->get_connection()->createSchemaManager();
$table_names = $schema_manager->listTableNames(); $table_names = $schema_manager->listTableNames();
if (!empty($table_names)) if (!empty($table_names))

View file

@ -28,9 +28,6 @@ abstract class migration implements migration_interface
/** @var \phpbb\db\driver\driver_interface */ /** @var \phpbb\db\driver\driver_interface */
protected $db; protected $db;
/** @var \Doctrine\DBAL\Connection */
protected $db_doctrine;
/** @var \phpbb\db\tools\tools_interface */ /** @var \phpbb\db\tools\tools_interface */
protected $db_tools; protected $db_tools;
@ -75,9 +72,6 @@ abstract class migration implements migration_interface
$this->php_ext = $php_ext; $this->php_ext = $php_ext;
$this->errors = array(); $this->errors = array();
$phpbb_config_php_file = new \phpbb\config_php_file($phpbb_root_path, $php_ext);
$this->db_doctrine = \phpbb\db\doctrine\connection_factory::get_connection($phpbb_config_php_file);
} }
/** /**

View file

@ -61,6 +61,14 @@ class doctrine implements tools_interface
$this->connection = $connection; $this->connection = $connection;
} }
/**
* {@inheritDoc}
*/
public function get_connection(): Connection
{
return $this->connection;
}
/** /**
* @return AbstractSchemaManager * @return AbstractSchemaManager
* *
@ -160,6 +168,7 @@ class doctrine implements tools_interface
*/ */
public function sql_index_exists(string $table_name, string $index_name): bool 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)); return $this->asset_exists($index_name, $this->get_filtered_index_list($table_name, true));
} }
@ -168,6 +177,7 @@ class doctrine implements tools_interface
*/ */
public function sql_unique_index_exists(string $table_name, string $index_name): bool 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)); return $this->asset_exists($index_name, $this->get_filtered_index_list($table_name, false));
} }
@ -322,6 +332,7 @@ class doctrine implements tools_interface
*/ */
public function sql_create_index(string $table_name, string $index_name, $column) 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( return $this->alter_schema(
function (Schema $schema) use ($table_name, $index_name, $column): void function (Schema $schema) use ($table_name, $index_name, $column): void
{ {
@ -335,6 +346,8 @@ class doctrine implements tools_interface
*/ */
public function sql_rename_index(string $table_name, string $index_name_old, string $index_name_new) 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( return $this->alter_schema(
function (Schema $schema) use ($table_name, $index_name_old, $index_name_new): void function (Schema $schema) use ($table_name, $index_name_old, $index_name_new): void
{ {
@ -348,6 +361,7 @@ class doctrine implements tools_interface
*/ */
public function sql_index_drop(string $table_name, string $index_name) 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( return $this->alter_schema(
function (Schema $schema) use ($table_name, $index_name): void function (Schema $schema) use ($table_name, $index_name): void
{ {
@ -361,6 +375,7 @@ class doctrine implements tools_interface
*/ */
public function sql_create_unique_index(string $table_name, string $index_name, $column) 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( return $this->alter_schema(
function (Schema $schema) use ($table_name, $index_name, $column): void function (Schema $schema) use ($table_name, $index_name, $column): void
{ {
@ -397,6 +412,43 @@ class doctrine implements tools_interface
} }
} }
/**
* {@inheritDoc}
*/
public static function normalize_index_name(string $table_name, string $index_name, bool $remove_prefix = false): string
{
$short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name));
if (!self::is_prefixed($index_name, $short_table_name . '_'))
{
$index_name = $short_table_name . '_' . $index_name;
}
else if ($remove_prefix)
{
$index_name = remove_prefix($index_name);
}
return $index_name;
}
/**
* {@inheritDoc}
*/
public static function remove_prefix(string $name): string
{
$prefix_end_pos = strpos($name, '_');
$prefixless_name = substr($name, $prefix_end_pos + 1);
return $prefixless_name;
}
/**
* {@inheritDoc}
*/
public static function is_prefixed(string $name, string $prefix): bool
{
return strpos($name, $prefix) === 0;
}
/** /**
* Returns an array of indices for either unique and primary keys, or simple indices. * Returns an array of indices for either unique and primary keys, or simple indices.
* *
@ -840,6 +892,7 @@ class doctrine implements tools_interface
{ {
$columns = (is_array($column)) ? $column : [$column]; $columns = (is_array($column)) ? $column : [$column];
$table = $schema->getTable($table_name); $table = $schema->getTable($table_name);
$index_name = self::normalize_index_name($table_name, $index_name);
if ($safe_check && $table->hasIndex($index_name)) if ($safe_check && $table->hasIndex($index_name))
{ {
@ -861,6 +914,8 @@ 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 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); $table = $schema->getTable($table_name);
$index_name_old = self::normalize_index_name($table_name, $index_name_old);
$index_name_new = self::normalize_index_name($table_name, $index_name_new);
if ($safe_check && !$table->hasIndex($index_name_old)) if ($safe_check && !$table->hasIndex($index_name_old))
{ {
@ -883,6 +938,7 @@ class doctrine implements tools_interface
{ {
$columns = (is_array($column)) ? $column : [$column]; $columns = (is_array($column)) ? $column : [$column];
$table = $schema->getTable($table_name); $table = $schema->getTable($table_name);
$index_name = self::normalize_index_name($table_name, $index_name);
if ($safe_check && $table->hasIndex($index_name)) if ($safe_check && $table->hasIndex($index_name))
{ {
@ -903,6 +959,7 @@ class doctrine implements tools_interface
protected function schema_index_drop(Schema $schema, string $table_name, string $index_name, bool $safe_check = false): void protected function schema_index_drop(Schema $schema, string $table_name, string $index_name, bool $safe_check = false): void
{ {
$table = $schema->getTable($table_name); $table = $schema->getTable($table_name);
$index_name = self::normalize_index_name($table_name, $index_name);
if ($safe_check && !$table->hasIndex($index_name)) if ($safe_check && !$table->hasIndex($index_name))
{ {

View file

@ -226,4 +226,41 @@ interface tools_interface
* @return void * @return void
*/ */
public function sql_truncate_table(string $table_name): void; public function sql_truncate_table(string $table_name): void;
/**
* Gets current Doctrine DBAL connection
*
* @return \Doctrine\DBAL\Connection
*/
public function get_connection(): \Doctrine\DBAL\Connection;
/**
* Adds short table name prefix to the index name 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
*
* @return string Prefixed index name
*/
public static function normalize_index_name(string $table_name, string $index_name, bool $remove_prefix = false): string;
/**
* Removes prefix from string if exists
*
* @param string $name String to remove the prefix from
*
* @return string Prefixless string
*/
public static function remove_prefix(string $name): string;
/**
* Tests if a string is prefixed with the prefix defined
*
* @param string $name String to test vs prefix
* @param string $prefix Prefix name
*
* @return bool True if a string id prefixed with the prefix defined, false otherwise
*/
public static function is_prefixed(string $name, string $prefix): bool;
} }

View file

@ -485,9 +485,10 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case
$this->assertNotEmpty($table_keys); $this->assertNotEmpty($table_keys);
$short_table_names = \phpbb\db\doctrine\table_helper::map_short_table_names(['custom_table' => 'cstmtbl'], 'phpbb_'); $table_names = array_merge(array_keys($db_table_schema), ['phpbb_custom_table']);
$this->assertEquals('phpbb_custom_table', array_search('cstmtbl', $short_table_names)); $short_table_names = \phpbb\db\doctrine\table_helper::map_short_table_names($table_names, 'phpbb_');
$this->assertEquals($short_table_names['phpbb_custom_table'], 'cstmtbl'); $this->assertEquals('phpbb_custom_table', array_search(\phpbb\db\doctrine\table_helper::generate_shortname('custom_table'), $short_table_names));
$this->assertEquals($short_table_names['phpbb_custom_table'], \phpbb\db\doctrine\table_helper::generate_shortname('custom_table'));
foreach ($table_keys as $table_name => $key_names) foreach ($table_keys as $table_name => $key_names)
{ {