Skip to content

Commit

Permalink
Remove support for foreign key referenced table as asset
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Sep 17, 2021
1 parent 7195d50 commit 0cdabe7
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 69 deletions.
15 changes: 4 additions & 11 deletions src/Schema/ForeignKeyConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ class ForeignKeyConstraint extends AbstractAsset implements Constraint

/**
* Table or asset identifier instance of the referenced table name the foreign key constraint is associated with.
*
* @var Table|Identifier
*/
protected $_foreignTableName;
protected Identifier $_foreignTableName;

/**
* Asset identifier instances of the referenced table column names the foreign key constraint is associated with.
Expand All @@ -50,27 +48,22 @@ class ForeignKeyConstraint extends AbstractAsset implements Constraint
* Initializes the foreign key constraint.
*
* @param array<int, string> $localColumnNames Names of the referencing table columns.
* @param Table|string $foreignTableName Referenced table.
* @param string $foreignTableName Referenced table.
* @param array<int, string> $foreignColumnNames Names of the referenced table columns.
* @param string $name Name of the foreign key constraint.
* @param array<string, mixed> $options Options associated with the foreign key constraint.
*/
public function __construct(
array $localColumnNames,
$foreignTableName,
string $foreignTableName,
array $foreignColumnNames,
string $name = '',
array $options = []
) {
$this->_setName($name);

$this->_localColumnNames = $this->createIdentifierMap($localColumnNames);

if ($foreignTableName instanceof Table) {
$this->_foreignTableName = $foreignTableName;
} else {
$this->_foreignTableName = new Identifier($foreignTableName);
}
$this->_foreignTableName = new Identifier($foreignTableName);

$this->_foreignColumnNames = $this->createIdentifierMap($foreignColumnNames);
$this->_options = $options;
Expand Down
13 changes: 2 additions & 11 deletions src/Schema/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,14 @@ public function dropColumn(string $name): self
*
* Name is inferred from the local columns.
*
* @param Table|string $foreignTable Table schema instance or table name
* @param array<int, string> $localColumnNames
* @param array<int, string> $foreignColumnNames
* @param array<string, mixed> $options
*
* @throws SchemaException
*/
public function addForeignKeyConstraint(
$foreignTable,
string $foreignTableName,
array $localColumnNames,
array $foreignColumnNames,
array $options = [],
Expand All @@ -351,14 +350,6 @@ public function addForeignKeyConstraint(
);
}

if ($foreignTable instanceof Table) {
foreach ($foreignColumnNames as $columnName) {
if (! $foreignTable->hasColumn($columnName)) {
throw ColumnDoesNotExist::new($columnName, $foreignTable->getName());
}
}
}

foreach ($localColumnNames as $columnName) {
if (! $this->hasColumn($columnName)) {
throw ColumnDoesNotExist::new($columnName, $this->_name);
Expand All @@ -367,7 +358,7 @@ public function addForeignKeyConstraint(

$constraint = new ForeignKeyConstraint(
$localColumnNames,
$foreignTable,
$foreignTableName,
$foreignColumnNames,
$name,
$options
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/ForeignKeyExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function setUp(): void
$owningTable->addColumn('id', 'integer', []);
$owningTable->addColumn('constraint_id', 'integer', []);
$owningTable->setPrimaryKey(['id']);
$owningTable->addForeignKeyConstraint($table, ['constraint_id'], ['id']);
$owningTable->addForeignKeyConstraint($table->getName(), ['constraint_id'], ['id']);

$schemaManager->createTable($table);
$schemaManager->createTable($owningTable);
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/Schema/PostgreSQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function testTableWithSchema(): void
$column = $nestedSchemaTable->addColumn('id', 'integer');
$column->setAutoincrement(true);
$nestedSchemaTable->setPrimaryKey(['id']);
$nestedSchemaTable->addForeignKeyConstraint($nestedRelatedTable, ['id'], ['id']);
$nestedSchemaTable->addForeignKeyConstraint($nestedRelatedTable->getName(), ['id'], ['id']);

$this->schemaManager->createTable($nestedRelatedTable);
$this->schemaManager->createTable($nestedSchemaTable);
Expand Down Expand Up @@ -309,7 +309,7 @@ public function testListQuotedTable(): void
$offlineTable->addColumn('username', 'string');
$offlineTable->addColumn('fk', 'integer');
$offlineTable->setPrimaryKey(['id']);
$offlineTable->addForeignKeyConstraint($offlineTable, ['fk'], ['id']);
$offlineTable->addForeignKeyConstraint($offlineTable->getName(), ['fk'], ['id']);

$this->schemaManager->dropAndCreateTable($offlineTable);

Expand Down
10 changes: 5 additions & 5 deletions tests/Platforms/AbstractPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ public function testQuotedColumnInForeignKeyPropagation(): void
$foreignTable->addColumn('`foo-bar`', 'string');

$table->addForeignKeyConstraint(
$foreignTable,
$foreignTable->getQuotedName($this->platform),
['create', 'foo', '`bar`'],
['create', 'bar', '`foo-bar`'],
[],
Expand All @@ -685,7 +685,7 @@ public function testQuotedColumnInForeignKeyPropagation(): void
$foreignTable->addColumn('`foo-bar`', 'string');

$table->addForeignKeyConstraint(
$foreignTable,
$foreignTable->getQuotedName($this->platform),
['create', 'foo', '`bar`'],
['create', 'bar', '`foo-bar`'],
[],
Expand All @@ -705,7 +705,7 @@ public function testQuotedColumnInForeignKeyPropagation(): void
$foreignTable->addColumn('`foo-bar`', 'string');

$table->addForeignKeyConstraint(
$foreignTable,
$foreignTable->getQuotedName($this->platform),
['create', 'foo', '`bar`'],
['create', 'bar', '`foo-bar`'],
[],
Expand Down Expand Up @@ -1468,8 +1468,8 @@ public function testGeneratesAlterTableRenameIndexUsedByForeignKeySQL(): void
$primaryTable->addColumn('baz', 'integer');
$primaryTable->addIndex(['foo'], 'idx_foo');
$primaryTable->addIndex(['bar'], 'idx_bar');
$primaryTable->addForeignKeyConstraint($foreignTable, ['foo'], ['id'], [], 'fk_foo');
$primaryTable->addForeignKeyConstraint($foreignTable, ['bar'], ['id'], [], 'fk_bar');
$primaryTable->addForeignKeyConstraint($foreignTable->getName(), ['foo'], ['id'], [], 'fk_foo');
$primaryTable->addForeignKeyConstraint($foreignTable->getName(), ['bar'], ['id'], [], 'fk_bar');

$tableDiff = new TableDiff('mytable');
$tableDiff->fromTable = $primaryTable;
Expand Down
16 changes: 8 additions & 8 deletions tests/Schema/ComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ public function testTableAddForeignKey(): void

$table2 = new Table('foo');
$table2->addColumn('fk', 'integer');
$table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id']);
$table2->addForeignKeyConstraint($tableForeign->getName(), ['fk'], ['id']);

$tableDiff = $this->comparator->diffTable($table1, $table2);

Expand All @@ -544,7 +544,7 @@ public function testTableRemoveForeignKey(): void

$table2 = new Table('foo');
$table2->addColumn('fk', 'integer');
$table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id']);
$table2->addForeignKeyConstraint($tableForeign->getName(), ['fk'], ['id']);

$tableDiff = $this->comparator->diffTable($table2, $table1);

Expand All @@ -559,11 +559,11 @@ public function testTableUpdateForeignKey(): void

$table1 = new Table('foo');
$table1->addColumn('fk', 'integer');
$table1->addForeignKeyConstraint($tableForeign, ['fk'], ['id']);
$table1->addForeignKeyConstraint($tableForeign->getName(), ['fk'], ['id']);

$table2 = new Table('foo');
$table2->addColumn('fk', 'integer');
$table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id'], ['onUpdate' => 'CASCADE']);
$table2->addForeignKeyConstraint($tableForeign->getName(), ['fk'], ['id'], ['onUpdate' => 'CASCADE']);

$tableDiff = $this->comparator->diffTable($table1, $table2);

Expand All @@ -581,11 +581,11 @@ public function testMovedForeignKeyForeignTable(): void

$table1 = new Table('foo');
$table1->addColumn('fk', 'integer');
$table1->addForeignKeyConstraint($tableForeign, ['fk'], ['id']);
$table1->addForeignKeyConstraint($tableForeign->getName(), ['fk'], ['id']);

$table2 = new Table('foo');
$table2->addColumn('fk', 'integer');
$table2->addForeignKeyConstraint($tableForeign2, ['fk'], ['id']);
$table2->addForeignKeyConstraint($tableForeign2->getName(), ['fk'], ['id']);

$tableDiff = $this->comparator->diffTable($table1, $table2);

Expand Down Expand Up @@ -982,8 +982,8 @@ public function testAvoidMultipleDropForeignKey(): void
$tableC->addColumn('table_a_id', 'integer');
$tableC->addColumn('table_b_id', 'integer');

$tableC->addForeignKeyConstraint($tableA, ['table_a_id'], ['id']);
$tableC->addForeignKeyConstraint($tableB, ['table_b_id'], ['id']);
$tableC->addForeignKeyConstraint($tableA->getName(), ['table_a_id'], ['id']);
$tableC->addForeignKeyConstraint($tableB->getName(), ['table_b_id'], ['id']);

$newSchema = new Schema();

Expand Down
11 changes: 4 additions & 7 deletions tests/Schema/ForeignKeyConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use PHPUnit\Framework\TestCase;

class ForeignKeyConstraintTest extends TestCase
Expand Down Expand Up @@ -60,12 +59,12 @@ public static function getIntersectsIndexColumnsData(): iterable
}

/**
* @param string|Table $foreignTableName
*
* @dataProvider getUnqualifiedForeignTableNameData
*/
public function testGetUnqualifiedForeignTableName($foreignTableName, string $expectedUnqualifiedTableName): void
{
public function testGetUnqualifiedForeignTableName(
string $foreignTableName,
string $expectedUnqualifiedTableName
): void {
$foreignKey = new ForeignKeyConstraint(['foo', 'bar'], $foreignTableName, ['fk_foo', 'fk_bar']);

self::assertSame($expectedUnqualifiedTableName, $foreignKey->getUnqualifiedForeignTableName());
Expand All @@ -79,8 +78,6 @@ public static function getUnqualifiedForeignTableNameData(): iterable
return [
['schema.foreign_table', 'foreign_table'],
['foreign_table', 'foreign_table'],
[new Table('schema.foreign_table'), 'foreign_table'],
[new Table('foreign_table'), 'foreign_table'],
];
}
}
2 changes: 1 addition & 1 deletion tests/Schema/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function testDeepClone(): void
$tableB = $schema->createTable('bar');
$tableB->addColumn('id', 'integer');
$tableB->addColumn('foo_id', 'integer');
$tableB->addForeignKeyConstraint($tableA, ['foo_id'], ['id']);
$tableB->addForeignKeyConstraint($tableA->getName(), ['foo_id'], ['id']);

$schemaNew = clone $schema;

Expand Down
31 changes: 9 additions & 22 deletions tests/Schema/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,7 @@ public function testAddForeignKeyConstraintUnknownLocalColumnThrowsException():
$foreignTable = new Table('bar');
$foreignTable->addColumn('id', 'integer');

$table->addForeignKeyConstraint($foreignTable, ['foo'], ['id']);
}

public function testAddForeignKeyConstraintUnknownForeignColumnThrowsException(): void
{
$this->expectException(SchemaException::class);

$table = new Table('foo');
$table->addColumn('id', 'integer');

$foreignTable = new Table('bar');
$foreignTable->addColumn('id', 'integer');

$table->addForeignKeyConstraint($foreignTable, ['id'], ['foo']);
$table->addForeignKeyConstraint($foreignTable->getName(), ['foo'], ['id']);
}

public function testAddForeignKeyConstraint(): void
Expand All @@ -320,7 +307,7 @@ public function testAddForeignKeyConstraint(): void
$foreignTable = new Table('bar');
$foreignTable->addColumn('id', 'integer');

$table->addForeignKeyConstraint($foreignTable, ['id'], ['id'], ['foo' => 'bar']);
$table->addForeignKeyConstraint($foreignTable->getName(), ['id'], ['id'], ['foo' => 'bar']);

$constraints = $table->getForeignKeys();
self::assertCount(1, $constraints);
Expand Down Expand Up @@ -373,7 +360,7 @@ public function testAddForeignKeyIndexImplicitly(): void
$foreignTable = new Table('bar');
$foreignTable->addColumn('id', 'integer');

$table->addForeignKeyConstraint($foreignTable, ['id'], ['id'], ['foo' => 'bar']);
$table->addForeignKeyConstraint($foreignTable->getName(), ['id'], ['id'], ['foo' => 'bar']);

$indexes = $table->getIndexes();
self::assertCount(1, $indexes);
Expand All @@ -392,7 +379,7 @@ public function testAddForeignKeyDoesNotCreateDuplicateIndex(): void
$foreignTable = new Table('bar');
$foreignTable->addColumn('foo', 'integer');

$table->addForeignKeyConstraint($foreignTable, ['bar'], ['foo']);
$table->addForeignKeyConstraint($foreignTable->getName(), ['bar'], ['foo']);

self::assertCount(1, $table->getIndexes());
self::assertTrue($table->hasIndex('bar_idx'));
Expand All @@ -412,7 +399,7 @@ public function testAddForeignKeyAddsImplicitIndexIfIndexColumnsDoNotSpan(): voi
$foreignTable->addColumn('foo', 'integer');
$foreignTable->addColumn('baz', 'string');

$table->addForeignKeyConstraint($foreignTable, ['bar', 'baz'], ['foo', 'baz']);
$table->addForeignKeyConstraint($foreignTable->getName(), ['bar', 'baz'], ['foo', 'baz']);

self::assertCount(3, $table->getIndexes());
self::assertTrue($table->hasIndex('composite_idx'));
Expand Down Expand Up @@ -491,7 +478,7 @@ public function testAddingFulfillingRegularIndexOverridesImplicitForeignKeyConst

$localTable = new Table('local');
$localTable->addColumn('id', 'integer');
$localTable->addForeignKeyConstraint($foreignTable, ['id'], ['id']);
$localTable->addForeignKeyConstraint($foreignTable->getName(), ['id'], ['id']);

self::assertCount(1, $localTable->getIndexes());

Expand All @@ -508,7 +495,7 @@ public function testAddingFulfillingUniqueIndexOverridesImplicitForeignKeyConstr

$localTable = new Table('local');
$localTable->addColumn('id', 'integer');
$localTable->addForeignKeyConstraint($foreignTable, ['id'], ['id']);
$localTable->addForeignKeyConstraint($foreignTable->getName(), ['id'], ['id']);

self::assertCount(1, $localTable->getIndexes());

Expand All @@ -525,7 +512,7 @@ public function testAddingFulfillingPrimaryKeyOverridesImplicitForeignKeyConstra

$localTable = new Table('local');
$localTable->addColumn('id', 'integer');
$localTable->addForeignKeyConstraint($foreignTable, ['id'], ['id']);
$localTable->addForeignKeyConstraint($foreignTable->getName(), ['id'], ['id']);

self::assertCount(1, $localTable->getIndexes());

Expand All @@ -542,7 +529,7 @@ public function testAddingFulfillingExplicitIndexOverridingImplicitForeignKeyCon

$localTable = new Table('local');
$localTable->addColumn('id', 'integer');
$localTable->addForeignKeyConstraint($foreignTable, ['id'], ['id']);
$localTable->addForeignKeyConstraint($foreignTable->getName(), ['id'], ['id']);

self::assertCount(1, $localTable->getIndexes());
self::assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750'));
Expand Down
2 changes: 1 addition & 1 deletion tests/Schema/Visitor/SchemaSqlCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function createFixtureSchema(): Schema
$tableB->addColumn('id', 'integer');
$tableB->setPrimaryKey(['id']);

$tableA->addForeignKeyConstraint($tableB, ['bar'], ['id']);
$tableA->addForeignKeyConstraint($tableB->getName(), ['bar'], ['id']);

return $schema;
}
Expand Down

0 comments on commit 0cdabe7

Please sign in to comment.