diff --git a/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php b/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php index c1da7ab8e83..2b45883b3fa 100644 --- a/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php @@ -532,7 +532,7 @@ public function getAlterTableSQL(TableDiff $diff) ); } elseif ($hasFromComment && ! $hasComment) { $commentsSql[] = $this->getDropColumnCommentSQL($diff->name, $column->getQuotedName($this)); - } elseif ($hasComment) { + } elseif (! $hasFromComment && $hasComment) { $commentsSql[] = $this->getCreateColumnCommentSQL( $diff->name, $column->getQuotedName($this), diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php index 8b0bd707ee5..045299367b6 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php @@ -178,12 +178,13 @@ public function testColumnComments(): void $table->addColumn('create', 'integer', ['comment' => 'Doctrine 0wnz comments for reserved keyword columns!']); $table->addColumn('commented_type', 'object'); $table->addColumn('commented_type_with_comment', 'array', ['comment' => 'Doctrine array type.']); + $table->addColumn('commented_req_change_column', 'integer', ['comment' => 'Some comment', 'notnull' => true]); $table->setPrimaryKey(['id']); $this->schemaManager->createTable($table); $columns = $this->schemaManager->listTableColumns('sqlsrv_column_comment'); - self::assertCount(12, $columns); + self::assertCount(13, $columns); self::assertNull($columns['id']->getComment()); self::assertNull($columns['comment_null']->getComment()); self::assertNull($columns['comment_false']->getComment()); @@ -199,6 +200,7 @@ public function testColumnComments(): void self::assertEquals('Doctrine 0wnz comments for reserved keyword columns!', $columns['[create]']->getComment()); self::assertNull($columns['commented_type']->getComment()); self::assertEquals('Doctrine array type.', $columns['commented_type_with_comment']->getComment()); + self::assertEquals('Some comment', $columns['commented_req_change_column']->getComment()); $tableDiff = new TableDiff('sqlsrv_column_comment'); $tableDiff->fromTable = $table; @@ -326,13 +328,29 @@ public function testColumnComments(): void new Column('commented_type_with_comment', Type::getType('array'), ['comment' => 'Doctrine array type.']) ); + // Change column requirements without changing comment. + $tableDiff->changedColumns['commented_req_change_column'] = new ColumnDiff( + 'commented_req_change_column', + new Column( + 'commented_req_change_column', + Type::getType('integer'), + ['comment' => 'Some comment', 'notnull' => true] + ), + ['notnull'], + new Column( + 'commented_req_change_column', + Type::getType('integer'), + ['comment' => 'Some comment', 'notnull' => false] + ), + ); + $tableDiff->removedColumns['comment_integer_0'] = new Column('comment_integer_0', Type::getType('integer'), ['comment' => 0]); $this->schemaManager->alterTable($tableDiff); $columns = $this->schemaManager->listTableColumns('sqlsrv_column_comment'); - self::assertCount(23, $columns); + self::assertCount(24, $columns); self::assertEquals('primary', $columns['id']->getComment()); self::assertNull($columns['comment_null']->getComment()); self::assertEquals('false', $columns['comment_false']->getComment()); @@ -356,6 +374,7 @@ public function testColumnComments(): void self::assertEquals('666', $columns['[select]']->getComment()); self::assertNull($columns['added_commented_type']->getComment()); self::assertEquals('666', $columns['added_commented_type_with_comment']->getComment()); + self::assertEquals('Some comment', $columns['commented_req_change_column']->getComment()); } public function testPkOrdering(): void diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php index ddb7df84a00..7a63f3ffccf 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php @@ -1718,4 +1718,19 @@ private function expectCteWithMinAndMaxRowNums( . ') AS doctrine_tbl WHERE doctrine_rownum >= %d AND doctrine_rownum <= %d ORDER BY doctrine_rownum ASC'; self::assertEquals(sprintf($pattern, $expectedSql, $expectedMin, $expectedMax), $sql); } + + public function testAlterTableWithSchemaSameColumnComments(): void + { + $tableDiff = new TableDiff('testschema.mytable'); + $tableDiff->changedColumns['quota'] = new ColumnDiff( + 'quota', + new Column('quota', Type::getType('integer'), ['comment' => 'A comment', 'notnull' => true]), + ['notnull'], + new Column('quota', Type::getType('integer'), ['comment' => 'A comment', 'notnull' => false]) + ); + + $expectedSql = ['ALTER TABLE testschema.mytable ALTER COLUMN quota INT NOT NULL']; + + self::assertEquals($expectedSql, $this->platform->getAlterTableSQL($tableDiff)); + } }