Skip to content

Commit

Permalink
Merge pull request #6726 from morozov/remove-incomplete-sqlite-fk-int…
Browse files Browse the repository at this point in the history
…rospection

Do not introspect incomplete SQLite foreign key constraint
  • Loading branch information
morozov authored Jan 17, 2025
2 parents b76f7d8 + 416afe8 commit 1820ce1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ awareness about deprecated code.

# Upgrade to 5.0

## BC BREAK: removed support for introspection of SQLite foreign key constraints with omitted referenced column names in an incomplete schema

If the SQLite schema manager fails to introspect the columns referenced by a foreign key constraint, instead of
producing a constraint with empty referenced columns, it will throw an exception.

## BC BREAK: removed `Table::columnsAreIndexed()`

The `Table::columnsAreIndexed()` method has been removed.
Expand Down
26 changes: 26 additions & 0 deletions src/Schema/Exception/UnsupportedSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Schema\Exception;

use Doctrine\DBAL\Schema\SchemaException;
use RuntimeException;

use function sprintf;

/** @psalm-immutable */
final class UnsupportedSchema extends RuntimeException implements SchemaException
{
public static function sqliteMissingForeignKeyConstraintReferencedColumns(
string $constraintTableName,
string $referencingTableName,
): self {
return new self(sprintf(
'Unable to introspect foreign key constraint "%s" because the referenced column names are omitted,'
. ' and the referenced table "%s" does not exist.',
$constraintTableName,
$referencingTableName,
));
}
}
12 changes: 4 additions & 8 deletions src/Schema/SQLiteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use Doctrine\DBAL\Platforms\SQLite;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\Exception\UnsupportedSchema;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\TextType;
use Doctrine\DBAL\Types\Type;
use Doctrine\Deprecations\Deprecation;

use function array_change_key_case;
use function array_map;
Expand Down Expand Up @@ -339,14 +339,10 @@ protected function _getPortableTableForeignKeysList(array $tableForeignKeys): ar
$foreignTableIndexes = $this->_getPortableTableIndexesList([], $value['foreignTable']);

if (! isset($foreignTableIndexes['primary'])) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/6701',
'Introspection of SQLite foreign key constraints with omitted referenced column names'
. ' in an incomplete schema is deprecated.',
throw UnsupportedSchema::sqliteMissingForeignKeyConstraintReferencedColumns(
$value['name'],
$value['foreignTable'],
);

continue;
}

$list[$id]['foreign'] = $foreignTableIndexes['primary']->getColumns();
Expand Down
20 changes: 3 additions & 17 deletions tests/Functional/Schema/SQLiteSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Exception\UnsupportedSchema;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;

use function array_keys;
use function array_shift;

class SQLiteSchemaManagerTest extends SchemaManagerFunctionalTestCase
{
use VerifyDeprecations;

protected function supportsPlatform(AbstractPlatform $platform): bool
{
return $platform instanceof SQLitePlatform;
Expand Down Expand Up @@ -75,8 +73,6 @@ public function testListForeignKeysFromExistingDatabase(): void
),
];

$this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6701');

self::assertEquals($expected, $this->schemaManager->listTableForeignKeys('user'));
}

Expand All @@ -91,19 +87,9 @@ public function testListForeignKeysWithImplicitColumnsFromIncompleteSchema(): vo
)
EOS);

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6701');

$expected = [
new ForeignKeyConstraint(
['t2_id'],
't2',
[],
'',
['onUpdate' => 'NO ACTION', 'onDelete' => 'NO ACTION', 'deferrable' => false, 'deferred' => false],
),
];
$this->expectException(UnsupportedSchema::class);

self::assertEquals($expected, $this->schemaManager->listTableForeignKeys('t1'));
$this->schemaManager->listTableForeignKeys('t1');
}

public function testColumnCollation(): void
Expand Down

0 comments on commit 1820ce1

Please sign in to comment.