This commit is contained in:
rxu 2025-06-25 12:43:09 +02:00 committed by GitHub
commit a55478cd28
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 358 additions and 28 deletions

View file

@ -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", '<br>', $e->getTraceAsString()));
$message = $e->getMessage();
$this->template->assign_var('MIGRATOR_ERROR', '<b>' . $message . '</b><br><br>' . $stack_trace);
}
$this->tpl_name = 'acp_ext_enable';

View file

@ -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);
}

View file

@ -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();
}

View file

@ -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<string, string> 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.
*/

View file

@ -0,0 +1,69 @@
<?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\v400;
use phpbb\db\migration\migration;
use phpbb\db\doctrine\table_helper;
class rename_duplicated_index_names extends migration
{
public static function depends_on()
{
return [
'\phpbb\db\migration\data\v400\dev',
];
}
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' => $rename_index,
];
}
public function revert_schema()
{
$schema = $this->update_schema();
array_walk($schema['rename_index'], function (&$index_data, $table_name) {
$index_data = array_flip($index_data);
});
return $schema;
}
}

View file

@ -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)

View file

@ -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];
};

View file

@ -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}
*/
@ -458,34 +471,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;
}
/**
@ -553,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)
@ -839,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

View file

@ -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
*

View file

@ -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", '<br>', $e->getTraceAsString()));
$message = $e->getMessage();
$this->iohandler->add_error_message($message, $stack_trace);
$this->iohandler->send_response(true);
$fail_cleanup = true;
}

View file

@ -0,0 +1,65 @@
<?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.
*
*/
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',
],
];
}
}

View file

@ -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'));
}
}