From 950fd93dae81d525d4bf8fff49fc6290a3d43339 Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 18 Jun 2025 21:44:20 +0700 Subject: [PATCH 1/4] [ticket/17528] Add test for adding autoincrement column PHPBB-17528 --- .../migration/schema_add_autoincrement.php | 43 +++++++++++++++++++ tests/dbal/migrator_test.php | 21 +++++++++ 2 files changed, 64 insertions(+) create mode 100644 tests/dbal/migration/schema_add_autoincrement.php diff --git a/tests/dbal/migration/schema_add_autoincrement.php b/tests/dbal/migration/schema_add_autoincrement.php new file mode 100644 index 0000000000..4e3ecff183 --- /dev/null +++ b/tests/dbal/migration/schema_add_autoincrement.php @@ -0,0 +1,43 @@ + +* @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', + ], + ]; + } +} diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index 5d16fa63ee..540dc85aba 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_add_autoincrement.php'; class phpbb_dbal_migrator_test extends phpbb_database_test_case { @@ -416,4 +417,24 @@ 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_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')); + } } From 5b947ee5545c2456b4b9e08750f7e2cfc70d866a Mon Sep 17 00:00:00 2001 From: rxu Date: Thu, 19 Jun 2025 11:54:17 +0700 Subject: [PATCH 2/4] [ticket/17528] Fix phpBB PostgreSQL platform [ci skip] PHPBB-17528 --- .../phpbb/db/doctrine/postgresql_platform.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/phpBB/phpbb/db/doctrine/postgresql_platform.php b/phpBB/phpbb/db/doctrine/postgresql_platform.php index ea637b4203..28f846de03 100644 --- a/phpBB/phpbb/db/doctrine/postgresql_platform.php +++ b/phpBB/phpbb/db/doctrine/postgresql_platform.php @@ -78,6 +78,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 = array_merge($diff->getAddedColumns(), $diff->getModifiedColumns()); + $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} */ From 389a813cdfb2ed16664aa572c0cd72bb398cb5cb Mon Sep 17 00:00:00 2001 From: rxu Date: Fri, 20 Jun 2025 14:46:39 +0700 Subject: [PATCH 3/4] [ticket/17528] Fix MySQL platform PHPBB-17528 --- .../doctrine/connection_parameter_factory.php | 1 + phpBB/phpbb/db/doctrine/mysql_platform.php | 62 +++++++++++++++++++ .../phpbb/db/doctrine/postgresql_platform.php | 1 + 3 files changed, 64 insertions(+) create mode 100644 phpBB/phpbb/db/doctrine/mysql_platform.php diff --git a/phpBB/phpbb/db/doctrine/connection_parameter_factory.php b/phpBB/phpbb/db/doctrine/connection_parameter_factory.php index 740c4b82b7..90e41061fc 100644 --- a/phpBB/phpbb/db/doctrine/connection_parameter_factory.php +++ b/phpBB/phpbb/db/doctrine/connection_parameter_factory.php @@ -149,6 +149,7 @@ class connection_parameter_factory $enrichment_tags = [ 'pdo_mysql' => [ 'charset' => 'UTF8', + 'platform' => new mysql_platform(), ], 'oci8' => [ 'charset' => 'UTF8', diff --git a/phpBB/phpbb/db/doctrine/mysql_platform.php b/phpBB/phpbb/db/doctrine/mysql_platform.php new file mode 100644 index 0000000000..cd368163dd --- /dev/null +++ b/phpBB/phpbb/db/doctrine/mysql_platform.php @@ -0,0 +1,62 @@ + + * @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 ahould 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; + } +} diff --git a/phpBB/phpbb/db/doctrine/postgresql_platform.php b/phpBB/phpbb/db/doctrine/postgresql_platform.php index 28f846de03..754ea47a99 100644 --- a/phpBB/phpbb/db/doctrine/postgresql_platform.php +++ b/phpBB/phpbb/db/doctrine/postgresql_platform.php @@ -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; From b21019885240f2403fcfb2887c6fb564eaad32c1 Mon Sep 17 00:00:00 2001 From: rxu Date: Fri, 20 Jun 2025 15:26:57 +0700 Subject: [PATCH 4/4] [ticket/17528] Fix PostgreSQL platform issues PHPBB-17528 --- phpBB/phpbb/db/doctrine/postgresql_platform.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/phpBB/phpbb/db/doctrine/postgresql_platform.php b/phpBB/phpbb/db/doctrine/postgresql_platform.php index 754ea47a99..2fa226b3ab 100644 --- a/phpBB/phpbb/db/doctrine/postgresql_platform.php +++ b/phpBB/phpbb/db/doctrine/postgresql_platform.php @@ -79,14 +79,14 @@ class postgresql_platform extends PostgreSQL94Platform return AbstractPlatform::getDefaultValueDeclarationSQL($column); } - /** - * {@inheritDoc} - */ - public function getAlterTableSQL(TableDiff $diff) - { + /** + * {@inheritDoc} + */ + public function getAlterTableSQL(TableDiff $diff) + { $sql = parent::getAlterTableSQL($diff); $table_name = $diff->getOldTable()->getName(); - $columns = array_merge($diff->getAddedColumns(), $diff->getModifiedColumns()); + $columns = $diff->getAddedColumns(); $post_sql = $sequence_sql = []; foreach ($columns as $column)