Skip to content

Commit

Permalink
Merge pull request #6669 from doctrine/4.2.x
Browse files Browse the repository at this point in the history
Merge 4.2.x up into 4.3.x
  • Loading branch information
morozov authored Dec 26, 2024
2 parents 83918a4 + 6428f1a commit beadca8
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 5 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/website-schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

name: "Website config validation"

on:
pull_request:
branches:
- "*.x"
paths:
- ".doctrine-project.json"
- ".github/workflows/website-schema.yml"
push:
branches:
- "*.x"
paths:
- ".doctrine-project.json"
- ".github/workflows/website-schema.yml"

jobs:
json-validate:
name: "Validate JSON schema"
uses: "doctrine/.github/.github/workflows/[email protected]"
16 changes: 11 additions & 5 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,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 @@ -842,7 +842,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 @@ -1173,8 +1173,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 @@ -1558,9 +1563,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 beadca8

Please sign in to comment.