Skip to content

Commit

Permalink
[doctrineGH-4243] Handle sqlite foreign key without constraint_name.
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Sep 6, 2020
1 parent f392ffc commit f47f593
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,11 @@ protected function _getPortableViewDefinition($view)
protected function _getPortableTableForeignKeysList($tableForeignKeys)
{
$list = [];
$i = 0;
foreach ($tableForeignKeys as $value) {
$value = array_change_key_case($value, CASE_LOWER);
$name = $value['constraint_name'];
if (! isset($list[$name])) {
$index = $value['constraint_name'] ?: $i++;
if (! isset($list[$index])) {
if (! isset($value['on_delete']) || $value['on_delete'] === 'RESTRICT') {
$value['on_delete'] = null;
}
Expand All @@ -418,8 +419,8 @@ protected function _getPortableTableForeignKeysList($tableForeignKeys)
$value['on_update'] = null;
}

$list[$name] = [
'name' => $name,
$list[$index] = [
'name' => $value['constraint_name'],
'local' => [],
'foreign' => [],
'foreignTable' => $value['table'],
Expand All @@ -430,8 +431,8 @@ protected function _getPortableTableForeignKeysList($tableForeignKeys)
];
}

$list[$name]['local'][] = $value['from'];
$list[$name]['foreign'][] = $value['to'];
$list[$index]['local'][] = $value['from'];
$list[$index]['foreign'][] = $value['to'];
}

$result = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,42 @@ public function testOnlyOwnCommentIsParsed(): void
->getColumn('col1')
->getComment());
}

public function testUnnamedForeignKeyConstraintHandling(): void
{
$sql = <<<SQL
create table "bug4243_users" (
"id" integer not null primary key autoincrement
);
create table "bug4243_posts"
("id" integer not null primary key autoincrement,
"user_id" integer not null,
"body" varchar not null,
"created_at" datetime null,
"updated_at" datetime null,
foreign key("user_id") references "bug4243_users"("id"));
SQL;

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

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

$offlineTable = new Table('bug4243_posts');
$offlineTable->addColumn('id', 'integer');

$comparator = new Schema\Comparator();
$diff = $comparator->diffTable($offlineTable, $onlineTable);

$sqls = $this->connection->getDatabasePlatform()->getAlterTableSQL($diff);

foreach ($sqls as $sql) {
$this->connection->exec($sql);
}

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

$this->assertFalse($diff);
}
}

0 comments on commit f47f593

Please sign in to comment.