From 8d1f8af7c65d0a7a4645dad763c4e48a92d0ef82 Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 15 Jun 2025 13:03:55 +0700 Subject: [PATCH 01/14] [ticket/17525] Correctly handle Doctrine DB tools exceptions, enrich error info PHPBB-17525 --- phpBB/includes/acp/acp_extensions.php | 6 ++++ phpBB/includes/functions.php | 2 +- phpBB/install/startup.php | 2 +- phpBB/phpbb/db/tools/doctrine.php | 40 +++++++++++---------------- phpBB/phpbb/install/installer.php | 4 ++- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/phpBB/includes/acp/acp_extensions.php b/phpBB/includes/acp/acp_extensions.php index e08c2e341b..626f1b09c1 100644 --- a/phpBB/includes/acp/acp_extensions.php +++ b/phpBB/includes/acp/acp_extensions.php @@ -276,6 +276,12 @@ class acp_extensions { $this->template->assign_var('MIGRATOR_ERROR', $e->getLocalisedMessage($this->user)); } + catch (\Exception $e) + { + $stack_trace = phpbb_filter_root_path(str_replace("\n", '
', $e->getTraceAsString())); + $message = $e->getMessage(); + $this->template->assign_var('MIGRATOR_ERROR', '' . $message . '

' . $stack_trace); + } $this->tpl_name = 'acp_ext_enable'; diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index f725d5f996..72241b52f0 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2716,7 +2716,7 @@ function get_backtrace() // Only show function arguments for include etc. // Other parameters may contain sensible information $argument = ''; - if (!empty($trace['args'][0]) && in_array($trace['function'], array('include', 'require', 'include_once', 'require_once'))) + if (!empty($trace['args'][0]) && in_array($trace['function'], array('include', 'require', 'include_once', 'require_once', 'try_apply'))) { $argument = htmlspecialchars(phpbb_filter_root_path($trace['args'][0]), ENT_COMPAT); } diff --git a/phpBB/install/startup.php b/phpBB/install/startup.php index b8ceb37342..0cff634795 100644 --- a/phpBB/install/startup.php +++ b/phpBB/install/startup.php @@ -107,7 +107,7 @@ function installer_msg_handler($errno, $msg_text, $errfile, $errline): bool { /** @var \phpbb\install\helper\iohandler\iohandler_interface $iohandler */ $iohandler = $phpbb_installer_container->get('installer.helper.iohandler'); - $iohandler->add_error_message($msg); + $iohandler->add_error_message($msg, get_backtrace()); $iohandler->send_response(true); exit(); } diff --git a/phpBB/phpbb/db/tools/doctrine.php b/phpBB/phpbb/db/tools/doctrine.php index aaf44aed68..65fd6b39aa 100644 --- a/phpBB/phpbb/db/tools/doctrine.php +++ b/phpBB/phpbb/db/tools/doctrine.php @@ -458,34 +458,26 @@ class doctrine implements tools_interface */ protected function alter_schema(callable $callback) { - try + $current_schema = $this->get_schema(); + $new_schema = clone $current_schema; + call_user_func($callback, $new_schema); + + $comparator = new comparator(); + $schemaDiff = $comparator->compareSchemas($current_schema, $new_schema); + $queries = $schemaDiff->toSql($this->get_schema_manager()->getDatabasePlatform()); + + if ($this->return_statements) { - $current_schema = $this->get_schema(); - $new_schema = clone $current_schema; - call_user_func($callback, $new_schema); - - $comparator = new comparator(); - $schemaDiff = $comparator->compareSchemas($current_schema, $new_schema); - $queries = $schemaDiff->toSql($this->get_schema_manager()->getDatabasePlatform()); - - if ($this->return_statements) - { - return $queries; - } - - foreach ($queries as $query) - { - // executeQuery() must be used here because $query might return a result set, for instance REPAIR does - $this->connection->executeQuery($query); - } - - return true; + return $queries; } - catch (Exception $e) + + foreach ($queries as $query) { - // @todo: check if it makes sense to properly handle the exception - return [$e->getMessage()]; + // executeQuery() must be used here because $query might return a result set, for instance REPAIR does + $this->connection->executeQuery($query); } + + return true; } /** diff --git a/phpBB/phpbb/install/installer.php b/phpBB/phpbb/install/installer.php index 23ab5df919..3664b39f2d 100644 --- a/phpBB/phpbb/install/installer.php +++ b/phpBB/phpbb/install/installer.php @@ -280,7 +280,9 @@ class installer } catch (\Exception $e) { - $this->iohandler->add_error_message($e->getMessage()); + $stack_trace = phpbb_filter_root_path(str_replace("\n", '
', $e->getTraceAsString())); + $message = $e->getMessage(); + $this->iohandler->add_error_message($message, $stack_trace); $this->iohandler->send_response(true); $fail_cleanup = true; } From 0e0214a71da29a33e83de466245166ff4942fd27 Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 15 Jun 2025 15:51:59 +0700 Subject: [PATCH 02/14] [ticket/17525] Avoid index name duplication (auth_role_id) phpbb_acl_groups and phpbb_acl_users have indexes with the same names (auth_role_id) which can cause issues on SQLite/Postgres PHPBB-17525 --- .../data/v400/rename_auth_role_id_index.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php diff --git a/phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php b/phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php new file mode 100644 index 0000000000..2b716583be --- /dev/null +++ b/phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php @@ -0,0 +1,58 @@ + + * @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\v400; + +use phpbb\db\migration\migration; + +class rename_auth_role_id_index extends migration +{ + public static function depends_on() + { + return [ + '\phpbb\db\migration\data\v400\dev', + ]; + } + + public function update_schema() + { + return [ + 'drop_keys' => [ + $this->table_prefix . 'acl_users' => [ + 'auth_role_id', + ], + ], + 'add_index' => [ + $this->table_prefix . 'acl_users' => [ + 'usr_auth_role_id' => ['auth_role_id'], + ], + ], + ]; + } + + public function revert_schema() + { + return [ + 'drop_keys' => [ + $this->table_prefix . 'acl_users' => [ + 'usr_auth_role_id', + ], + ], + 'add_index' => [ + $this->table_prefix . 'acl_users' => [ + 'auth_role_id' => ['auth_role_id'], + ], + ], + ]; + } +} From 7b0b95250c56171bca3a7d490b81a2f265a0e36b Mon Sep 17 00:00:00 2001 From: rxu Date: Tue, 17 Jun 2025 00:46:17 +0700 Subject: [PATCH 03/14] [ticket/17525] Avoid more index name duplication Many more tables have indexes with the same names which can cause issues on SQLite/Postgres. Also, add an option for migrations to rename indexes. PHPBB-17525 --- .../data/v400/rename_auth_role_id_index.php | 90 +++++++++++++++---- phpBB/phpbb/db/migration/helper.php | 1 + phpBB/phpbb/db/migration/schema_generator.php | 21 ++++- phpBB/phpbb/db/tools/doctrine.php | 39 ++++++++ phpBB/phpbb/db/tools/tools_interface.php | 11 +++ 5 files changed, 143 insertions(+), 19 deletions(-) diff --git a/phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php b/phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php index 2b716583be..9a3fd790f7 100644 --- a/phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php +++ b/phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php @@ -27,14 +27,74 @@ class rename_auth_role_id_index extends migration public function update_schema() { return [ - 'drop_keys' => [ - $this->table_prefix . 'acl_users' => [ - 'auth_role_id', + 'rename_index' => [ + $this->table_prefix . 'acl_groups' => [ + 'auth_role_id' => 'aclgrps_auth_role_id', + 'group_id' => 'aclgrps_group_id', ], - ], - 'add_index' => [ $this->table_prefix . 'acl_users' => [ - 'usr_auth_role_id' => ['auth_role_id'], + 'auth_role_id' => 'aclusrs_auth_role_id', + 'user_id' => 'aclusrs_user_id', + ], + $this->table_prefix . 'attachments' => [ + 'poster_id' => 'attchmnts_poster_id', + 'topic_id' => 'attchmnts_topic_id', + ], + $this->table_prefix . 'forums_watch' => [ + 'forum_id' => 'frmswtch_forum_id', + 'user_id' => 'frmswtch_user_id', + ], + $this->table_prefix . 'log' => [ + 'forum_id' => 'log_forum_id', + 'topic_id' => 'log_topic_id', + 'user_id' => 'log_user_id', + ], + $this->table_prefix . 'login_attempts' => [ + 'user_id' => 'lgnatmpts_user_id', + ], + $this->table_prefix . 'moderator_cache' => [ + 'forum_id' => 'mdrtrcch_forum_id', + ], + $this->table_prefix . 'oauth_states' => [ + 'user_id' => 'oauthsts_user_id', + ], + $this->table_prefix . 'oauth_tokens' => [ + 'user_id' => 'oauthtkns_user_id', + ], + $this->table_prefix . 'poll_options' => [ + 'topic_id' => 'pllopts_topic_id', + ], + $this->table_prefix . 'poll_votes' => [ + 'topic_id' => 'pllvts_topic_id', + ], + $this->table_prefix . 'posts' => [ + 'forum_id' => 'psts_forum_id', + 'poster_id' => 'psts_poster_id', + 'topic_id' => 'psts_topic_id', + ], + $this->table_prefix . 'privmsgs_folder' => [ + 'user_id' => 'pmfldr_user_id', + ], + $this->table_prefix . 'privmsgs_rules' => [ + 'user_id' => 'pmrls_user_id', + ], + $this->table_prefix . 'topics' => [ + 'forum_id' => 'tpcs_forum_id', + ], + $this->table_prefix . 'topics_track' => [ + 'forum_id' => 'tpcstrk_forum_id', + 'topic_id' => 'tpcstrk_topic_id', + ], + $this->table_prefix . 'topics_watch' => [ + 'topic_id' => 'tpcswtch_topic_id', + 'user_id' => 'tpcswtch_user_id', + ], + $this->table_prefix . 'user_group' => [ + 'group_id' => 'usrgrp_group_id', + 'user_id' => 'usrgrp_user_id', + ], + $this->table_prefix . 'user_notifications' => [ + 'user_id' => 'usrntf_user_id', ], ], ]; @@ -42,17 +102,11 @@ class rename_auth_role_id_index extends migration public function revert_schema() { - return [ - 'drop_keys' => [ - $this->table_prefix . 'acl_users' => [ - 'usr_auth_role_id', - ], - ], - 'add_index' => [ - $this->table_prefix . 'acl_users' => [ - 'auth_role_id' => ['auth_role_id'], - ], - ], - ]; + $schema = $this->update_schema(); + array_walk($schema['rename_index'], function (&$index_data, $table_name) { + $index_data = array_flip($index_data); + }); + + return $schema; } } diff --git a/phpBB/phpbb/db/migration/helper.php b/phpBB/phpbb/db/migration/helper.php index bce2efff51..44cd23462a 100644 --- a/phpBB/phpbb/db/migration/helper.php +++ b/phpBB/phpbb/db/migration/helper.php @@ -42,6 +42,7 @@ class helper 'add_primary_keys' => 2, // perform_schema_changes only uses one level, but second is in the function 'add_unique_index' => 2, 'add_index' => 2, + 'rename_index' => 1, ); foreach ($nested_level as $change_type => $data_depth) diff --git a/phpBB/phpbb/db/migration/schema_generator.php b/phpBB/phpbb/db/migration/schema_generator.php index ce07c30d45..90cfddbe5a 100644 --- a/phpBB/phpbb/db/migration/schema_generator.php +++ b/phpBB/phpbb/db/migration/schema_generator.php @@ -187,6 +187,7 @@ class schema_generator 'add_index' => 'KEYS', 'add_unique_index' => 'KEYS', 'drop_keys' => 'KEYS', + 'rename_index' => 'KEYS', ]; $schema_changes = $migration->update_schema(); @@ -206,6 +207,7 @@ class schema_generator { case 'add': case 'change': + case 'rename': $action = function(&$value, $changes, $value_transform = null) { self::set_all($value, $changes, $value_transform); }; @@ -347,7 +349,7 @@ class schema_generator */ private static function get_value_transform(string $change_type, string $schema_type) : Closure|null { - if ($change_type !== 'add') + if (!in_array($change_type, ['add', 'rename'])) { return null; } @@ -355,6 +357,23 @@ class schema_generator switch ($schema_type) { case 'index': + if ($change_type == 'rename') + { + return function(&$value, $key, $change) { + if (isset($value[$key])) + { + $data_backup = $value[$key]; + unset($value[$key]); + $value[$change] = $data_backup; + unset($data_backup); + } + else + { + return null; + } + }; + } + return function(&$value, $key, $change) { $value[$key] = ['INDEX', $change]; }; diff --git a/phpBB/phpbb/db/tools/doctrine.php b/phpBB/phpbb/db/tools/doctrine.php index 65fd6b39aa..73987d26ee 100644 --- a/phpBB/phpbb/db/tools/doctrine.php +++ b/phpBB/phpbb/db/tools/doctrine.php @@ -330,6 +330,19 @@ class doctrine implements tools_interface ); } + /** + * {@inheritDoc} + */ + public function sql_rename_index(string $table_name, string $index_name_old, string $index_name_new) + { + return $this->alter_schema( + function (Schema $schema) use ($table_name, $index_name_old, $index_name_new): void + { + $this->schema_rename_index($schema, $table_name, $index_name_old, $index_name_new); + } + ); + } + /** * {@inheritDoc} */ @@ -545,6 +558,11 @@ class doctrine implements tools_interface 'use_key' => true, 'per_table' => true, ], + 'rename_index' => [ + 'method' => 'schema_rename_index', + 'use_key' => true, + 'per_table' => true, + ], ]; foreach ($mapping as $action => $params) @@ -831,6 +849,27 @@ class doctrine implements tools_interface $table->addIndex($columns, $index_name); } + /** + * @param Schema $schema + * @param string $table_name + * @param string $index_name_old + * @param string $index_name_new + * @param bool $safe_check + * + * @throws SchemaException + */ + 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); + + if ($safe_check && !$table->hasIndex($index_name_old)) + { + return; + } + + $table->renameIndex($index_name_old, $index_name_new); + } + /** * @param Schema $schema * @param string $table_name diff --git a/phpBB/phpbb/db/tools/tools_interface.php b/phpBB/phpbb/db/tools/tools_interface.php index 0b8cefd8dd..09f47ed566 100644 --- a/phpBB/phpbb/db/tools/tools_interface.php +++ b/phpBB/phpbb/db/tools/tools_interface.php @@ -165,6 +165,17 @@ interface tools_interface */ public function sql_create_index(string $table_name, string $index_name, $column); + /** + * Rename index + * + * @param string $table_name Table to modify + * @param string $index_name_old Name of the index to rename + * @param string $index_name_new New name of the index being renamed + * + * @return bool|string[] True if the statements have been executed + */ + public function sql_rename_index(string $table_name, string $index_name_old, string $index_name_new); + /** * Drop Index * From 78dcb0c86207b182d8abd517880e5057ca3a3f85 Mon Sep 17 00:00:00 2001 From: rxu Date: Tue, 17 Jun 2025 01:01:19 +0700 Subject: [PATCH 04/14] [ticket/17525] Avoid more index name duplication PHPBB-17525 --- .../data/v400/rename_auth_role_id_index.php | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php b/phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php index 9a3fd790f7..9537c884dd 100644 --- a/phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php +++ b/phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php @@ -40,8 +40,15 @@ class rename_auth_role_id_index extends migration 'poster_id' => 'attchmnts_poster_id', 'topic_id' => 'attchmnts_topic_id', ], + $this->table_prefix . 'bbcodes' => [ + 'display_on_post' => 'bbcds_display_on_post', + ], + $this->table_prefix . 'forums' => [ + 'left_right_id' => 'frms_left_right_id', + ], $this->table_prefix . 'forums_watch' => [ 'forum_id' => 'frmswtch_forum_id', + 'notify_stat' => 'frmswtch_notify_stat', 'user_id' => 'frmswtch_user_id', ], $this->table_prefix . 'log' => [ @@ -55,10 +62,15 @@ class rename_auth_role_id_index extends migration $this->table_prefix . 'moderator_cache' => [ 'forum_id' => 'mdrtrcch_forum_id', ], + $this->table_prefix . 'modules' => [ + 'left_right_id' => 'mdls_left_right_id', + ], $this->table_prefix . 'oauth_states' => [ + 'provider' => 'oauthsts_provider', 'user_id' => 'oauthsts_user_id', ], $this->table_prefix . 'oauth_tokens' => [ + 'provider' => 'oauthtkns_provider', 'user_id' => 'oauthtkns_user_id', ], $this->table_prefix . 'poll_options' => [ @@ -72,11 +84,26 @@ class rename_auth_role_id_index extends migration 'poster_id' => 'psts_poster_id', 'topic_id' => 'psts_topic_id', ], + $this->table_prefix . 'privmsgs' => [ + 'author_id' => 'pms_author_id', + ], $this->table_prefix . 'privmsgs_folder' => [ - 'user_id' => 'pmfldr_user_id', + 'user_id' => 'pmsfldr_user_id', ], $this->table_prefix . 'privmsgs_rules' => [ - 'user_id' => 'pmrls_user_id', + 'user_id' => 'pmsrls_user_id', + ], + $this->table_prefix . 'privmsgs_to' => [ + 'author_id' => 'pmsto_author_id', + ], + $this->table_prefix . 'reports' => [ + 'post_id' => 'rprts_post_id', + ], + $this->table_prefix . 'search_wordmatch' => [ + 'post_id' => 'wrdmtch_post_id', + ], + $this->table_prefix . 'smilies' => [ + 'display_on_post' => 'smls_display_on_post', ], $this->table_prefix . 'topics' => [ 'forum_id' => 'tpcs_forum_id', @@ -87,6 +114,7 @@ class rename_auth_role_id_index extends migration ], $this->table_prefix . 'topics_watch' => [ 'topic_id' => 'tpcswtch_topic_id', + 'notify_stat' => 'tpcswtch_notify_stat', 'user_id' => 'tpcswtch_user_id', ], $this->table_prefix . 'user_group' => [ From 84d195ac218bfc09c76f588131cb921864a614ca Mon Sep 17 00:00:00 2001 From: rxu Date: Tue, 17 Jun 2025 12:14:01 +0700 Subject: [PATCH 05/14] [ticket/17525] Add migration renaming index test PHPBB-17525 --- tests/dbal/migration/schema_index.php | 65 +++++++++++++++++++++++++++ tests/dbal/migrator_test.php | 24 ++++++++++ 2 files changed, 89 insertions(+) create mode 100644 tests/dbal/migration/schema_index.php diff --git a/tests/dbal/migration/schema_index.php b/tests/dbal/migration/schema_index.php new file mode 100644 index 0000000000..1ea8eb7c78 --- /dev/null +++ b/tests/dbal/migration/schema_index.php @@ -0,0 +1,65 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +class phpbb_dbal_migration_schema_index extends \phpbb\db\migration\migration +{ + function update_schema() + { + return [ + 'add_tables' => [ + $this->table_prefix . 'foobar1' => [ + 'COLUMNS' => [ + 'user_id' => ['UINT', 0], + 'username' => ['VCHAR:50', 0], + ], + 'KEYS' => [ + 'tstidx_user_id' => ['UNIQUE', 'user_id'], + 'tstidx_username' => ['INDEX', 'username'], + ], + ], + $this->table_prefix . 'foobar2' => [ + 'COLUMNS' => [ + 'ban_userid' => ['ULINT', 0], + 'ban_ip' => ['VCHAR:40', ''], + 'ban_reason' => ['VCHAR:100', ''], + ], + 'KEYS' => [ + 'tstidx_ban_userid' => ['UNIQUE', 'ban_userid'], + 'tstidxban_data' => ['INDEX', ['ban_ip', 'ban_reason']], + ], + ], + ], + + 'rename_index' => [ + $this->table_prefix . 'foobar1' => [ + 'tstidx_user_id' => 'fbr1_user_id', + 'tstidx_username' => 'fbr1_username', + ], + $this->table_prefix . 'foobar2' => [ + 'tstidx_ban_userid' => 'fbr2_ban_userid', + 'tstidxban_data' => 'fbr2_ban_data', + ], + ], + ]; + } + + function revert_schema() + { + return [ + 'drop_tables' => [ + $this->table_prefix . 'foobar1', + $this->table_prefix . 'foobar2', + ], + ]; + } +} diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index 5d16fa63ee..f48ad35275 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -24,6 +24,7 @@ require_once __DIR__ . '/migration/revert_table_with_dependency.php'; require_once __DIR__ . '/migration/fail.php'; require_once __DIR__ . '/migration/installed.php'; require_once __DIR__ . '/migration/schema.php'; +require_once __DIR__ . '/migration/schema_index.php'; class phpbb_dbal_migrator_test extends phpbb_database_test_case { @@ -416,4 +417,27 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $this->assertFalse($this->db_tools->sql_column_exists('phpbb_config', 'test_column1')); $this->assertFalse($this->db_tools->sql_table_exists('phpbb_foobar')); } + + public function test_rename_index() + { + $this->migrator->set_migrations(array('phpbb_dbal_migration_schema_index')); + + while (!$this->migrator->finished()) + { + $this->migrator->update(); + } + + $this->assertTrue($this->db_tools->sql_unique_index_exists('phpbb_foobar1', 'fbr1_user_id')); + $this->assertTrue($this->db_tools->sql_index_exists('phpbb_foobar1', 'fbr1_username')); + $this->assertTrue($this->db_tools->sql_unique_index_exists('phpbb_foobar2', 'fbr2_ban_userid')); + $this->assertTrue($this->db_tools->sql_index_exists('phpbb_foobar2', 'fbr2_ban_data')); + + while ($this->migrator->migration_state('phpbb_dbal_migration_schema_index')) + { + $this->migrator->revert('phpbb_dbal_migration_schema_index'); + } + + $this->assertFalse($this->db_tools->sql_table_exists('phpbb_foobar1')); + $this->assertFalse($this->db_tools->sql_table_exists('phpbb_foobar2')); + } } From 45a69eca140895981b41fc8b4ca2b6870aad8cfc Mon Sep 17 00:00:00 2001 From: rxu Date: Tue, 17 Jun 2025 14:09:34 +0700 Subject: [PATCH 06/14] [ticket/17525] Rename migration to reflect its purpose PHPBB-17525 --- ...auth_role_id_index.php => rename_duplicated_index_names.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename phpBB/phpbb/db/migration/data/v400/{rename_auth_role_id_index.php => rename_duplicated_index_names.php} (98%) diff --git a/phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php similarity index 98% rename from phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php rename to phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php index 9537c884dd..0c55bd3854 100644 --- a/phpBB/phpbb/db/migration/data/v400/rename_auth_role_id_index.php +++ b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php @@ -15,7 +15,7 @@ namespace phpbb\db\migration\data\v400; use phpbb\db\migration\migration; -class rename_auth_role_id_index extends migration +class rename_duplicated_index_names extends migration { public static function depends_on() { From 1339a31c2369ed545dc086873e644bb28b5bce1b Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 25 Jun 2025 17:42:36 +0700 Subject: [PATCH 07/14] [ticket/17525] Rename all indexes to make names unique With this reNAMING schema, max index name length is 23. PHPBB-17525 --- phpBB/phpbb/db/doctrine/table_helper.php | 102 ++++++++++++++ .../v400/rename_duplicated_index_names.php | 125 ++++-------------- 2 files changed, 129 insertions(+), 98 deletions(-) diff --git a/phpBB/phpbb/db/doctrine/table_helper.php b/phpBB/phpbb/db/doctrine/table_helper.php index 935db35838..98d33b80cd 100644 --- a/phpBB/phpbb/db/doctrine/table_helper.php +++ b/phpBB/phpbb/db/doctrine/table_helper.php @@ -123,6 +123,108 @@ class table_helper } } + /** + * Maps short table names for the purpose of prefixing tables' index names. + * + * @param array $additional_tables Additional table names without prefix to add to the map. + * @param array $table_prefix Tables prefix. + * + * @return array Pairs of table names and their short name representations. + */ + public static function map_short_table_names(array $additional_tables = [], string $table_prefix = ''): array + { + $short_table_names_map = [ + "{$table_prefix}acl_groups" => 'aclgrps', + "{$table_prefix}acl_options" => 'aclopts', + "{$table_prefix}acl_roles" => 'aclrls', + "{$table_prefix}acl_roles_data" => 'aclrlsdt', + "{$table_prefix}acl_users" => 'aclusrs', + "{$table_prefix}attachments" => 'atchmnts', + "{$table_prefix}backups" => 'bckps', + "{$table_prefix}bans" => 'bans', + "{$table_prefix}bbcodes" => 'bbcds', + "{$table_prefix}bookmarks" => 'bkmrks', + "{$table_prefix}bots" => 'bots', + "{$table_prefix}captcha_answers" => 'cptchans', + "{$table_prefix}captcha_questions" => 'cptchqs', + "{$table_prefix}config" => 'cnfg', + "{$table_prefix}config_text" => 'cnfgtxt', + "{$table_prefix}confirm" => 'cnfrm', + "{$table_prefix}disallow" => 'dslw', + "{$table_prefix}drafts" => 'drfts', + "{$table_prefix}ext" => 'ext', + "{$table_prefix}extension_groups" => 'extgrps', + "{$table_prefix}extensions" => 'exts', + "{$table_prefix}forums" => 'frms', + "{$table_prefix}forums_access" => 'frmsacs', + "{$table_prefix}forums_track" => 'frmstrck', + "{$table_prefix}forums_watch" => 'frmswtch', + "{$table_prefix}groups" => 'grps', + "{$table_prefix}icons" => 'icns', + "{$table_prefix}lang" => 'lang', + "{$table_prefix}log" => 'log', + "{$table_prefix}login_attempts" => 'lgnatmpts', + "{$table_prefix}migrations" => 'mgrtns', + "{$table_prefix}moderator_cache" => 'mdrtche', + "{$table_prefix}modules" => 'mdls', + "{$table_prefix}notification_emails"=> 'ntfemls', + "{$table_prefix}notification_push" => 'ntfpsh', + "{$table_prefix}notification_types" => 'ntftps', + "{$table_prefix}notifications" => 'nftcns', + "{$table_prefix}oauth_accounts" => 'oauthacnts', + "{$table_prefix}oauth_states" => 'oauthsts', + "{$table_prefix}oauth_tokens" => 'oauthtkns', + "{$table_prefix}poll_options" => 'pllopts', + "{$table_prefix}poll_votes" => 'pllvts', + "{$table_prefix}posts" => 'psts', + "{$table_prefix}privmsgs" => 'pms', + "{$table_prefix}privmsgs_folder" => 'pmsfldr', + "{$table_prefix}privmsgs_rules" => 'pmsrls', + "{$table_prefix}privmsgs_to" => 'pmsto', + "{$table_prefix}profile_fields" => 'prflds', + "{$table_prefix}profile_fields_data"=> 'prfldt', + "{$table_prefix}profile_fields_lang"=> 'prfldlng', + "{$table_prefix}profile_lang" => 'prflng', + "{$table_prefix}push_subscriptions" => 'pshsbscrs', + "{$table_prefix}qa_confirm" => 'qacnfm', + "{$table_prefix}ranks" => 'rnks', + "{$table_prefix}reports" => 'rprts', + "{$table_prefix}reports_reasons" => 'rprtrsns', + "{$table_prefix}search_results" => 'srchrslts', + "{$table_prefix}search_wordlist" => 'wrdlst', + "{$table_prefix}search_wordmatch" => 'wrdmtch', + "{$table_prefix}sessions" => 'ssns', + "{$table_prefix}sessions_keys" => 'ssnkeys', + "{$table_prefix}sitelist" => 'sitelst', + "{$table_prefix}smilies" => 'smls', + "{$table_prefix}storage" => 'strg', + "{$table_prefix}styles" => 'stls', + "{$table_prefix}teampage" => 'teampg', + "{$table_prefix}topics" => 'tpcs', + "{$table_prefix}topics_posted" => 'tpcspstd', + "{$table_prefix}topics_track" => 'tpcstrk', + "{$table_prefix}topics_watch" => 'tpkswtch', + "{$table_prefix}user_group" => 'usrgrp', + "{$table_prefix}user_notifications" => 'usrntfs', + "{$table_prefix}users" => 'usrs', + "{$table_prefix}warnings" => 'wrns', + "{$table_prefix}words" => 'wrds', + "{$table_prefix}zebra" => 'zbra', + ]; + + // Add table prefix to additional tables + if (!empty($table_prefix && !empty($additional_tables))) + { + foreach ($additional_tables as $key => $value) + { + $additional_tables["{$table_prefix}{$key}"] = $value; + unset($additional_tables[$key]); + } + } + + return array_merge($short_table_names_map, $additional_tables); + } + /** * Private constructor. Call methods of table_helper statically. */ diff --git a/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php index 0c55bd3854..60900f67ae 100644 --- a/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php +++ b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php @@ -14,6 +14,7 @@ namespace phpbb\db\migration\data\v400; use phpbb\db\migration\migration; +use phpbb\db\doctrine\table_helper; class rename_duplicated_index_names extends migration { @@ -26,105 +27,33 @@ class rename_duplicated_index_names extends migration public function update_schema() { + $rename_index = $table_keys = []; + $db_table_schema = json_decode(file_get_contents($this->phpbb_root_path . 'store/schema.json'), true); + foreach ($db_table_schema as $table_name => $table_data) + { + if (isset($table_data['KEYS'])) + { + $table_name = $this->table_prefix . $table_name; + foreach ($table_data['KEYS'] as $key_name => $key_data) + { + $table_keys[$table_name][] = $key_name; + } + } + } + + $short_table_names = table_helper::map_short_table_names([], $this->table_prefix); + foreach ($table_keys as $table_name => $key_names) + { + $key_name_new = $short_table_names[$table_name] . '_' . $key_name; + foreach ($key_names as $key_name) + { + $rename_index[$table_name][$key_name] = $key_name_new; + $rename_index[$table_name][$table_name . '_' . $key_name] = $key_name_new; + } + } + return [ - 'rename_index' => [ - $this->table_prefix . 'acl_groups' => [ - 'auth_role_id' => 'aclgrps_auth_role_id', - 'group_id' => 'aclgrps_group_id', - ], - $this->table_prefix . 'acl_users' => [ - 'auth_role_id' => 'aclusrs_auth_role_id', - 'user_id' => 'aclusrs_user_id', - ], - $this->table_prefix . 'attachments' => [ - 'poster_id' => 'attchmnts_poster_id', - 'topic_id' => 'attchmnts_topic_id', - ], - $this->table_prefix . 'bbcodes' => [ - 'display_on_post' => 'bbcds_display_on_post', - ], - $this->table_prefix . 'forums' => [ - 'left_right_id' => 'frms_left_right_id', - ], - $this->table_prefix . 'forums_watch' => [ - 'forum_id' => 'frmswtch_forum_id', - 'notify_stat' => 'frmswtch_notify_stat', - 'user_id' => 'frmswtch_user_id', - ], - $this->table_prefix . 'log' => [ - 'forum_id' => 'log_forum_id', - 'topic_id' => 'log_topic_id', - 'user_id' => 'log_user_id', - ], - $this->table_prefix . 'login_attempts' => [ - 'user_id' => 'lgnatmpts_user_id', - ], - $this->table_prefix . 'moderator_cache' => [ - 'forum_id' => 'mdrtrcch_forum_id', - ], - $this->table_prefix . 'modules' => [ - 'left_right_id' => 'mdls_left_right_id', - ], - $this->table_prefix . 'oauth_states' => [ - 'provider' => 'oauthsts_provider', - 'user_id' => 'oauthsts_user_id', - ], - $this->table_prefix . 'oauth_tokens' => [ - 'provider' => 'oauthtkns_provider', - 'user_id' => 'oauthtkns_user_id', - ], - $this->table_prefix . 'poll_options' => [ - 'topic_id' => 'pllopts_topic_id', - ], - $this->table_prefix . 'poll_votes' => [ - 'topic_id' => 'pllvts_topic_id', - ], - $this->table_prefix . 'posts' => [ - 'forum_id' => 'psts_forum_id', - 'poster_id' => 'psts_poster_id', - 'topic_id' => 'psts_topic_id', - ], - $this->table_prefix . 'privmsgs' => [ - 'author_id' => 'pms_author_id', - ], - $this->table_prefix . 'privmsgs_folder' => [ - 'user_id' => 'pmsfldr_user_id', - ], - $this->table_prefix . 'privmsgs_rules' => [ - 'user_id' => 'pmsrls_user_id', - ], - $this->table_prefix . 'privmsgs_to' => [ - 'author_id' => 'pmsto_author_id', - ], - $this->table_prefix . 'reports' => [ - 'post_id' => 'rprts_post_id', - ], - $this->table_prefix . 'search_wordmatch' => [ - 'post_id' => 'wrdmtch_post_id', - ], - $this->table_prefix . 'smilies' => [ - 'display_on_post' => 'smls_display_on_post', - ], - $this->table_prefix . 'topics' => [ - 'forum_id' => 'tpcs_forum_id', - ], - $this->table_prefix . 'topics_track' => [ - 'forum_id' => 'tpcstrk_forum_id', - 'topic_id' => 'tpcstrk_topic_id', - ], - $this->table_prefix . 'topics_watch' => [ - 'topic_id' => 'tpcswtch_topic_id', - 'notify_stat' => 'tpcswtch_notify_stat', - 'user_id' => 'tpcswtch_user_id', - ], - $this->table_prefix . 'user_group' => [ - 'group_id' => 'usrgrp_group_id', - 'user_id' => 'usrgrp_user_id', - ], - $this->table_prefix . 'user_notifications' => [ - 'user_id' => 'usrntf_user_id', - ], - ], + 'rename_index' => $rename_index, ]; } From a229797cd756e6b05a735709ca13649a2fb3cf86 Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 25 Jun 2025 20:52:45 +0700 Subject: [PATCH 08/14] [ticket/17525] Add database schema getter PHPBB-17525 --- phpBB/phpbb/db/doctrine/table_helper.php | 3 ++- .../v400/rename_duplicated_index_names.php | 26 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/phpBB/phpbb/db/doctrine/table_helper.php b/phpBB/phpbb/db/doctrine/table_helper.php index 98d33b80cd..448597df3d 100644 --- a/phpBB/phpbb/db/doctrine/table_helper.php +++ b/phpBB/phpbb/db/doctrine/table_helper.php @@ -130,11 +130,12 @@ class table_helper * @param array $table_prefix Tables prefix. * * @return array Pairs of table names and their short name representations. + * @psalm-return array{string, string} */ public static function map_short_table_names(array $additional_tables = [], string $table_prefix = ''): array { $short_table_names_map = [ - "{$table_prefix}acl_groups" => 'aclgrps', + "{$table_prefix}acl_groups" => 'aclgrps', "{$table_prefix}acl_options" => 'aclopts', "{$table_prefix}acl_roles" => 'aclrls', "{$table_prefix}acl_roles_data" => 'aclrlsdt', diff --git a/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php index 60900f67ae..e3924c1c9d 100644 --- a/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php +++ b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php @@ -28,12 +28,11 @@ class rename_duplicated_index_names extends migration public function update_schema() { $rename_index = $table_keys = []; - $db_table_schema = json_decode(file_get_contents($this->phpbb_root_path . 'store/schema.json'), true); + $db_table_schema = $this->get_schema(); foreach ($db_table_schema as $table_name => $table_data) { if (isset($table_data['KEYS'])) { - $table_name = $this->table_prefix . $table_name; foreach ($table_data['KEYS'] as $key_name => $key_data) { $table_keys[$table_name][] = $key_name; @@ -66,4 +65,27 @@ class rename_duplicated_index_names extends migration return $schema; } + + protected function get_schema() + { + $self_classname = '\\' . str_replace('/', '\\', self::class); + $finder_factory = new \phpbb\finder\factory(null, false, $this->phpbb_root_path, $this->php_ext); + $finder = $finder_factory->get(); + $migrator_classes = $finder->core_path('phpbb/db/migration/data/')->get_classes(); + $self_class_index = array_search($self_classname, $migrator_classes); + unset($migrator_classes[$self_class_index]); + + $schema_generator = new \phpbb\db\migration\schema_generator( + $migrator_classes, + $this->config, + $this->db, + $this->db_tools, + $this->phpbb_root_path, + $this->php_ext, + $this->table_prefix, + $this->tables + ); + + return $schema_generator->get_schema(); + } } From 75c5fe9459260e91e8887cc68e845865a0d6490b Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 25 Jun 2025 22:24:50 +0700 Subject: [PATCH 09/14] [ticket/17525] Add index names test for generated database schema PHPBB-17525 --- phpBB/phpbb/db/doctrine/table_helper.php | 2 +- tests/dbal/migrator_test.php | 55 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/doctrine/table_helper.php b/phpBB/phpbb/db/doctrine/table_helper.php index 448597df3d..70ac39078f 100644 --- a/phpBB/phpbb/db/doctrine/table_helper.php +++ b/phpBB/phpbb/db/doctrine/table_helper.php @@ -127,7 +127,7 @@ class table_helper * Maps short table names for the purpose of prefixing tables' index names. * * @param array $additional_tables Additional table names without prefix to add to the map. - * @param array $table_prefix Tables prefix. + * @param string $table_prefix Tables prefix. * * @return array Pairs of table names and their short name representations. * @psalm-return array{string, string} diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index f48ad35275..ac502f1816 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -440,4 +440,59 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $this->assertFalse($this->db_tools->sql_table_exists('phpbb_foobar1')); $this->assertFalse($this->db_tools->sql_table_exists('phpbb_foobar2')); } + + public function test_schema_generator(): array + { + global $phpbb_root_path, $phpEx; + + $finder_factory = new \phpbb\finder\factory(null, false, $phpbb_root_path, $phpEx); + $finder = $finder_factory->get(); + $migrator_classes = $finder->core_path('phpbb/db/migration/data/')->get_classes(); + + $schema_generator = new \phpbb\db\migration\schema_generator( + $migrator_classes, + $this->config, + $this->db, + $this->db_tools, + $phpbb_root_path, + $phpEx, + 'phpbb_', + self::get_core_tables() + ); + $db_table_schema = $schema_generator->get_schema(); + + $this->assertNotEmpty($db_table_schema); + + return $db_table_schema; + } + + /** + * @depends test_schema_generator + */ + public function test_table_indexes(array $db_table_schema) + { + $table_keys = []; + foreach ($db_table_schema as $table_name => $table_data) + { + if (isset($table_data['KEYS'])) + { + foreach ($table_data['KEYS'] as $key_name => $key_data) + { + $table_keys[$table_name][] = $key_name; + } + } + } + + $this->assertNotEmpty($table_keys); + + $short_table_names = \phpbb\db\doctrine\table_helper::map_short_table_names([], 'phpbb_'); + foreach ($table_keys as $table_name => $key_names) + { + $index_prefix = $short_table_names[$table_name] . '_'; + foreach ($key_names as $key_name) + { + $this->assertFalse(strpos($key_name, $index_prefix), "$key_name does not contain $index_prefix"); + } + } + } } From de1f6329ff7dae141bb0073a0be8604a5c7dd525 Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 25 Jun 2025 22:44:08 +0700 Subject: [PATCH 10/14] [ticket/17525] Fix tests PHPBB-17525 --- phpBB/phpbb/db/doctrine/table_helper.php | 2 +- tests/dbal/migrator_test.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/db/doctrine/table_helper.php b/phpBB/phpbb/db/doctrine/table_helper.php index 70ac39078f..ec5c2040e8 100644 --- a/phpBB/phpbb/db/doctrine/table_helper.php +++ b/phpBB/phpbb/db/doctrine/table_helper.php @@ -214,7 +214,7 @@ class table_helper ]; // Add table prefix to additional tables - if (!empty($table_prefix && !empty($additional_tables))) + if (!empty($table_prefix) && !empty($additional_tables)) { foreach ($additional_tables as $key => $value) { diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index ac502f1816..454c05b5eb 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -491,7 +491,7 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $index_prefix = $short_table_names[$table_name] . '_'; foreach ($key_names as $key_name) { - $this->assertFalse(strpos($key_name, $index_prefix), "$key_name does not contain $index_prefix"); + $this->assertEquals(0, strpos($key_name, $index_prefix), "$key_name does not contain $index_prefix"); } } } From 5e9d616f5760812cd5ed6b63dca0fefb4adf0b78 Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 25 Jun 2025 23:48:45 +0700 Subject: [PATCH 11/14] [ticket/17525] Map Sphinx table, add more test assertions PHPBB-17525 --- phpBB/phpbb/db/doctrine/table_helper.php | 3 ++- tests/dbal/migrator_test.php | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/db/doctrine/table_helper.php b/phpBB/phpbb/db/doctrine/table_helper.php index ec5c2040e8..972d31eab4 100644 --- a/phpBB/phpbb/db/doctrine/table_helper.php +++ b/phpBB/phpbb/db/doctrine/table_helper.php @@ -198,12 +198,13 @@ class table_helper "{$table_prefix}sessions_keys" => 'ssnkeys', "{$table_prefix}sitelist" => 'sitelst', "{$table_prefix}smilies" => 'smls', + "{$table_prefix}sphinx" => 'sphnx', "{$table_prefix}storage" => 'strg', "{$table_prefix}styles" => 'stls', "{$table_prefix}teampage" => 'teampg', "{$table_prefix}topics" => 'tpcs', "{$table_prefix}topics_posted" => 'tpcspstd', - "{$table_prefix}topics_track" => 'tpcstrk', + "{$table_prefix}topics_track" => 'tpcstrck', "{$table_prefix}topics_watch" => 'tpkswtch', "{$table_prefix}user_group" => 'usrgrp', "{$table_prefix}user_notifications" => 'usrntfs', diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index 454c05b5eb..e7fbd4eb2d 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -485,7 +485,10 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $this->assertNotEmpty($table_keys); - $short_table_names = \phpbb\db\doctrine\table_helper::map_short_table_names([], 'phpbb_'); + $short_table_names = \phpbb\db\doctrine\table_helper::map_short_table_names(['custom_table' => 'cstmtbl'], 'phpbb_'); + $this->assertEquals('phpbb_custom_table', array_search('cstmtbl', $short_table_names)); + $this->assertEquals($short_table_names['phpbb_custom_table'], 'cstmtbl'); + foreach ($table_keys as $table_name => $key_names) { $index_prefix = $short_table_names[$table_name] . '_'; From 4da8591dd8a47d917ed2aa39830c71d71cb5f718 Mon Sep 17 00:00:00 2001 From: rxu Date: Fri, 27 Jun 2025 22:10:27 +0700 Subject: [PATCH 12/14] [ticket/17525] Run index renaming migration the last in migrations queue PHPBB-17525 --- .../db/migration/data/v400/rename_duplicated_index_names.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php index e3924c1c9d..0ca9359a47 100644 --- a/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php +++ b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php @@ -21,7 +21,7 @@ class rename_duplicated_index_names extends migration public static function depends_on() { return [ - '\phpbb\db\migration\data\v400\dev', + '\phpbb\db\migration\data\v400\storage_track_index', ]; } @@ -43,9 +43,9 @@ class rename_duplicated_index_names extends migration $short_table_names = table_helper::map_short_table_names([], $this->table_prefix); foreach ($table_keys as $table_name => $key_names) { - $key_name_new = $short_table_names[$table_name] . '_' . $key_name; foreach ($key_names as $key_name) { + $key_name_new = $short_table_names[$table_name] . '_' . $key_name; $rename_index[$table_name][$key_name] = $key_name_new; $rename_index[$table_name][$table_name . '_' . $key_name] = $key_name_new; } From 58b3e5dee04f26f5a160e765691fe7401ee0e0db Mon Sep 17 00:00:00 2001 From: rxu Date: Tue, 1 Jul 2025 00:11:36 +0700 Subject: [PATCH 13/14] [ticket/17525] Migration to rename actual database indexes if any Also generate short table names rather than use hardcoded map PHPBB-17525 --- phpBB/phpbb/db/doctrine/table_helper.php | 128 +++++------------- .../v400/rename_duplicated_index_names.php | 77 ++++++++--- 2 files changed, 94 insertions(+), 111 deletions(-) diff --git a/phpBB/phpbb/db/doctrine/table_helper.php b/phpBB/phpbb/db/doctrine/table_helper.php index 972d31eab4..150e98d95b 100644 --- a/phpBB/phpbb/db/doctrine/table_helper.php +++ b/phpBB/phpbb/db/doctrine/table_helper.php @@ -124,107 +124,51 @@ class table_helper } /** - * Maps short table names for the purpose of prefixing tables' index names. + * Maps table names to thair short names for the purpose of prefixing tables' index names. * - * @param array $additional_tables Additional table names without prefix to add to the map. - * @param string $table_prefix Tables prefix. + * @param array $table_names Table names with prefix to add to the map. + * @param string $table_prefix Tables prefix. * * @return array Pairs of table names and their short name representations. * @psalm-return array{string, string} */ - public static function map_short_table_names(array $additional_tables = [], string $table_prefix = ''): array + public static function map_short_table_names(array $table_names = [], string $table_prefix = ''): array { - $short_table_names_map = [ - "{$table_prefix}acl_groups" => 'aclgrps', - "{$table_prefix}acl_options" => 'aclopts', - "{$table_prefix}acl_roles" => 'aclrls', - "{$table_prefix}acl_roles_data" => 'aclrlsdt', - "{$table_prefix}acl_users" => 'aclusrs', - "{$table_prefix}attachments" => 'atchmnts', - "{$table_prefix}backups" => 'bckps', - "{$table_prefix}bans" => 'bans', - "{$table_prefix}bbcodes" => 'bbcds', - "{$table_prefix}bookmarks" => 'bkmrks', - "{$table_prefix}bots" => 'bots', - "{$table_prefix}captcha_answers" => 'cptchans', - "{$table_prefix}captcha_questions" => 'cptchqs', - "{$table_prefix}config" => 'cnfg', - "{$table_prefix}config_text" => 'cnfgtxt', - "{$table_prefix}confirm" => 'cnfrm', - "{$table_prefix}disallow" => 'dslw', - "{$table_prefix}drafts" => 'drfts', - "{$table_prefix}ext" => 'ext', - "{$table_prefix}extension_groups" => 'extgrps', - "{$table_prefix}extensions" => 'exts', - "{$table_prefix}forums" => 'frms', - "{$table_prefix}forums_access" => 'frmsacs', - "{$table_prefix}forums_track" => 'frmstrck', - "{$table_prefix}forums_watch" => 'frmswtch', - "{$table_prefix}groups" => 'grps', - "{$table_prefix}icons" => 'icns', - "{$table_prefix}lang" => 'lang', - "{$table_prefix}log" => 'log', - "{$table_prefix}login_attempts" => 'lgnatmpts', - "{$table_prefix}migrations" => 'mgrtns', - "{$table_prefix}moderator_cache" => 'mdrtche', - "{$table_prefix}modules" => 'mdls', - "{$table_prefix}notification_emails"=> 'ntfemls', - "{$table_prefix}notification_push" => 'ntfpsh', - "{$table_prefix}notification_types" => 'ntftps', - "{$table_prefix}notifications" => 'nftcns', - "{$table_prefix}oauth_accounts" => 'oauthacnts', - "{$table_prefix}oauth_states" => 'oauthsts', - "{$table_prefix}oauth_tokens" => 'oauthtkns', - "{$table_prefix}poll_options" => 'pllopts', - "{$table_prefix}poll_votes" => 'pllvts', - "{$table_prefix}posts" => 'psts', - "{$table_prefix}privmsgs" => 'pms', - "{$table_prefix}privmsgs_folder" => 'pmsfldr', - "{$table_prefix}privmsgs_rules" => 'pmsrls', - "{$table_prefix}privmsgs_to" => 'pmsto', - "{$table_prefix}profile_fields" => 'prflds', - "{$table_prefix}profile_fields_data"=> 'prfldt', - "{$table_prefix}profile_fields_lang"=> 'prfldlng', - "{$table_prefix}profile_lang" => 'prflng', - "{$table_prefix}push_subscriptions" => 'pshsbscrs', - "{$table_prefix}qa_confirm" => 'qacnfm', - "{$table_prefix}ranks" => 'rnks', - "{$table_prefix}reports" => 'rprts', - "{$table_prefix}reports_reasons" => 'rprtrsns', - "{$table_prefix}search_results" => 'srchrslts', - "{$table_prefix}search_wordlist" => 'wrdlst', - "{$table_prefix}search_wordmatch" => 'wrdmtch', - "{$table_prefix}sessions" => 'ssns', - "{$table_prefix}sessions_keys" => 'ssnkeys', - "{$table_prefix}sitelist" => 'sitelst', - "{$table_prefix}smilies" => 'smls', - "{$table_prefix}sphinx" => 'sphnx', - "{$table_prefix}storage" => 'strg', - "{$table_prefix}styles" => 'stls', - "{$table_prefix}teampage" => 'teampg', - "{$table_prefix}topics" => 'tpcs', - "{$table_prefix}topics_posted" => 'tpcspstd', - "{$table_prefix}topics_track" => 'tpcstrck', - "{$table_prefix}topics_watch" => 'tpkswtch', - "{$table_prefix}user_group" => 'usrgrp', - "{$table_prefix}user_notifications" => 'usrntfs', - "{$table_prefix}users" => 'usrs', - "{$table_prefix}warnings" => 'wrns', - "{$table_prefix}words" => 'wrds', - "{$table_prefix}zebra" => 'zbra', - ]; - - // Add table prefix to additional tables - if (!empty($table_prefix) && !empty($additional_tables)) + $short_table_names_map = []; + foreach ($table_names as $table_name) { - foreach ($additional_tables as $key => $value) - { - $additional_tables["{$table_prefix}{$key}"] = $value; - unset($additional_tables[$key]); - } + $short_table_names_map[$table_name] = self::generate_shortname(str_replace($table_prefix, '', $table_name)); } - return array_merge($short_table_names_map, $additional_tables); + return $short_table_names_map; + } + + /** + * 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. + * + * @return string Short table name. + */ + public static function generate_shortname(string $table_name = ''): string + { + // Only shorten actually long names + if (strlen($table_name) > 4) + { + // Remove vowels + $table_name = preg_replace('/([^aeiou_])([aeiou]+)/i', '$1', $table_name); + + // Remove underscores + $table_name = str_replace('_', '', $table_name); + + // Remove repeated consonants and their combinations (like 'ss', 'flfl' and similar) + $table_name = preg_replace('/(.+)\\1+/i', '$1', $table_name); + + // Restrict short name length to 10 chars + $table_name = substr($table_name, 0, 10); + } + + return $table_name; } /** diff --git a/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php index 0ca9359a47..eb785878fb 100644 --- a/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php +++ b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php @@ -13,10 +13,10 @@ namespace phpbb\db\migration\data\v400; -use phpbb\db\migration\migration; +use phpbb\db\migration\container_aware_migration; use phpbb\db\doctrine\table_helper; -class rename_duplicated_index_names extends migration +class rename_duplicated_index_names extends container_aware_migration { public static function depends_on() { @@ -27,27 +27,25 @@ class rename_duplicated_index_names extends migration public function update_schema() { - $rename_index = $table_keys = []; - $db_table_schema = $this->get_schema(); - foreach ($db_table_schema as $table_name => $table_data) - { - if (isset($table_data['KEYS'])) - { - foreach ($table_data['KEYS'] as $key_name => $key_data) - { - $table_keys[$table_name][] = $key_name; - } - } - } + $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); - $short_table_names = table_helper::map_short_table_names([], $this->table_prefix); - foreach ($table_keys as $table_name => $key_names) + foreach ($tables_index_names as $table_name => $key_names) { foreach ($key_names as $key_name) { - $key_name_new = $short_table_names[$table_name] . '_' . $key_name; - $rename_index[$table_name][$key_name] = $key_name_new; - $rename_index[$table_name][$table_name . '_' . $key_name] = $key_name_new; + $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); + + $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; } } @@ -88,4 +86,45 @@ class rename_duplicated_index_names extends migration return $schema_generator->get_schema(); } + + public function get_tables_index_names() + { + $table_keys = []; + $doctrine = $this->container->get('dbal.conn.doctrine'); + $schema_manager = $doctrine->createSchemaManager(); + $table_names = $schema_manager->listTableNames(); + + if (!empty($table_names)) + { + 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) + { + return !$index->isPrimary(); + }) + ); + + if (!empty($index_names)) + { + $table_keys[$table_name] = $index_names; + } + } + } + else + { + $db_table_schema = $this->get_schema(); + foreach ($db_table_schema as $table_name => $table_data) + { + if (isset($table_data['KEYS'])) + { + $table_keys[$table_name] = array_keys($table_data['KEYS']); + } + } + } + + return $table_keys; + } } From 1cadb3818af3cf891722ba9bffc526d9ba098e6c Mon Sep 17 00:00:00 2001 From: rxu Date: Tue, 1 Jul 2025 14:27:38 +0700 Subject: [PATCH 14/14] [ticket/17525] Provide Doctrine connection object for migrations PHPBB-17525 --- .../migration/data/v400/rename_duplicated_index_names.php | 7 +++---- phpBB/phpbb/db/migration/migration.php | 6 ++++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php index eb785878fb..b4c10271e6 100644 --- a/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php +++ b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php @@ -13,10 +13,10 @@ namespace phpbb\db\migration\data\v400; -use phpbb\db\migration\container_aware_migration; +use phpbb\db\migration\migration; use phpbb\db\doctrine\table_helper; -class rename_duplicated_index_names extends container_aware_migration +class rename_duplicated_index_names extends migration { public static function depends_on() { @@ -90,8 +90,7 @@ class rename_duplicated_index_names extends container_aware_migration public function get_tables_index_names() { $table_keys = []; - $doctrine = $this->container->get('dbal.conn.doctrine'); - $schema_manager = $doctrine->createSchemaManager(); + $schema_manager = $this->db_doctrine->createSchemaManager(); $table_names = $schema_manager->listTableNames(); if (!empty($table_names)) diff --git a/phpBB/phpbb/db/migration/migration.php b/phpBB/phpbb/db/migration/migration.php index 6bfc395e4d..63290229f9 100644 --- a/phpBB/phpbb/db/migration/migration.php +++ b/phpBB/phpbb/db/migration/migration.php @@ -28,6 +28,9 @@ abstract class migration implements migration_interface /** @var \phpbb\db\driver\driver_interface */ protected $db; + /** @var \Doctrine\DBAL\Connection */ + protected $db_doctrine; + /** @var \phpbb\db\tools\tools_interface */ protected $db_tools; @@ -72,6 +75,9 @@ abstract class migration implements migration_interface $this->php_ext = $php_ext; $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); } /**