diff --git a/phpBB/phpbb/db/doctrine/table_helper.php b/phpBB/phpbb/db/doctrine/table_helper.php index 150e98d95b..ae80e23a50 100644 --- a/phpBB/phpbb/db/doctrine/table_helper.php +++ b/phpBB/phpbb/db/doctrine/table_helper.php @@ -146,7 +146,7 @@ class table_helper /** * 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. */ diff --git a/phpBB/phpbb/db/tools/doctrine.php b/phpBB/phpbb/db/tools/doctrine.php index a190e1f319..2c52575651 100644 --- a/phpBB/phpbb/db/tools/doctrine.php +++ b/phpBB/phpbb/db/tools/doctrine.php @@ -168,6 +168,7 @@ 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)); } @@ -176,6 +177,7 @@ 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)); } @@ -330,6 +332,7 @@ 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 { @@ -343,6 +346,8 @@ 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 { @@ -356,6 +361,7 @@ 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 { @@ -369,6 +375,7 @@ 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 { @@ -405,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. * @@ -848,6 +892,7 @@ 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); if ($safe_check && $table->hasIndex($index_name)) { @@ -869,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 { $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)) { @@ -891,6 +938,7 @@ 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); if ($safe_check && $table->hasIndex($index_name)) { @@ -911,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 { $table = $schema->getTable($table_name); + $index_name = self::normalize_index_name($table_name, $index_name); if ($safe_check && !$table->hasIndex($index_name)) { diff --git a/phpBB/phpbb/db/tools/tools_interface.php b/phpBB/phpbb/db/tools/tools_interface.php index e8c23a5109..a3fdae8004 100644 --- a/phpBB/phpbb/db/tools/tools_interface.php +++ b/phpBB/phpbb/db/tools/tools_interface.php @@ -233,4 +233,34 @@ interface tools_interface * @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; }