Skip to content
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
20 changes: 15 additions & 5 deletions src/Schema/ForeignKeyConstraintEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ public function create(): ForeignKeyConstraint
throw InvalidForeignKeyConstraintDefinition::referencedColumnNamesNotSet($this->name);
}

$options = [];

if ($this->matchType !== MatchType::SIMPLE) {
$options['match'] = $this->matchType->value;
}

if ($this->onUpdateAction !== ReferentialAction::NO_ACTION) {
$options['onUpdate'] = $this->onUpdateAction->value;
}

if ($this->onDeleteAction !== ReferentialAction::NO_ACTION) {
$options['onDelete'] = $this->onDeleteAction->value;
}

return new ForeignKeyConstraint(
array_map(
static fn (UnqualifiedName $columnName) => $columnName->toString(),
Expand All @@ -237,11 +251,7 @@ public function create(): ForeignKeyConstraint
$this->referencedColumnNames,
),
$this->name?->toString() ?? '',
array_merge([
'match' => $this->matchType->value,
'onUpdate' => $this->onUpdateAction->value,
'onDelete' => $this->onDeleteAction->value,
], match ($this->deferrability) {
array_merge($options, match ($this->deferrability) {
Deferrability::NOT_DEFERRABLE => [],
Deferrability::DEFERRABLE => ['deferrable' => true],
Deferrability::DEFERRED => ['deferrable' => true, 'deferred' => true],
Expand Down
2 changes: 1 addition & 1 deletion tests/Platforms/AbstractMySQLPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ protected function getQuotedColumnInForeignKeySQL(): array
{
return [
'CREATE TABLE `quoted` (`create` VARCHAR(255) NOT NULL, foo VARCHAR(255) NOT NULL, '
. '`bar` VARCHAR(255) NOT NULL, INDEX IDX_22660D028FD6E0FB8C736521D79164E3 (`create`, foo, `bar`))',
. '`bar` VARCHAR(255) NOT NULL, INDEX IDX_22660D028FD6E0FB8C7365216D704F76 (`create`, foo, `bar`))',
'ALTER TABLE `quoted` ADD CONSTRAINT FK_WITH_RESERVED_KEYWORD FOREIGN KEY (`create`, foo, `bar`)'
. ' REFERENCES `foreign` (`create`, bar, `foo-bar`)',
'ALTER TABLE `quoted` ADD CONSTRAINT FK_WITH_NON_RESERVED_KEYWORD FOREIGN KEY (`create`, foo, `bar`)'
Expand Down
157 changes: 92 additions & 65 deletions tests/Platforms/AbstractPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\DBAL\Schema\ComparatorConfig;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\UniqueConstraint;
Expand Down Expand Up @@ -422,47 +423,58 @@ public function testQuotedNameInIndexSQL(): void

public function testQuotedColumnInForeignKeyPropagation(): void
{
$table = new Table('`quoted`', [
Column::editor()
->setUnquotedName('create')
->setTypeName(Types::STRING)
->setLength(255)
->create(),
Column::editor()
->setUnquotedName('foo')
->setTypeName(Types::STRING)
->setLength(255)
->create(),
Column::editor()
->setQuotedName('bar')
->setTypeName(Types::STRING)
->setLength(255)
->create(),
]);

$table->addForeignKeyConstraint(
'foreign',
['create', 'foo', '`bar`'],
['create', 'bar', '`foo-bar`'],
[],
'FK_WITH_RESERVED_KEYWORD',
);
$referencingColumnNames = [
UnqualifiedName::unquoted('create'),
UnqualifiedName::unquoted('foo'),
UnqualifiedName::quoted('bar'),
];

$table->addForeignKeyConstraint(
'foo',
['create', 'foo', '`bar`'],
['create', 'bar', '`foo-bar`'],
[],
'FK_WITH_NON_RESERVED_KEYWORD',
);
$referencedColumnNames = [
UnqualifiedName::unquoted('create'),
UnqualifiedName::unquoted('bar'),
UnqualifiedName::quoted('foo-bar'),
];

$table->addForeignKeyConstraint(
'`foo-bar`',
['create', 'foo', '`bar`'],
['create', 'bar', '`foo-bar`'],
[],
'FK_WITH_INTENDED_QUOTATION',
);
$table = Table::editor()
->setQuotedName('quoted')
->setColumns(
Column::editor()
->setUnquotedName('create')
->setTypeName(Types::STRING)
->setLength(255)
->create(),
Column::editor()
->setUnquotedName('foo')
->setTypeName(Types::STRING)
->setLength(255)
->create(),
Column::editor()
->setQuotedName('bar')
->setTypeName(Types::STRING)
->setLength(255)
->create(),
)
->setForeignKeyConstraints(
ForeignKeyConstraint::editor()
->setUnquotedName('FK_WITH_RESERVED_KEYWORD')
->setReferencingColumnNames(...$referencingColumnNames)
->setUnquotedReferencedTableName('foreign')
->setReferencedColumnNames(...$referencedColumnNames)
->create(),
ForeignKeyConstraint::editor()
->setUnquotedName('FK_WITH_NON_RESERVED_KEYWORD')
->setReferencingColumnNames(...$referencingColumnNames)
->setUnquotedReferencedTableName('foo')
->setReferencedColumnNames(...$referencedColumnNames)
->create(),
ForeignKeyConstraint::editor()
->setUnquotedName('FK_WITH_INTENDED_QUOTATION')
->setReferencingColumnNames(...$referencingColumnNames)
->setQuotedReferencedTableName('foo-bar')
->setReferencedColumnNames(...$referencedColumnNames)
->create(),
)
->create();

$sql = $this->platform->getCreateTableSQL($table);
self::assertEquals($this->getQuotedColumnInForeignKeySQL(), $sql);
Expand Down Expand Up @@ -974,32 +986,47 @@ abstract protected function getAlterStringToFixedStringSQL(): array;

public function testGeneratesAlterTableRenameIndexUsedByForeignKeySQL(): void
{
$foreignTable = new Table('foreign_table', [
Column::editor()
->setUnquotedName('id')
->setTypeName(Types::INTEGER)
->create(),
]);
$foreignTable->setPrimaryKey(['id']);

$primaryTable = new Table('mytable', [
Column::editor()
->setUnquotedName('foo')
->setTypeName(Types::INTEGER)
->create(),
Column::editor()
->setUnquotedName('bar')
->setTypeName(Types::INTEGER)
->create(),
Column::editor()
->setUnquotedName('baz')
->setTypeName(Types::INTEGER)
->create(),
]);
$primaryTable->addIndex(['foo'], 'idx_foo');
$primaryTable->addIndex(['bar'], 'idx_bar');
$primaryTable->addForeignKeyConstraint($foreignTable->getName(), ['foo'], ['id'], [], 'fk_foo');
$primaryTable->addForeignKeyConstraint($foreignTable->getName(), ['bar'], ['id'], [], 'fk_bar');
$primaryTable = Table::editor()
->setUnquotedName('mytable')
->setColumns(
Column::editor()
->setUnquotedName('foo')
->setTypeName(Types::INTEGER)
->create(),
Column::editor()
->setUnquotedName('bar')
->setTypeName(Types::INTEGER)
->create(),
Column::editor()
->setUnquotedName('baz')
->setTypeName(Types::INTEGER)
->create(),
)
->setIndexes(
Index::editor()
->setUnquotedName('idx_foo')
->setUnquotedColumnNames('foo')
->create(),
Index::editor()
->setUnquotedName('idx_bar')
->setUnquotedColumnNames('bar')
->create(),
)
->setForeignKeyConstraints(
ForeignKeyConstraint::editor()
->setUnquotedName('fk_foo')
->setUnquotedReferencingColumnNames('foo')
->setUnquotedReferencedTableName('foreign_table')
->setUnquotedReferencedColumnNames('id')
->create(),
ForeignKeyConstraint::editor()
->setUnquotedName('fk_bar')
->setUnquotedReferencingColumnNames('bar')
->setUnquotedReferencedTableName('foreign_table')
->setUnquotedReferencedColumnNames('id')
->create(),
)
->create();

$tableDiff = new TableDiff($primaryTable, renamedIndexes: [
'idx_foo' => new Index('idx_foo_renamed', ['foo']),
Expand Down
2 changes: 1 addition & 1 deletion tests/Platforms/DB2PlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected function getQuotedColumnInForeignKeySQL(): array
. ' REFERENCES foo ("create", bar, "foo-bar")',
'ALTER TABLE "quoted" ADD CONSTRAINT FK_WITH_INTENDED_QUOTATION FOREIGN KEY ("create", foo, "bar")'
. ' REFERENCES "foo-bar" ("create", bar, "foo-bar")',
'CREATE INDEX IDX_22660D028FD6E0FB8C736521D79164E3 ON "quoted" ("create", foo, "bar")',
'CREATE INDEX IDX_22660D028FD6E0FB8C7365216D704F76 ON "quoted" ("create", foo, "bar")',
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Platforms/OraclePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ protected function getQuotedColumnInForeignKeySQL(): array
. ' REFERENCES foo ("create", bar, "foo-bar")',
'ALTER TABLE "quoted" ADD CONSTRAINT FK_WITH_INTENDED_QUOTATION FOREIGN KEY ("create", foo, "bar")'
. ' REFERENCES "foo-bar" ("create", bar, "foo-bar")',
'CREATE INDEX IDX_22660D028FD6E0FB8C736521D79164E3 ON "quoted" ("create", foo, "bar")',
'CREATE INDEX IDX_22660D028FD6E0FB8C7365216D704F76 ON "quoted" ("create", foo, "bar")',
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Platforms/PostgreSQLPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ protected function getQuotedColumnInForeignKeySQL(): array
return [
'CREATE TABLE "quoted" ("create" VARCHAR(255) NOT NULL, '
. 'foo VARCHAR(255) NOT NULL, "bar" VARCHAR(255) NOT NULL)',
'CREATE INDEX IDX_22660D028FD6E0FB8C736521D79164E3 ON "quoted" ("create", foo, "bar")',
'CREATE INDEX IDX_22660D028FD6E0FB8C7365216D704F76 ON "quoted" ("create", foo, "bar")',
'ALTER TABLE "quoted" ADD CONSTRAINT FK_WITH_RESERVED_KEYWORD FOREIGN KEY ("create", foo, "bar")'
. ' REFERENCES "foreign" ("create", bar, "foo-bar")',
'ALTER TABLE "quoted" ADD CONSTRAINT FK_WITH_NON_RESERVED_KEYWORD FOREIGN KEY ("create", foo, "bar")'
Expand Down
2 changes: 1 addition & 1 deletion tests/Platforms/SQLServerPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ protected function getQuotedColumnInForeignKeySQL(): array
return [
'CREATE TABLE [quoted] ([create] NVARCHAR(255) NOT NULL, '
. 'foo NVARCHAR(255) NOT NULL, [bar] NVARCHAR(255) NOT NULL)',
'CREATE INDEX IDX_22660D028FD6E0FB8C736521D79164E3 ON [quoted] ([create], foo, [bar])',
'CREATE INDEX IDX_22660D028FD6E0FB8C7365216D704F76 ON [quoted] ([create], foo, [bar])',
'ALTER TABLE [quoted] ADD CONSTRAINT FK_WITH_RESERVED_KEYWORD'
. ' FOREIGN KEY ([create], foo, [bar]) REFERENCES [foreign] ([create], bar, [foo-bar])',
'ALTER TABLE [quoted] ADD CONSTRAINT FK_WITH_NON_RESERVED_KEYWORD'
Expand Down
2 changes: 1 addition & 1 deletion tests/Platforms/SQLitePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ protected function getQuotedColumnInForeignKeySQL(): array
'REFERENCES foo ("create", bar, "foo-bar") NOT DEFERRABLE INITIALLY IMMEDIATE, ' .
'CONSTRAINT FK_WITH_INTENDED_QUOTATION FOREIGN KEY ("create", foo, "bar") ' .
'REFERENCES "foo-bar" ("create", bar, "foo-bar") NOT DEFERRABLE INITIALLY IMMEDIATE)',
'CREATE INDEX IDX_22660D028FD6E0FB8C736521D79164E3 ON "quoted" ("create", foo, "bar")',
'CREATE INDEX IDX_22660D028FD6E0FB8C7365216D704F76 ON "quoted" ("create", foo, "bar")',
];
}

Expand Down