Skip to content

Commit

Permalink
[GH-4243] Address review comments and improve test example to fk with…
Browse files Browse the repository at this point in the history
… multiple columns.
  • Loading branch information
beberlei committed Sep 6, 2020
1 parent 010f69b commit 0748561
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/Doctrine/DBAL/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ public function diffTable(Table $table1, Table $table2)

// only named foreign key constraints can change, otherwise it appears as removed+added.
if (
$constraint1->getName() &&
$constraint2->getName() &&
strlen($constraint1->getName()) > 0 &&
strlen($constraint2->getName()) > 0 &&
strtolower($constraint1->getName()) === strtolower($constraint2->getName())
) {
$tableDifferences->changedForeignKeys[] = $constraint2;
Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,9 @@ protected function _getPortableViewDefinition($view)
protected function _getPortableTableForeignKeysList($tableForeignKeys)
{
$list = [];
$i = 0;
foreach ($tableForeignKeys as $value) {
$value = array_change_key_case($value, CASE_LOWER);
$index = $value['constraint_name'] ?: $i++;
$index = $value['constraint_name'] ?? $value['id'];
if (! isset($list[$index])) {
if (! isset($value['on_delete']) || $value['on_delete'] === 'RESTRICT') {
$value['on_delete'] = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,35 +261,34 @@ public function testOnlyOwnCommentIsParsed(): void
public function testUnnamedForeignKeyConstraintHandling(): void
{
$sql = <<<SQL
CREATE TABLE "bug4243_types" (
"id" integer not null primary key autoincrement
CREATE TABLE "unnamedfk_referenced1" (
"id1" integer not null,
"id2" integer not null,
primary key("id1", "id2")
);
CREATE TABLE "bug4243_groups" (
CREATE TABLE "unnamedfk_referenced2" (
"id" integer not null primary key autoincrement
);
CREATE TABLE "bug4243_documents" (
"hash" varchar not null,
CREATE TABLE "unnamedfk_referencing" (
"id" integer not null primary key autoincrement,
"created_at" datetime null,
"updated_at" datetime null,
"title" varchar not null,
"ek" varchar not null,
"iv" varchar not null,
"deleted_at" datetime null,
"type_id" integer not null,
"group_id" integer null,
foreign key("type_id") references "bug4243_types"("id") on delete cascade,
foreign key("group_id") references "bug4243_groups"("id") on delete cascade
"reference1_id1" integer not null,
"reference1_id2" integer not null,
"reference2_id" integer not null,
foreign key("reference1_id1", "reference1_id2") references "unnamedfk_referenced1"("id1", "id2") on delete cascade,
foreign key("reference2_id") references "unnamedfk_referenced2"("id") on delete cascade
)
SQL;

$this->connection->exec($sql);

$sm = $this->connection->getSchemaManager();
$onlineTable = $sm->listTableDetails('bug4243_documents');
$onlineTable = $sm->listTableDetails('unnamedfk_referencing');

$offlineTable = new Table('bug4243_documents');
$offlineTable = new Table('unnamedfk_referencing');
$offlineTable->addColumn('id', 'integer');
$offlineTable->addColumn('reference1_id1', 'integer');
$offlineTable->addColumn('reference1_id2', 'integer');
$offlineTable->addColumn('reference2_id', 'integer');

$comparator = new Schema\Comparator();
$diff = $comparator->diffTable($offlineTable, $onlineTable);
Expand All @@ -300,7 +299,7 @@ public function testUnnamedForeignKeyConstraintHandling(): void
$this->connection->exec($sql);
}

$onlineTableAfter = $sm->listTableDetails('bug4243_documents');
$onlineTableAfter = $sm->listTableDetails('unnamedfk_referencing');
$diff = $comparator->diffTable($onlineTable, $onlineTableAfter);

$this->assertFalse($diff);
Expand Down

0 comments on commit 0748561

Please sign in to comment.