Skip to content

Commit c6ea5b1

Browse files
committed
Remove support for primary indexes
1 parent 8161346 commit c6ea5b1

14 files changed

+54
-326
lines changed

UPGRADE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ awareness about deprecated code.
88

99
# Upgrade to 5.0
1010

11+
## BC BREAK: Changes in features related to primary key constraints
12+
13+
1. The `Index` class can no longer represent a primary key constraint. As a result:
14+
1. The `Table::getIndexes()` and `AbstractSchemaManager::listTableIndexes()` methods no longer return the index that
15+
backs the primary key constraint.
16+
2. The index that backs the primary key constraint is no longer considered during implicit index management.
17+
2. The `Table::getPrimaryKey()` and `Table::setPrimaryKey()` methods have been removed.
18+
3. The `Table::renameIndex()` method can no longer be used to rename a primary key constraint.
19+
4. The `AbstractPlatform::getCreatePrimaryKeySQL()` method has been removed.
20+
5. The `PostgreSQLPlatform::getDropIndexSQL()` method no longer builds SQL for dropping a primary key constraint. As a
21+
result, dropping a primary key constraint on Postgres is now only possible if the constraint name is known (e.g. in
22+
the result of database schema introspection).
23+
1124
## BC BREAK: `INTEGER PRIMARY KEY` columns are no longer introspected as auto-incremented on SQLite
1225

1326
Even though `INTEGER PRIMARY KEY` columns are effectively auto-incremented on SQLite, DBAL no longer introspects them as

src/Platforms/AbstractMySQLPlatform.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,6 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff): array
392392

393393
$indexClause = 'INDEX ' . $addedIndex->getObjectName()->toSQL($this);
394394

395-
if ($addedIndex->isPrimary()) {
396-
continue;
397-
}
398-
399395
if ($addedIndex->isUnique()) {
400396
$indexClause = 'UNIQUE ' . $indexClause;
401397
}

src/Platforms/AbstractPlatform.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
use Doctrine\DBAL\Types\Exception\TypeNotFound;
4545
use Doctrine\DBAL\Types\Exception\TypesException;
4646
use Doctrine\DBAL\Types\Type;
47-
use Doctrine\Deprecations\Deprecation;
4847

4948
use function addcslashes;
5049
use function array_map;
@@ -842,10 +841,6 @@ private function buildCreateTableSQL(Table $table, bool $createForeignKeys): arr
842841
$parameters['foreignKeys'] = [];
843842

844843
foreach ($table->getIndexes() as $index) {
845-
if ($index->isPrimary()) {
846-
continue;
847-
}
848-
849844
$parameters['indexes'][] = $index;
850845
}
851846

@@ -1112,10 +1107,6 @@ public function getCreateIndexSQL(Index $index, string $table): string
11121107
));
11131108
}
11141109

1115-
if ($index->isPrimary()) {
1116-
return $this->getCreatePrimaryKeySQL($index, $table);
1117-
}
1118-
11191110
$query = 'CREATE ' . $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name . ' ON ' . $table;
11201111
$query .= ' (' . implode(', ', $index->getQuotedColumns($this)) . ')' . $this->getPartialIndexSQL($index);
11211112

@@ -1142,23 +1133,6 @@ protected function getCreateIndexSQLFlags(Index $index): string
11421133
return $index->isUnique() ? 'UNIQUE ' : '';
11431134
}
11441135

1145-
/**
1146-
* Returns the SQL to create an unnamed primary key constraint.
1147-
*
1148-
* @deprecated
1149-
*/
1150-
public function getCreatePrimaryKeySQL(Index $index, string $table): string
1151-
{
1152-
Deprecation::triggerIfCalledFromOutside(
1153-
'doctrine/dbal',
1154-
'https://github.com/doctrine/dbal/pull/6867',
1155-
'%s() is deprecated.',
1156-
__METHOD__,
1157-
);
1158-
1159-
return 'ALTER TABLE ' . $table . ' ADD PRIMARY KEY (' . implode(', ', $index->getQuotedColumns($this)) . ')';
1160-
}
1161-
11621136
/**
11631137
* Returns the SQL to create a named schema.
11641138
*/

src/Platforms/OraclePlatform.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use Doctrine\DBAL\TransactionIsolationLevel;
2525
use Doctrine\DBAL\Types\BinaryType;
2626
use Doctrine\DBAL\Types\Types;
27-
use Doctrine\Deprecations\Deprecation;
2827
use InvalidArgumentException;
2928

3029
use function array_merge;
@@ -134,20 +133,6 @@ public function getBitOrComparisonExpression(string $value1, string $value2): st
134133
. '+' . $value2 . ')';
135134
}
136135

137-
/** @deprecated */
138-
public function getCreatePrimaryKeySQL(Index $index, string $table): string
139-
{
140-
Deprecation::triggerIfCalledFromOutside(
141-
'doctrine/dbal',
142-
'https://github.com/doctrine/dbal/pull/6867',
143-
'%s() is deprecated.',
144-
__METHOD__,
145-
);
146-
147-
return 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $index->getObjectName()->toSQL($this)
148-
. ' PRIMARY KEY (' . implode(', ', $index->getQuotedColumns($this)) . ')';
149-
}
150-
151136
/**
152137
* {@inheritDoc}
153138
*

src/Platforms/PostgreSQLPlatform.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Doctrine\DBAL\Schema\TableDiff;
2020
use Doctrine\DBAL\TransactionIsolationLevel;
2121
use Doctrine\DBAL\Types\Types;
22-
use Doctrine\Deprecations\Deprecation;
2322
use UnexpectedValueException;
2423

2524
use function array_merge;
@@ -32,9 +31,7 @@
3231
use function is_string;
3332
use function sprintf;
3433
use function str_contains;
35-
use function str_ends_with;
3634
use function strtolower;
37-
use function substr;
3835
use function trim;
3936

4037
/**
@@ -385,24 +382,6 @@ public function getDropForeignKeySQL(string $foreignKey, string $table): string
385382

386383
public function getDropIndexSQL(string $name, string $table): string
387384
{
388-
if (str_ends_with($table, '"')) {
389-
$primaryKeyName = substr($table, 0, -1) . '_pkey"';
390-
} else {
391-
$primaryKeyName = $table . '_pkey';
392-
}
393-
394-
if ($name === '"primary"' || $name === $primaryKeyName) {
395-
Deprecation::triggerIfCalledFromOutside(
396-
'doctrine/dbal',
397-
'https://github.com/doctrine/dbal/pull/6867',
398-
'Building the SQL for dropping primary key constraint via %s() is deprecated. Use'
399-
. ' getDropConstraintSQL() instead.',
400-
__METHOD__,
401-
);
402-
403-
return $this->getDropConstraintSQL($primaryKeyName, $table);
404-
}
405-
406385
if (str_contains($table, '.')) {
407386
[$schema] = explode('.', $table);
408387
$name = $schema . '.' . $name;

src/Platforms/SQLServerPlatform.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Doctrine\DBAL\SQL\Builder\SelectSQLBuilder;
2424
use Doctrine\DBAL\TransactionIsolationLevel;
2525
use Doctrine\DBAL\Types\Types;
26-
use Doctrine\Deprecations\Deprecation;
2726
use InvalidArgumentException;
2827

2928
use function array_map;
@@ -240,25 +239,6 @@ protected function _getCreateTableSQL(OptionallyQualifiedName $tableName, array
240239
return array_merge($sql, $commentsSql, $defaultConstraintsSql);
241240
}
242241

243-
/** @deprecated */
244-
public function getCreatePrimaryKeySQL(Index $index, string $table): string
245-
{
246-
Deprecation::triggerIfCalledFromOutside(
247-
'doctrine/dbal',
248-
'https://github.com/doctrine/dbal/pull/6867',
249-
'%s() is deprecated.',
250-
__METHOD__,
251-
);
252-
253-
$sql = 'ALTER TABLE ' . $table . ' ADD PRIMARY KEY';
254-
255-
if ($index->hasFlag('nonclustered')) {
256-
$sql .= ' NONCLUSTERED';
257-
}
258-
259-
return $sql . ' (' . implode(', ', $index->getQuotedColumns($this)) . ')';
260-
}
261-
262242
private function unquoteSingleIdentifier(string $possiblyQuotedName): string
263243
{
264244
return str_starts_with($possiblyQuotedName, '[') && str_ends_with($possiblyQuotedName, ']')

src/Platforms/SQLitePlatform.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,6 @@ protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff): array
502502
$sql = [];
503503

504504
foreach ($this->getIndexesInAlteredTable($diff) as $index) {
505-
if ($index->isPrimary()) {
506-
continue;
507-
}
508-
509505
$sql[] = $this->getCreateIndexSQL($index, $table->getObjectName()->toSQL($this));
510506
}
511507

@@ -562,10 +558,6 @@ public function getCreateIndexSQL(Index $index, string $table): string
562558
));
563559
}
564560

565-
if ($index->isPrimary()) {
566-
return $this->getCreatePrimaryKeySQL($index, $table);
567-
}
568-
569561
if (strpos($table, '.') !== false) {
570562
[$schema, $table] = explode('.', $table);
571563
$name = $schema . '.' . $name;
@@ -591,12 +583,6 @@ public function getDropTablesSQL(array $tables): array
591583
return $sql;
592584
}
593585

594-
/** @deprecated */
595-
public function getCreatePrimaryKeySQL(Index $index, string $table): string
596-
{
597-
throw NotSupported::new(__METHOD__);
598-
}
599-
600586
public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, string $table): string
601587
{
602588
throw NotSupported::new(__METHOD__);
@@ -850,7 +836,7 @@ private function getIndexesInAlteredTable(TableDiff $diff): array
850836
$index->getName(),
851837
$indexColumns,
852838
$index->isUnique(),
853-
$index->isPrimary(),
839+
false,
854840
$index->getFlags(),
855841
);
856842
}

src/Schema/Comparator.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,6 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff
208208

209209
// See if all the indexes from the old table exist in the new one
210210
foreach ($newIndexes as $newIndexName => $newIndex) {
211-
if ($newIndex->isPrimary()) {
212-
continue;
213-
}
214-
215211
if ($oldTable->hasIndex($newIndexName)) {
216212
continue;
217213
}
@@ -221,10 +217,6 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff
221217

222218
// See if there are any removed indexes in the new table
223219
foreach ($oldIndexes as $oldIndexName => $oldIndex) {
224-
if ($oldIndex->isPrimary()) {
225-
continue;
226-
}
227-
228220
if (! $newTable->hasIndex($oldIndexName)) {
229221
$droppedIndexes[$oldIndexName] = $oldIndex;
230222

src/Schema/Exception/InvalidIndexDefinition.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Doctrine\DBAL\Schema\Exception;
66

7-
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
87
use Doctrine\DBAL\Schema\SchemaException;
98
use LogicException;
109

@@ -19,24 +18,16 @@ public static function columnNamesNotSet(): self
1918
return new self('Index column names are not set.');
2019
}
2120

22-
public static function primaryKeyIndexHasColumnLengths(): self
23-
{
24-
return new self('Primary key index cannot have column lengths.');
25-
}
26-
27-
public static function primaryKeyIndexOnANullableColumn(UnqualifiedName $columnName): self
28-
{
29-
return new self(sprintf(
30-
'Primary key index cannot use nullable column %s.',
31-
$columnName->toString(),
32-
));
33-
}
34-
3521
public static function invalidColumnLength(mixed $length): self
3622
{
3723
return new self(sprintf(
3824
'Indexed column length must be an integer, %s given.',
3925
is_object($length) ? $length::class : gettype($length),
4026
));
4127
}
28+
29+
public static function fromPrimaryIndex(): self
30+
{
31+
return new self('Primary indexes are not supported.');
32+
}
4233
}

0 commit comments

Comments
 (0)