Skip to content

Commit f9f592c

Browse files
committed
Do not ignore foreign keys DDL on non-InnoDB MySQL
1 parent 60ce1fe commit f9f592c

File tree

3 files changed

+7
-124
lines changed

3 files changed

+7
-124
lines changed

UPGRADE.md

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

99
# Upgrade to 4.0
1010

11+
## BC BREAK: foreign key DDL is generated on MySQL regardless of the storage engine.
12+
13+
The DBAL generates DDL for foreign keys regardless of the MySQL storage engines used by the table
14+
that owns the foreign key constraint.
15+
1116
## BC BREAK: removed `AbstractPlatform` methods exposing quote characters.
1217

1318
The `AbstractPlatform::getStringLiteralQuoteCharacter()` and `::getIdentifierQuoteCharacter()` methods

src/Platforms/AbstractMySQLPlatform.php

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Doctrine\DBAL\Schema\Table;
1414
use Doctrine\DBAL\Schema\TableDiff;
1515
use Doctrine\DBAL\TransactionIsolationLevel;
16-
use Doctrine\Deprecations\Deprecation;
1716

1817
use function array_merge;
1918
use function array_unique;
@@ -24,7 +23,6 @@
2423
use function is_numeric;
2524
use function sprintf;
2625
use function str_replace;
27-
use function strcasecmp;
2826
use function strtoupper;
2927
use function trim;
3028

@@ -211,38 +209,6 @@ public function getListTablesSQL(): string
211209
return "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'";
212210
}
213211

214-
/**
215-
* {@inheritDoc}
216-
*/
217-
public function getCreateTablesSQL(array $tables): array
218-
{
219-
$sql = [];
220-
221-
foreach ($tables as $table) {
222-
$sql = array_merge($sql, $this->getCreateTableWithoutForeignKeysSQL($table));
223-
}
224-
225-
foreach ($tables as $table) {
226-
if (! $table->hasOption('engine') || $this->engineSupportsForeignKeys($table->getOption('engine'))) {
227-
foreach ($table->getForeignKeys() as $foreignKey) {
228-
$sql[] = $this->getCreateForeignKeySQL(
229-
$foreignKey,
230-
$table->getQuotedName($this)
231-
);
232-
}
233-
} elseif (count($table->getForeignKeys()) > 0) {
234-
Deprecation::trigger(
235-
'doctrine/dbal',
236-
'https://github.com/doctrine/dbal/pull/5414',
237-
'Relying on the DBAL not generating DDL for foreign keys on MySQL engines'
238-
. ' other than InnoDB is deprecated. Define foreign key constraints only if they are necessary.'
239-
);
240-
}
241-
}
242-
243-
return $sql;
244-
}
245-
246212
/**
247213
* {@inheritDoc}
248214
*/
@@ -291,17 +257,8 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
291257

292258
// Propagate foreign key constraints only for InnoDB.
293259
if (isset($options['foreignKeys'])) {
294-
if (! isset($options['engine']) || $this->engineSupportsForeignKeys($options['engine'])) {
295-
foreach ($options['foreignKeys'] as $definition) {
296-
$sql[] = $this->getCreateForeignKeySQL($definition, $name);
297-
}
298-
} elseif (count($options['foreignKeys']) > 0) {
299-
Deprecation::trigger(
300-
'doctrine/dbal',
301-
'https://github.com/doctrine/dbal/pull/5414',
302-
'Relying on the DBAL not generating DDL for foreign keys on MySQL engines'
303-
. ' other than InnoDB is deprecated. Define foreign key constraints only if they are necessary.'
304-
);
260+
foreach ($options['foreignKeys'] as $definition) {
261+
$sql[] = $this->getCreateForeignKeySQL($definition, $name);
305262
}
306263
}
307264

@@ -351,11 +308,6 @@ private function buildTableOptions(array $options): string
351308
return implode(' ', $tableOptions);
352309
}
353310

354-
private function engineSupportsForeignKeys(string $engine): bool
355-
{
356-
return strcasecmp(trim($engine), 'InnoDB') === 0;
357-
}
358-
359311
/**
360312
* {@inheritDoc}
361313
*/

tests/Platforms/AbstractMySQLPlatformTestCase.php

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
99
use Doctrine\DBAL\Platforms\MySQL;
1010
use Doctrine\DBAL\Schema\Comparator;
11-
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
1211
use Doctrine\DBAL\Schema\Index;
1312
use Doctrine\DBAL\Schema\Table;
1413
use Doctrine\DBAL\Schema\TableDiff;
@@ -539,79 +538,6 @@ public function testGetVariableLengthBinaryTypeDeclarationSQLNoLength(): void
539538
parent::testGetVariableLengthBinaryTypeDeclarationSQLNoLength();
540539
}
541540

542-
public function testDoesNotPropagateForeignKeyCreationForNonSupportingEngines(): void
543-
{
544-
$table = new Table('foreign_table');
545-
$table->addColumn('id', 'integer');
546-
$table->addColumn('fk_id', 'integer');
547-
$table->addForeignKeyConstraint('foreign_table', ['fk_id'], ['id']);
548-
$table->setPrimaryKey(['id']);
549-
$table->addOption('engine', 'MyISAM');
550-
551-
self::assertSame(
552-
[
553-
'CREATE TABLE foreign_table (id INT NOT NULL, fk_id INT NOT NULL, '
554-
. 'INDEX IDX_5690FFE2A57719D0 (fk_id), PRIMARY KEY(id)) '
555-
. 'ENGINE = MyISAM',
556-
],
557-
$this->platform->getCreateTableSQL($table)
558-
);
559-
560-
$table = clone $table;
561-
$table->addOption('engine', 'InnoDB');
562-
563-
self::assertSame(
564-
[
565-
'CREATE TABLE foreign_table (id INT NOT NULL, fk_id INT NOT NULL, '
566-
. 'INDEX IDX_5690FFE2A57719D0 (fk_id), PRIMARY KEY(id)) '
567-
. 'ENGINE = InnoDB',
568-
'ALTER TABLE foreign_table ADD CONSTRAINT FK_5690FFE2A57719D0 FOREIGN KEY (fk_id)'
569-
. ' REFERENCES foreign_table (id)',
570-
],
571-
$this->platform->getCreateTableSQL($table)
572-
);
573-
}
574-
575-
public function testDoesNotPropagateForeignKeyAlterationForNonSupportingEngines(): void
576-
{
577-
$table = new Table('foreign_table');
578-
$table->addColumn('id', 'integer');
579-
$table->addColumn('fk_id', 'integer');
580-
$table->addForeignKeyConstraint('foreign_table', ['fk_id'], ['id']);
581-
$table->setPrimaryKey(['id']);
582-
$table->addOption('engine', 'MyISAM');
583-
584-
$addedForeignKeys = [new ForeignKeyConstraint(['fk_id'], 'foo', ['id'], 'fk_add')];
585-
$changedForeignKeys = [new ForeignKeyConstraint(['fk_id'], 'bar', ['id'], 'fk_change')];
586-
$removedForeignKeys = [new ForeignKeyConstraint(['fk_id'], 'baz', ['id'], 'fk_remove')];
587-
588-
$tableDiff = new TableDiff('foreign_table');
589-
$tableDiff->fromTable = $table;
590-
$tableDiff->addedForeignKeys = $addedForeignKeys;
591-
$tableDiff->changedForeignKeys = $changedForeignKeys;
592-
$tableDiff->removedForeignKeys = $removedForeignKeys;
593-
594-
self::assertEmpty($this->platform->getAlterTableSQL($tableDiff));
595-
596-
$table->addOption('engine', 'InnoDB');
597-
598-
$tableDiff = new TableDiff('foreign_table');
599-
$tableDiff->fromTable = $table;
600-
$tableDiff->addedForeignKeys = $addedForeignKeys;
601-
$tableDiff->changedForeignKeys = $changedForeignKeys;
602-
$tableDiff->removedForeignKeys = $removedForeignKeys;
603-
604-
self::assertSame(
605-
[
606-
'ALTER TABLE foreign_table DROP FOREIGN KEY fk_remove',
607-
'ALTER TABLE foreign_table DROP FOREIGN KEY fk_change',
608-
'ALTER TABLE foreign_table ADD CONSTRAINT fk_add FOREIGN KEY (fk_id) REFERENCES foo (id)',
609-
'ALTER TABLE foreign_table ADD CONSTRAINT fk_change FOREIGN KEY (fk_id) REFERENCES bar (id)',
610-
],
611-
$this->platform->getAlterTableSQL($tableDiff)
612-
);
613-
}
614-
615541
/**
616542
* @return string[]
617543
*/

0 commit comments

Comments
 (0)