Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove support for invalid auto-increment column definitions on SQLite #6853

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ awareness about deprecated code.

# Upgrade to 5.0

## BC BREAK: removed support for invalid auto-increment column definitions on SQLite

The following auto-increment column definitions are no longer supported on SQLite:

1. An auto-increment column that is not a primary key.
2. An auto-increment column that is part of a composite primary key.

## BC BREAK: removed `TableDiff` methods

The `TableDiff::getModifiedForeignKeys()` and `TableDiff::getModifiedIndexes()` methods have been removed.
Expand Down
29 changes: 29 additions & 0 deletions src/Platforms/Exception/UnsupportedTableDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Platforms\Exception;

use Doctrine\DBAL\Schema\Name\UnqualifiedName;
use InvalidArgumentException;

use function sprintf;

final class UnsupportedTableDefinition extends InvalidArgumentException implements PlatformException
{
public static function autoIncrementColumnNotPartOfPrimaryKey(UnqualifiedName $columnName): self
{
return new self(sprintf(
'Auto-increment column %s must be a primary key column.',
$columnName->toString(),
));
}

public static function autoIncrementColumnPartOfCompositePrimaryKey(UnqualifiedName $columnName): self
{
return new self(sprintf(
'Auto-increment column %s cannot be part of a composite primary key.',
$columnName->toString(),
));
}
}
18 changes: 6 additions & 12 deletions src/Platforms/SQLitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\Exception\NotSupported;
use Doctrine\DBAL\Platforms\Exception\UnsupportedTableDefinition;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Exception\ColumnDoesNotExist;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
Expand All @@ -22,7 +23,6 @@
use Doctrine\DBAL\SQL\Builder\SelectSQLBuilder;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types;
use Doctrine\Deprecations\Deprecation;
use InvalidArgumentException;

use function array_combine;
Expand Down Expand Up @@ -345,17 +345,11 @@ private function hasAutoIncrementColumn(array $columns, array $parameters): bool
->toNormalizedValue($folding);

if (! isset($primaryKeyColumnNames[$columnName])) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/6849',
'Declaring a column that is not part of the primary key as auto-increment is deprecated.',
);
} elseif (count($primaryKeyColumnNames) > 1) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/6849',
'Declaring a column that is part of a composite primary key as auto-increment is deprecated.',
);
throw UnsupportedTableDefinition::autoIncrementColumnNotPartOfPrimaryKey($column['name']);
}

if (count($primaryKeyColumnNames) > 1) {
throw UnsupportedTableDefinition::autoIncrementColumnPartOfCompositePrimaryKey($column['name']);
}

return true;
Expand Down
8 changes: 3 additions & 5 deletions tests/Platforms/SQLitePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\Exception\UnsupportedTableDefinition;
use Doctrine\DBAL\Platforms\SQLite;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Schema\Column;
Expand All @@ -18,15 +19,12 @@
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;

use function implode;

/** @extends AbstractPlatformTestCase<SQLitePlatform> */
class SQLitePlatformTest extends AbstractPlatformTestCase
{
use VerifyDeprecations;

public function createPlatform(): AbstractPlatform
{
return new SQLitePlatform();
Expand Down Expand Up @@ -646,7 +644,7 @@ public function testCreateTableWithNonPrimaryKeyAutoIncrementColumn(): void
$table = new Table('test_autoincrement');
$table->addColumn('id', Types::INTEGER, ['autoincrement' => true]);

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6849');
$this->expectException(UnsupportedTableDefinition::class);
$this->platform->getCreateTableSQL($table);
}

Expand All @@ -657,7 +655,7 @@ public function testCreateTableWithCompositePrimaryKeyAutoIncrementColumn(): voi
$table->addColumn('id2', Types::INTEGER);
$table->setPrimaryKey(['id1', 'id2']);

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6849');
$this->expectException(UnsupportedTableDefinition::class);
$this->platform->getCreateTableSQL($table);
}
}