Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions phpBB/phpbb/db/middleware/mysql/platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,34 @@ class platform extends AbstractMySQLPlatform
public function getAlterTableSQL(TableDiff $diff)
{
$sql = parent::getAlterTableSQL($diff);
$table = $diff->getOldTable();
$columns = $diff->getAddedColumns();

foreach ($columns as $column)
if ($table = $diff->getOldTable())
{
$column_name = $column->getName();
if (!empty($column->getAutoincrement()) && $table)
$primary_key_dropped = array_filter($diff->getDroppedIndexes(), function($val, $key)
{
return $val->isPrimary();
},
ARRAY_FILTER_USE_BOTH
);
$columns = $diff->getAddedColumns();
foreach ($columns as $column)
{
foreach ($sql as $i => $query)
$column_name = $column->getName();
if (!empty($column->getAutoincrement()))
{
if (stripos($query, "add $column_name"))
foreach ($sql as $i => $query)
{
if (!$table->getPrimaryKey())
{
$sql[$i] = str_replace(' DEFAULT NULL', '', $sql[$i]);
$sql[$i] .= ' PRIMARY KEY';
}
else
if (stripos($query, "add $column_name"))
{
$sql[$i] .= ", ADD KEY ($column_name)";
if (!$table->getPrimaryKey() || $primary_key_dropped)
{
$sql[$i] = str_replace(' DEFAULT NULL', '', $sql[$i]);
$sql[$i] .= ' PRIMARY KEY';
}
else
{
$sql[$i] .= ", ADD KEY ($column_name)";
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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;

class add_sessions_autoincrement_column extends migration
{
public static function depends_on(): array
{
return [
'\phpbb\db\migration\data\v400\dev',
];
}

public function update_schema(): array
{
return [
'drop_primary_keys' => [
$this->table_prefix . 'sessions' => ['PRIMARY_KEY'],
],
'add_columns' => [
$this->table_prefix . 'sessions' => [
'id' => ['BINT', null, 'auto_increment'],
],
],
'add_primary_keys' => [
$this->table_prefix . 'sessions' => ['id'],
],
'add_index' => [
$this->table_prefix . 'sessions' => [
'session_id' => ['session_id'],
],
],
];
}

public function revert_schema(): array
{
return [
'drop_columns' => [
$this->table_prefix . 'sessions' => ['id'],
],
'drop_keys' => [
$this->table_prefix . 'sessions' => ['session_id'],
],
'add_primary_keys' => [
$this->table_prefix . 'sessions' => ['session_id'],
],
];
}
}
7 changes: 4 additions & 3 deletions phpBB/phpbb/db/migration/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ public function get_schema_steps($schema_changes)
'drop_keys' => 2,
'drop_columns' => 2,
'add_primary_keys' => 2, // perform_schema_changes only uses one level, but second is in the function
'drop_primary_keys' => 1,
'add_unique_index' => 2,
'add_index' => 2,
'rename_index' => 1,
);

foreach ($nested_level as $change_type => $data_depth)
foreach ($schema_changes as $change_type => $schema_change)
{
if (!empty($schema_changes[$change_type]))
if (!empty($data_depth = $nested_level[$change_type]))
{
foreach ($schema_changes[$change_type] as $key => $value)
foreach ($schema_change as $key => $value)
{
if ($data_depth === 1)
{
Expand Down
67 changes: 52 additions & 15 deletions phpBB/phpbb/db/migration/schema_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

use Closure;
use phpbb\config\config;
use phpbb\db\doctrine\table_helper;
use phpbb\db\driver\driver_interface;
use phpbb\db\tools\doctrine;
use phpbb\db\migrator;
use phpbb\db\tools\tools_interface;
use UnexpectedValueException;
Expand All @@ -38,7 +40,7 @@ class schema_generator
protected $class_names;

/** @var string */
protected $table_prefix;
protected static $table_prefix;

/** @var string */
protected $phpbb_root_path;
Expand Down Expand Up @@ -79,7 +81,7 @@ public function __construct(
$this->class_names = $class_names;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->table_prefix = $table_prefix;
self::$table_prefix = $table_prefix;
$this->table_names = $tables;
}

Expand Down Expand Up @@ -174,7 +176,7 @@ private function apply_migration_to_schema(string $migration_class)
$this->db_tools,
$this->phpbb_root_path,
$this->php_ext,
$this->table_prefix,
self::$table_prefix,
$this->table_names
);

Expand All @@ -186,6 +188,7 @@ private function apply_migration_to_schema(string $migration_class)
'change_columns' => 'COLUMNS',
'add_index' => 'KEYS',
'add_primary_keys' => 'PRIMARY_KEY',
'drop_primary_keys' => null,
'add_unique_index' => 'KEYS',
'drop_keys' => 'KEYS',
'rename_index' => 'KEYS',
Expand Down Expand Up @@ -215,8 +218,8 @@ private function apply_migration_to_schema(string $migration_class)
break;

case 'drop':
$action = function(&$value, $changes, $value_transform = null) {
self::unset_all($value, $changes);
$action = function(&$value, $changes, $value_transform = null, $table_name = null) {
self::unset_all($value, $changes, $value_transform, $table_name);
};
break;

Expand Down Expand Up @@ -260,7 +263,7 @@ private function for_each_table(array $data, callable $callback, $column = null,
$target = &$target[$column];
}

$callback($target, $values, $value_transform);
$callback($target, $values, $value_transform, $table);
}
}

Expand All @@ -282,23 +285,39 @@ private static function set_all(&$schema, $data, callable|null $value_transform
}
else
{
$schema[$key] = $change;
if (isset($schema) && is_string($schema))
{
$schema = $change;
}
else
{
$schema[$key] = $change;
}
}
}
}

/**
* Remove an array of values from the schema
*
* @param mixed $schema Reference to the schema entry.
* @param mixed $data Array of values to be removed.
* @param mixed $schema Reference to the schema entry.
* @param mixed $data Array of values to be removed.
* @param callable|null $value_transform Callback to transform the value being set.
* @param string|null $table_name Table name the value being unset for.
*/
private static function unset_all(&$schema, $data)
private static function unset_all(&$schema, $data, callable|null $value_transform = null, string|null $table_name = null)
{
$data = (!is_array($data)) ? [$data] : $data;
foreach ($data as $key)
{
unset($schema[$key]);
if (is_callable($value_transform))
{
$value_transform($schema, $key, $table_name);
}
else
{
unset($schema[$key]);
}
}
}

Expand Down Expand Up @@ -350,7 +369,7 @@ private static function handle_add_column(array &$value, string $key, array $cha
*/
private static function get_value_transform(string $change_type, string $schema_type) : Closure|null
{
if (!in_array($change_type, ['add', 'rename']))
if (!in_array($change_type, ['add', 'drop', 'rename']))
{
return null;
}
Expand Down Expand Up @@ -384,10 +403,28 @@ private static function get_value_transform(string $change_type, string $schema_
$value[$key] = ['UNIQUE', $change];
};

case 'keys':
if ($change_type == 'drop')
{
return function(&$value, $key, $table_name) {
if (!isset($value[$key]))
{
$short_table_name = table_helper::generate_shortname(doctrine::remove_prefix($table_name, self::$table_prefix));
$key = !str_starts_with($key, $short_table_name) ? doctrine::add_prefix($key, $short_table_name) : doctrine::remove_prefix($key, $short_table_name);
}
unset($value[$key]);
};
}
return null;

case 'columns':
return function(&$value, $key, $change) {
self::handle_add_column($value, $key, $change);
};
if ($change_type == 'add')
{
return function(&$value, $key, $change) {
self::handle_add_column($value, $key, $change);
};
}
return null;
}

return null;
Expand Down
48 changes: 47 additions & 1 deletion phpBB/phpbb/db/tools/doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ public function sql_list_index(string $table_name): array
*/
public function sql_index_exists(string $table_name, string $index_name): bool
{
$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;

return $this->asset_exists($index_name, $this->get_filtered_index_list($table_name, true));
}

Expand All @@ -189,6 +192,9 @@ public function sql_index_exists(string $table_name, string $index_name): bool
*/
public function sql_unique_index_exists(string $table_name, string $index_name): bool
{
$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;

return $this->asset_exists($index_name, $this->get_filtered_index_list($table_name, false));
}

Expand Down Expand Up @@ -373,6 +379,19 @@ function (Schema $schema) use ($table_name, $column): void
);
}

/**
* {@inheritDoc}
*/
public function sql_drop_primary_key(string $table_name)
{
return $this->alter_schema(
function (Schema $schema) use ($table_name): void
{
$this->schema_drop_primary_key($schema, $table_name);
}
);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -587,9 +606,15 @@ protected function schema_perform_changes(Schema $schema, array $schema_changes)
'use_key' => false,
'per_table' => true,
],
'drop_primary_keys' => [
'method' => 'schema_drop_primary_key',
'use_key' => false,
'per_table' => true,
],
'add_primary_keys' => [
'method' => 'schema_create_primary_key',
'use_key' => true,
'use_key' => false,
'per_table' => true,
],
'add_unique_index' => [
'method' => 'schema_create_unique_index',
Expand Down Expand Up @@ -990,6 +1015,27 @@ protected function schema_index_drop(Schema $schema, string $table_name, string
$table->dropIndex($index_name);
}

/**
* Drops primary key from a table
*
* @param Schema $schema
* @param string $table_name
* @param bool $safe_check
*
* @throws SchemaException
*/
protected function schema_drop_primary_key(Schema $schema, string $table_name, bool $safe_check = false): void
{
$table = $schema->getTable($table_name);

if ($safe_check && !$table->getPrimaryKey())
{
return;
}

$table->dropPrimaryKey();
}

/**
* Creates primary key for a table
*
Expand Down
10 changes: 10 additions & 0 deletions phpBB/phpbb/db/tools/tools_interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ public function sql_create_unique_index(string $table_name, string $index_name,
*/
public function sql_create_primary_key(string $table_name, $column);


/**
* Drop primary key
*
* @param string $table_name Table to modify
*
* @return bool|string[] True if the statements have been executed
*/
public function sql_drop_primary_key(string $table_name);

/**
* Truncate the table
*
Expand Down
Loading