Skip to content

Commit 10ed011

Browse files
authored
Merge pull request #6958 from morozov/preserve-pk-non-clustered
Preserve the non-clustered property of the PK constraint
2 parents 4a6ee92 + 16d7ee3 commit 10ed011

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/Schema/Table.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use function array_map;
2727
use function array_merge;
2828
use function array_values;
29+
use function assert;
2930
use function count;
3031
use function implode;
3132
use function in_array;
@@ -200,6 +201,13 @@ public function addPrimaryKeyConstraint(PrimaryKeyConstraint $primaryKeyConstrai
200201
$primaryKeyConstraint->getObjectName()?->toString(),
201202
);
202203

204+
// there is no way to set a primary index with flags. we have to set it and then add the flag
205+
if (! $primaryKeyConstraint->isClustered()) {
206+
$index = $this->getPrimaryKey();
207+
assert($index !== null);
208+
$index->addFlag('nonclustered');
209+
}
210+
203211
$this->primaryKeyConstraint = $primaryKeyConstraint;
204212

205213
return $this;

tests/Platforms/SQLServerPlatformTest.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Doctrine\DBAL\Schema\Comparator;
1616
use Doctrine\DBAL\Schema\ComparatorConfig;
1717
use Doctrine\DBAL\Schema\Index;
18+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1819
use Doctrine\DBAL\Schema\Sequence;
1920
use Doctrine\DBAL\Schema\Table;
2021
use Doctrine\DBAL\Schema\TableDiff;
@@ -567,14 +568,21 @@ public function testCreateClusteredIndex(): void
567568

568569
public function testCreateNonClusteredPrimaryKeyInTable(): void
569570
{
570-
$table = new Table('tbl', [
571-
Column::editor()
572-
->setUnquotedName('id')
573-
->setTypeName(Types::INTEGER)
574-
->create(),
575-
]);
576-
$table->setPrimaryKey(['id']);
577-
$table->getIndex('primary')->addFlag('nonclustered');
571+
$table = Table::editor()
572+
->setUnquotedName('tbl')
573+
->setColumns(
574+
Column::editor()
575+
->setUnquotedName('id')
576+
->setTypeName(Types::INTEGER)
577+
->create(),
578+
)
579+
->setPrimaryKeyConstraint(
580+
PrimaryKeyConstraint::editor()
581+
->setUnquotedColumnNames('id')
582+
->setIsClustered(false)
583+
->create(),
584+
)
585+
->create();
578586

579587
self::assertEquals(
580588
['CREATE TABLE tbl (id INT NOT NULL, PRIMARY KEY NONCLUSTERED (id))'],

0 commit comments

Comments
 (0)