Skip to content

Commit

Permalink
Merge pull request #6668 from morozov/unnamed-unique-constraints
Browse files Browse the repository at this point in the history
Fix creation of unnamed unique constraints
  • Loading branch information
morozov authored Dec 25, 2024
2 parents 497e0e4 + 343f8a7 commit 6428f1a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ private function buildCreateTableSQL(Table $table, bool $createForeignKeys): arr

foreach ($table->getIndexes() as $index) {
if (! $index->isPrimary()) {
$options['indexes'][$index->getQuotedName($this)] = $index;
$options['indexes'][] = $index;

continue;
}
Expand All @@ -839,7 +839,7 @@ private function buildCreateTableSQL(Table $table, bool $createForeignKeys): arr
}

foreach ($table->getUniqueConstraints() as $uniqueConstraint) {
$options['uniqueConstraints'][$uniqueConstraint->getQuotedName($this)] = $uniqueConstraint;
$options['uniqueConstraints'][] = $uniqueConstraint;
}

if ($createForeignKeys) {
Expand Down Expand Up @@ -1170,8 +1170,13 @@ public function getCreateSchemaSQL(string $schemaName): string
*/
public function getCreateUniqueConstraintSQL(UniqueConstraint $constraint, string $tableName): string
{
return 'ALTER TABLE ' . $tableName . ' ADD CONSTRAINT ' . $constraint->getQuotedName($this) . ' UNIQUE'
. ' (' . implode(', ', $constraint->getQuotedColumns($this)) . ')';
$sql = 'ALTER TABLE ' . $tableName . ' ADD';

if ($constraint->getName() !== '') {
$sql .= ' CONSTRAINT ' . $constraint->getQuotedName($this);
}

return $sql . ' UNIQUE (' . implode(', ', $constraint->getQuotedColumns($this)) . ')';
}

/**
Expand Down Expand Up @@ -1546,9 +1551,10 @@ public function getUniqueConstraintDeclarationSQL(UniqueConstraint $constraint):
throw new InvalidArgumentException('Incomplete definition. "columns" required.');
}

$chunks = ['CONSTRAINT'];
$chunks = [];

if ($constraint->getName() !== '') {
$chunks[] = 'CONSTRAINT';
$chunks[] = $constraint->getQuotedName($this);
}

Expand Down
49 changes: 49 additions & 0 deletions tests/Functional/Schema/ForeignKeyConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional\Schema;

use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;

final class ForeignKeyConstraintTest extends FunctionalTestCase
{
public function testUnnamedForeignKeyConstraint(): void
{
$this->dropTableIfExists('users');
$this->dropTableIfExists('roles');
$this->dropTableIfExists('teams');

$roles = new Table('roles');
$roles->addColumn('id', Types::INTEGER);
$roles->setPrimaryKey(['id']);

$teams = new Table('teams');
$teams->addColumn('id', Types::INTEGER);
$teams->setPrimaryKey(['id']);

$users = new Table('users', [
new Column('id', Type::getType(Types::INTEGER)),
new Column('role_id', Type::getType(Types::INTEGER)),
new Column('team_id', Type::getType(Types::INTEGER)),
], [], [], [
new ForeignKeyConstraint(['role_id'], 'roles', ['id']),
new ForeignKeyConstraint(['team_id'], 'teams', ['id']),
]);
$users->setPrimaryKey(['id']);

$sm = $this->connection->createSchemaManager();
$sm->createTable($roles);
$sm->createTable($teams);
$sm->createTable($users);

$table = $sm->introspectTable('users');

self::assertCount(2, $table->getForeignKeys());
}
}
36 changes: 36 additions & 0 deletions tests/Functional/Schema/UniqueConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional\Schema;

use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\UniqueConstraint;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;

final class UniqueConstraintTest extends FunctionalTestCase
{
public function testUnnamedUniqueConstraint(): void
{
$this->dropTableIfExists('users');

$users = new Table('users', [
new Column('id', Type::getType(Types::INTEGER)),
new Column('username', Type::getType(Types::STRING), ['length' => 32]),
new Column('email', Type::getType(Types::STRING), ['length' => 255]),
], [], [
new UniqueConstraint('', ['username']),
new UniqueConstraint('', ['email']),
], []);

$sm = $this->connection->createSchemaManager();
$sm->createTable($users);

// we want to assert that the two empty names don't clash, but introspection of unique constraints is currently
// not supported. for now, we just assert that the table can be created without exceptions.
$this->expectNotToPerformAssertions();
}
}

0 comments on commit 6428f1a

Please sign in to comment.