Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge 4.3.x up into 5.0.x #6672

Closed
wants to merge 10 commits into from
16 changes: 11 additions & 5 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
@@ -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;
}
@@ -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) {
@@ -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)) . ')';
}

/**
@@ -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);
}

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();
}
}