This commit is contained in:
rxu 2025-07-05 08:00:08 +00:00 committed by GitHub
commit 42c8607f8a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 160 additions and 6 deletions

View file

@ -149,6 +149,7 @@ class connection_parameter_factory
$enrichment_tags = [
'pdo_mysql' => [
'charset' => 'UTF8',
'platform' => new mysql_platform(),
],
'oci8' => [
'charset' => 'UTF8',

View file

@ -0,0 +1,62 @@
<?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\doctrine;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Schema\TableDiff;
/**
* MySQL specific schema handling.
*
* While adding auto_increment column to MySQL, it must be indexed.
* If it's indexed as primary key, it should be declared as NOT NULL
* because MySQL primary key columns cannot be NULL.
*/
class mysql_platform extends AbstractMySQLPlatform
{
/**
* {@inheritDoc}
*/
public function getAlterTableSQL(TableDiff $diff)
{
$sql = parent::getAlterTableSQL($diff);
$table = $diff->getOldTable();
$columns = $diff->getAddedColumns();
foreach ($columns as $column)
{
$column_name = $column->getName();
if (!empty($column->getAutoincrement()) && $table)
{
foreach ($sql as $i => $query)
{
if (stripos($query, "add $column_name"))
{
if (!$table->getPrimaryKey())
{
$sql[$i] = str_replace(' DEFAULT NULL', '', $sql[$i]);
$sql[$i] .= ' PRIMARY KEY';
}
else
{
$sql[$i] .= ", ADD KEY ($column_name)";
}
}
}
}
}
return $sql;
}
}

View file

@ -18,6 +18,7 @@ use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\BigIntType;
use Doctrine\DBAL\Types\IntegerType;
use Doctrine\DBAL\Types\SmallIntType;
@ -78,6 +79,36 @@ class postgresql_platform extends PostgreSQL94Platform
return AbstractPlatform::getDefaultValueDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function getAlterTableSQL(TableDiff $diff)
{
$sql = parent::getAlterTableSQL($diff);
$table_name = $diff->getOldTable()->getName();
$columns = $diff->getAddedColumns();
$post_sql = $sequence_sql = [];
foreach ($columns as $column)
{
$column_name = $column->getName();
if (!empty($column->getAutoincrement()))
{
$sequence = new Sequence($this->getIdentitySequenceName($table_name, $column_name));
$sequence_sql[] = $this->getCreateSequenceSQL($sequence);
$post_sql[] = 'ALTER SEQUENCE '.$sequence->getName().' OWNED BY ' . $table_name . '.' . $column_name;
}
}
$sql = array_merge($sequence_sql, $sql, $post_sql);
foreach ($sql as $i => $query)
{
$sql[$i] = str_replace('{{placeholder_sequence}}', "nextval('{$table_name}_seq')", $query);
}
return $sql;
}
/**
* {@inheritDoc}
*/

View file

@ -101,15 +101,11 @@ class remove_jabber extends migration
];
}
public function move_jabber_to_email_notifications(int|null $start)
public function move_jabber_to_email_notifications()
{
$limit = 1000;
$sql = 'UPDATE ' . $this->tables['user_notifications'] . '
SET ' . $this->db->sql_build_array('UPDATE', ['method' => 'notification.method.email']) . "
WHERE method = 'notification.method.jabber'";
$this->db->sql_query_limit($sql, $limit, $start ?: 0);
return $this->db->sql_affectedrows() < $limit ? true : $start + $limit;
$this->db->sql_query($sql);
}
}

View file

@ -0,0 +1,43 @@
<?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 schema_add_autoincrement extends \phpbb\db\migration\migration
{
function update_schema()
{
return [
'add_tables' => [
$this->table_prefix . 'noid' => [
'COLUMNS' => [
'text' => ['VCHAR:50', ''],
],
],
],
'add_columns' => [
$this->table_prefix . 'noid' => [
'id' => ['UINT:3', null, 'auto_increment'],
],
],
];
}
function revert_schema()
{
return [
'drop_tables' => [
$this->table_prefix . 'noid',
],
];
}
}

View file

@ -25,6 +25,7 @@ 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';
require_once __DIR__ . '/migration/schema_add_autoincrement.php';
class phpbb_dbal_migrator_test extends phpbb_database_test_case
{
@ -502,4 +503,24 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case
}
}
}
public function test_add_autoincrement_column()
{
$this->migrator->set_migrations(['schema_add_autoincrement']);
while (!$this->migrator->finished())
{
$this->migrator->update();
}
$this->assertTrue($this->db_tools->sql_table_exists('phpbb_noid'));
$this->assertTrue($this->db_tools->sql_column_exists('phpbb_noid', 'id'));
while ($this->migrator->migration_state('schema_add_autoincrement'))
{
$this->migrator->revert('schema_add_autoincrement');
}
$this->assertFalse($this->db_tools->sql_table_exists('phpbb_noid'));
}
}