1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-06 08:47:45 +02:00

Merge pull request #6830 from rxu/ticket/17525

[ticket/17525] Correctly handle Doctrine DB tools exceptions, enrich error info
This commit is contained in:
Marc Alexander
2025-07-05 09:31:52 +02:00
committed by GitHub
29 changed files with 621 additions and 55 deletions

View File

@@ -13,6 +13,7 @@
namespace phpbb\db\doctrine;
use InvalidArgumentException;
use phpbb\db\tools\doctrine as doctrine_dbtools;
class table_helper
{
@@ -123,6 +124,54 @@ class table_helper
}
}
/**
* Maps table names to their short names for the purpose of prefixing tables' index names.
*
* @param array $table_names Table names with prefix to add to the map.
* @param string $table_prefix Tables prefix.
*
* @return array<string, string> Pairs of table names and their short name representations.
* @psalm-return array{string, string}
*/
public static function map_short_table_names(array $table_names = [], string $table_prefix = ''): array
{
$short_table_names_map = [];
foreach ($table_names as $table_name)
{
$short_table_names_map[$table_name] = self::generate_shortname(doctrine_dbtools::remove_prefix($table_name, $table_prefix));
}
return $short_table_names_map;
}
/**
* Generates short table names for the purpose of prefixing tables' index names.
*
* @param string $table_name Table name without 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;
}
/**
* Private constructor. Call methods of table_helper statically.
*/

View File

@@ -0,0 +1,146 @@
<?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;
use phpbb\db\tools\doctrine as doctrine_dbtools;
class rename_duplicated_index_names extends migration
{
/**
* @var array
*/
protected static $table_keys;
/**
* @var array
*/
protected static $rename_index;
public static function depends_on()
{
return [
'\phpbb\db\migration\data\v400\storage_track_index',
];
}
public function update_schema()
{
if (!isset(self::$rename_index))
{
if (!isset(self::$table_keys))
{
$this->get_tables_index_names();
}
$short_table_names = table_helper::map_short_table_names(array_keys(self::$table_keys), $this->table_prefix);
foreach (self::$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)
{
// If 'old' key name is already new format, do not rename it
if (str_starts_with($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;
self::$rename_index[$table_name][$key_name] = $key_name_new;
}
}
}
return [
'rename_index' => self::$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;
}
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();
}
public function get_tables_index_names()
{
$schema_manager = $this->db_tools->get_connection()->createSchemaManager();
$table_names = $schema_manager->listTableNames();
if (!empty($table_names))
{
foreach ($table_names as $table_name)
{
$indices = $schema_manager->listTableIndexes($table_name);
$indices = array_keys(array_filter($indices,
function (\Doctrine\DBAL\Schema\Index $index)
{
return !$index->isPrimary();
})
);
if (!empty($indices))
{
self::$table_keys[$table_name] = $indices;
}
}
}
else
{
foreach ($this->get_schema() as $table_name => $table_data)
{
if (isset($table_data['KEYS']))
{
self::$table_keys[$table_name] = array_keys($table_data['KEYS']);
}
}
}
}
}

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

@@ -72,6 +72,8 @@ abstract class migration implements migration_interface
$this->php_ext = $php_ext;
$this->errors = array();
$this->db_tools->set_table_prefix($this->table_prefix);
}
/**

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

@@ -49,6 +49,11 @@ class doctrine implements tools_interface
*/
private $return_statements;
/**
* @var string
*/
private $table_prefix;
/**
* Database tools constructors.
*
@@ -61,6 +66,14 @@ class doctrine implements tools_interface
$this->connection = $connection;
}
/**
* {@inheritDoc}
*/
public function get_connection(): Connection
{
return $this->connection;
}
/**
* @return AbstractSchemaManager
*
@@ -86,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}
*/
@@ -330,6 +351,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}
*/
@@ -384,6 +418,23 @@ class doctrine implements tools_interface
}
}
/**
* {@inheritDoc}
*/
public static function add_prefix(string $name, string $prefix): string
{
return str_ends_with($prefix, '_') ? $prefix . $name : $prefix . '_' . $name;
}
/**
* {@inheritDoc}
*/
public static function remove_prefix(string $name, string $prefix = ''): string
{
$prefix = str_ends_with($prefix, '_') ? $prefix : $prefix . '_';
return $prefix && str_starts_with($name, $prefix) ? substr($name, strlen($prefix)) : $name;
}
/**
* Returns an array of indices for either unique and primary keys, or simple indices.
*
@@ -458,34 +509,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 +596,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)
@@ -610,6 +658,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)
@@ -635,6 +684,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 = !str_starts_with($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)
@@ -667,6 +717,8 @@ class doctrine implements tools_interface
}
/**
* Removes a table
*
* @param Schema $schema
* @param string $table_name
* @param bool $safe_check
@@ -684,6 +736,8 @@ class doctrine implements tools_interface
}
/**
* Adds column to a table
*
* @param Schema $schema
* @param string $table_name
* @param string $column_name
@@ -714,6 +768,8 @@ class doctrine implements tools_interface
}
/**
* Alters column properties
*
* @param Schema $schema
* @param string $table_name
* @param string $column_name
@@ -744,6 +800,8 @@ class doctrine implements tools_interface
}
/**
* Alters column properties or adds a column
*
* @param Schema $schema
* @param string $table_name
* @param string $column_name
@@ -766,6 +824,8 @@ class doctrine implements tools_interface
}
/**
* Removes a column in a table
*
* @param Schema $schema
* @param string $table_name
* @param string $column_name
@@ -818,6 +878,8 @@ class doctrine implements tools_interface
}
/**
* Creates non-unique index for a table
*
* @param Schema $schema
* @param string $table_name
* @param string $index_name
@@ -830,6 +892,8 @@ class doctrine implements tools_interface
{
$columns = (is_array($column)) ? $column : [$column];
$table = $schema->getTable($table_name);
$short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix));
$index_name = !str_starts_with($index_name, $short_table_name) ? self::add_prefix($index_name, $short_table_name) : $index_name;
if ($safe_check && $table->hasIndex($index_name))
{
@@ -840,6 +904,38 @@ class doctrine implements tools_interface
}
/**
* Renames table index
*
* @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);
$short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix));
if (!$table->hasIndex($index_name_old))
{
$index_name_old = !str_starts_with($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 = !str_starts_with($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))
{
return;
}
$table->renameIndex($index_name_old, $index_name_new);
}
/**
* Creates unique (non-primary) index for a table
*
* @param Schema $schema
* @param string $table_name
* @param string $index_name
@@ -852,6 +948,8 @@ class doctrine implements tools_interface
{
$columns = (is_array($column)) ? $column : [$column];
$table = $schema->getTable($table_name);
$short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix));
$index_name = !str_starts_with($index_name, $short_table_name) ? self::add_prefix($index_name, $short_table_name) : $index_name;
if ($safe_check && $table->hasIndex($index_name))
{
@@ -862,6 +960,8 @@ class doctrine implements tools_interface
}
/**
* Removes table index
*
* @param Schema $schema
* @param string $table_name
* @param string $index_name
@@ -872,6 +972,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);
$short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix));
if (!$table->hasIndex($index_name))
{
$index_name = !str_starts_with($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))
{
@@ -882,6 +988,8 @@ class doctrine implements tools_interface
}
/**
* Creates primary key for a table
*
* @param $column
* @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
*
@@ -215,4 +226,41 @@ interface tools_interface
* @return 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 prefix to a string if needed
*
* @param string $name Table name with tables prefix
* @param string $prefix Prefix to add
*
* @return string Prefixed name
*/
public static function add_prefix(string $name, string $prefix): string;
/**
* 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 $prefix Prefix to remove
*
* @return string Prefixless string
*/
public static function remove_prefix(string $name, string $prefix = ''): string;
/**
* 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

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

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