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..8a2090b6d9 --- /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 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; + } +} diff --git a/phpBB/phpbb/db/doctrine/postgresql_platform.php b/phpBB/phpbb/db/doctrine/postgresql_platform.php index ea637b4203..2fa226b3ab 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; @@ -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} */ diff --git a/phpBB/phpbb/db/migration/data/v400/remove_jabber.php b/phpBB/phpbb/db/migration/data/v400/remove_jabber.php index 12759f5f47..17bf49d753 100644 --- a/phpBB/phpbb/db/migration/data/v400/remove_jabber.php +++ b/phpBB/phpbb/db/migration/data/v400/remove_jabber.php @@ -34,7 +34,15 @@ class remove_jabber extends migration $this->table_prefix . 'users' => [ 'user_jabber', ], - ] + ], + 'add_columns' => [ + $this->table_prefix . 'user_notifications' => [ + 'id' => ['ULINT', null, 'auto_increment'], + ], + ], + 'add_primary_keys' => [ + $this->table_prefix . 'user_notifications' => ['id'], + ], ]; } @@ -45,7 +53,12 @@ class remove_jabber extends migration $this->table_prefix . 'users' => [ 'user_jabber' => ['VCHAR_UNI', ''], ], - ] + ], + 'drop_columns' => [ + $this->table_prefix . 'user_notifications' => [ + 'id', + ], + ], ]; } @@ -105,11 +118,22 @@ class remove_jabber extends migration { $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); + $sql = 'SELECT id FROM ' . $this->tables['user_notifications'] . " + WHERE method = 'notification.method.jabber' + ORDER BY id ASC"; + $result = $this->db->sql_query_limit($sql, $limit, $start ?: 0); + $rowset = $this->db->sql_fetchrowset($result); + $this->db->sql_freeresult($result); + $ids_array = array_column($rowset, 'id'); - return $this->db->sql_affectedrows() < $limit ? true : $start + $limit; + if (count($ids_array)) + { + $sql = 'UPDATE ' . $this->tables['user_notifications'] . ' + SET ' . $this->db->sql_build_array('UPDATE', ['method' => 'notification.method.email']) . ' + WHERE ' . $this->db->sql_in_set('id', $ids_array); + $this->db->sql_query($sql); + } + + return count($ids_array) < $limit ? true : $start + $limit; } } diff --git a/phpBB/phpbb/db/migration/schema_generator.php b/phpBB/phpbb/db/migration/schema_generator.php index 90cfddbe5a..1153f4c67e 100644 --- a/phpBB/phpbb/db/migration/schema_generator.php +++ b/phpBB/phpbb/db/migration/schema_generator.php @@ -185,6 +185,7 @@ class schema_generator 'drop_columns' => 'COLUMNS', 'change_columns' => 'COLUMNS', 'add_index' => 'KEYS', + 'add_primary_keys' => 'PRIMARY_KEY', 'add_unique_index' => 'KEYS', 'drop_keys' => 'KEYS', 'rename_index' => 'KEYS', diff --git a/phpBB/phpbb/db/tools/doctrine.php b/phpBB/phpbb/db/tools/doctrine.php index 043252b4c5..2d8908c9bc 100644 --- a/phpBB/phpbb/db/tools/doctrine.php +++ b/phpBB/phpbb/db/tools/doctrine.php @@ -398,7 +398,7 @@ class doctrine implements tools_interface return $this->alter_schema( function (Schema $schema) use ($table_name, $column): void { - $this->schema_create_primary_key($schema, $column, $table_name); + $this->schema_create_primary_key($schema, $table_name, $column); } ); } @@ -990,16 +990,16 @@ class doctrine implements tools_interface /** * Creates primary key for a table * - * @param $column * @param Schema $schema * @param string $table_name + * @param array|string $column_name * @param bool $safe_check * * @throws SchemaException */ - protected function schema_create_primary_key(Schema $schema, $column, string $table_name, bool $safe_check = false): void + protected function schema_create_primary_key(Schema $schema, string $table_name, array|string $column_name, bool $safe_check = false): void { - $columns = (is_array($column)) ? $column : [$column]; + $columns = (is_array($column_name)) ? $column_name : [$column_name]; $table = $schema->getTable($table_name); $table->dropPrimaryKey(); $table->setPrimaryKey($columns); 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 f4b80fc5e9..284c52a07c 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -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')); + } } diff --git a/tests/notification/fixtures/email_notification.type.post.xml b/tests/notification/fixtures/email_notification.type.post.xml index 6d064141b7..11643098dd 100644 --- a/tests/notification/fixtures/email_notification.type.post.xml +++ b/tests/notification/fixtures/email_notification.type.post.xml @@ -164,12 +164,14 @@ + iditem_typeitem_iduser_idmethodnotify + notification.type.post 0 2 @@ -177,6 +179,7 @@ 1 + notification.type.post 0 3 @@ -184,6 +187,7 @@ 1 + notification.type.post 0 4 @@ -191,6 +195,7 @@ 1 + notification.type.post 0 5 @@ -198,6 +203,7 @@ 1 + notification.type.post 0 6 @@ -205,6 +211,7 @@ 1 + notification.type.post 0 7 @@ -212,6 +219,7 @@ 1 + notification.type.post 0 8 @@ -219,6 +227,7 @@ 1 + notification.type.forum 0 2 @@ -226,6 +235,7 @@ 1 + notification.type.forum 0 3 @@ -233,6 +243,7 @@ 1 + notification.type.forum 0 4 @@ -240,6 +251,7 @@ 1 + notification.type.forum 0 5 @@ -247,6 +259,7 @@ 1 + notification.type.forum 0 6 @@ -254,6 +267,7 @@ 1 + notification.type.forum 0 7 @@ -261,6 +275,7 @@ 1 + notification.type.forum 0 8 diff --git a/tests/notification/fixtures/submit_post_notification.type.bookmark.xml b/tests/notification/fixtures/submit_post_notification.type.bookmark.xml index b6163e9ed0..5af7663f23 100644 --- a/tests/notification/fixtures/submit_post_notification.type.bookmark.xml +++ b/tests/notification/fixtures/submit_post_notification.type.bookmark.xml @@ -119,12 +119,14 @@
+ iditem_typeitem_iduser_idmethodnotify + notification.type.bookmark 0 2 @@ -132,6 +134,7 @@ 1 + notification.type.bookmark 0 3 @@ -139,6 +142,7 @@ 1 + notification.type.bookmark 0 4 @@ -146,6 +150,7 @@ 1 + notification.type.bookmark 0 5 @@ -153,6 +158,7 @@ 1 + notification.type.bookmark 0 6 @@ -160,6 +166,7 @@ 0 + notification.type.bookmark 0 3 diff --git a/tests/notification/fixtures/submit_post_notification.type.forum.xml b/tests/notification/fixtures/submit_post_notification.type.forum.xml index b8df34bedc..c152ff6dc0 100644 --- a/tests/notification/fixtures/submit_post_notification.type.forum.xml +++ b/tests/notification/fixtures/submit_post_notification.type.forum.xml @@ -155,12 +155,14 @@
+ iditem_typeitem_iduser_idmethodnotify + notification.type.post 0 2 @@ -168,6 +170,7 @@ 1 + notification.type.post 0 3 @@ -175,6 +178,7 @@ 1 + notification.type.post 0 4 @@ -182,6 +186,7 @@ 1 + notification.type.post 0 5 @@ -189,6 +194,7 @@ 1 + notification.type.post 0 6 @@ -196,6 +202,7 @@ 1 + notification.type.post 0 7 @@ -203,6 +210,7 @@ 1 + notification.type.post 0 8 @@ -210,6 +218,7 @@ 1 + notification.type.forum 0 2 @@ -217,6 +226,7 @@ 1 + notification.type.forum 0 3 @@ -224,6 +234,7 @@ 1 + notification.type.forum 0 4 @@ -231,6 +242,7 @@ 1 + notification.type.forum 0 5 @@ -238,6 +250,7 @@ 1 + notification.type.forum 0 6 @@ -245,6 +258,7 @@ 1 + notification.type.forum 0 7 @@ -252,6 +266,7 @@ 1 + notification.type.forum 0 8 diff --git a/tests/notification/fixtures/submit_post_notification.type.mention.xml b/tests/notification/fixtures/submit_post_notification.type.mention.xml index 86ae1fd037..581472848d 100644 --- a/tests/notification/fixtures/submit_post_notification.type.mention.xml +++ b/tests/notification/fixtures/submit_post_notification.type.mention.xml @@ -136,12 +136,14 @@
+ iditem_typeitem_iduser_idmethodnotify + notification.type.mention 0 2 @@ -149,6 +151,7 @@ 1 + notification.type.mention 0 3 @@ -156,6 +159,7 @@ 1 + notification.type.mention 0 4 @@ -163,6 +167,7 @@ 1 + notification.type.mention 0 5 @@ -170,6 +175,7 @@ 1 + notification.type.mention 0 6 @@ -177,6 +183,7 @@ 0 + notification.type.mention 0 8 diff --git a/tests/notification/fixtures/submit_post_notification.type.post.xml b/tests/notification/fixtures/submit_post_notification.type.post.xml index 75bfb03e89..0c11b2bb5d 100644 --- a/tests/notification/fixtures/submit_post_notification.type.post.xml +++ b/tests/notification/fixtures/submit_post_notification.type.post.xml @@ -155,12 +155,14 @@
+ iditem_typeitem_iduser_idmethodnotify + notification.type.post 0 2 @@ -168,6 +170,7 @@ 1 + notification.type.post 0 3 @@ -175,6 +178,7 @@ 1 + notification.type.post 0 4 @@ -182,6 +186,7 @@ 1 + notification.type.post 0 5 @@ -189,6 +194,7 @@ 1 + notification.type.post 0 6 @@ -196,6 +202,7 @@ 1 + notification.type.post 0 7 @@ -203,6 +210,7 @@ 1 + notification.type.post 0 8 @@ -210,6 +218,7 @@ 1 + notification.type.forum 0 2 @@ -217,6 +226,7 @@ 1 + notification.type.forum 0 3 @@ -224,6 +234,7 @@ 1 + notification.type.forum 0 4 @@ -231,6 +242,7 @@ 1 + notification.type.forum 0 5 @@ -238,6 +250,7 @@ 1 + notification.type.forum 0 6 @@ -245,6 +258,7 @@ 1 + notification.type.forum 0 7 @@ -252,6 +266,7 @@ 1 + notification.type.forum 0 8 diff --git a/tests/notification/fixtures/submit_post_notification.type.post_in_queue.xml b/tests/notification/fixtures/submit_post_notification.type.post_in_queue.xml index 12e73b0ff2..fdb5152a44 100644 --- a/tests/notification/fixtures/submit_post_notification.type.post_in_queue.xml +++ b/tests/notification/fixtures/submit_post_notification.type.post_in_queue.xml @@ -103,12 +103,14 @@
+ iditem_typeitem_iduser_idmethodnotify + notification.type.needs_approval 0 2 @@ -116,6 +118,7 @@ 1 + notification.type.needs_approval 0 3 @@ -123,6 +126,7 @@ 1 + notification.type.needs_approval 0 4 @@ -130,6 +134,7 @@ 1 + notification.type.needs_approval 0 5 @@ -137,6 +142,7 @@ 1 + notification.type.needs_approval 0 6 @@ -144,6 +150,7 @@ 1 + notification.type.needs_approval 0 7 @@ -151,6 +158,7 @@ 0 + notification.type.needs_approval 0 9 diff --git a/tests/notification/fixtures/submit_post_notification.type.quote.xml b/tests/notification/fixtures/submit_post_notification.type.quote.xml index 9f4ba91475..bb1c26ffcb 100644 --- a/tests/notification/fixtures/submit_post_notification.type.quote.xml +++ b/tests/notification/fixtures/submit_post_notification.type.quote.xml @@ -91,12 +91,14 @@
+ iditem_typeitem_iduser_idmethodnotify + notification.type.quote 0 2 @@ -104,6 +106,7 @@ 1 + notification.type.quote 0 3 @@ -111,6 +114,7 @@ 1 + notification.type.quote 0 4 @@ -118,6 +122,7 @@ 1 + notification.type.quote 0 5 @@ -125,6 +130,7 @@ 1 + notification.type.quote 0 6 diff --git a/tests/notification/fixtures/submit_post_notification.type.topic.xml b/tests/notification/fixtures/submit_post_notification.type.topic.xml index 1f96ed2ee7..c16afde110 100644 --- a/tests/notification/fixtures/submit_post_notification.type.topic.xml +++ b/tests/notification/fixtures/submit_post_notification.type.topic.xml @@ -99,12 +99,14 @@
+ iditem_typeitem_iduser_idmethodnotify + notification.type.topic 0 2 @@ -112,6 +114,7 @@ 1 + notification.type.topic 0 6 @@ -119,6 +122,7 @@ 1 + notification.type.topic 0 7 @@ -126,6 +130,7 @@ 1 + notification.type.topic 0 8 diff --git a/tests/notification/fixtures/webpush_notification.type.post.xml b/tests/notification/fixtures/webpush_notification.type.post.xml index d59d5b92c4..5b56f13b09 100644 --- a/tests/notification/fixtures/webpush_notification.type.post.xml +++ b/tests/notification/fixtures/webpush_notification.type.post.xml @@ -185,12 +185,14 @@
+ iditem_typeitem_iduser_idmethodnotify + notification.type.post 0 2 @@ -198,6 +200,7 @@ 1 + notification.type.post 0 3 @@ -205,6 +208,7 @@ 1 + notification.type.post 0 4 @@ -212,6 +216,7 @@ 1 + notification.type.post 0 5 @@ -219,6 +224,7 @@ 1 + notification.type.post 0 6 @@ -226,6 +232,7 @@ 1 + notification.type.post 0 7 @@ -233,6 +240,7 @@ 1 + notification.type.post 0 8 @@ -240,6 +248,7 @@ 1 + notification.type.forum 0 2 @@ -247,6 +256,7 @@ 1 + notification.type.forum 0 3 @@ -254,6 +264,7 @@ 1 + notification.type.forum 0 4 @@ -261,6 +272,7 @@ 1 + notification.type.forum 0 5 @@ -268,6 +280,7 @@ 1 + notification.type.forum 0 6 @@ -275,6 +288,7 @@ 1 + notification.type.forum 0 7 @@ -282,6 +296,7 @@ 1 + notification.type.forum 0 8