-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
193 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/Schema/Exception/InvalidUniqueConstraintDefinition.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Schema\Exception; | ||
|
||
use Doctrine\DBAL\Schema\SchemaException; | ||
use LogicException; | ||
|
||
/** @psalm-immutable */ | ||
final class InvalidUniqueConstraintDefinition extends LogicException implements SchemaException | ||
{ | ||
public static function columnNamesAreNotSet(): self | ||
{ | ||
return new self('Unique constraint column names are not set.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Schema; | ||
|
||
use Doctrine\DBAL\Schema\Exception\InvalidUniqueConstraintDefinition; | ||
use Doctrine\DBAL\Schema\Name\UnqualifiedName; | ||
|
||
use function array_map; | ||
use function array_merge; | ||
use function array_values; | ||
use function count; | ||
|
||
final class UniqueConstraintEditor | ||
{ | ||
private ?UnqualifiedName $name = null; | ||
|
||
/** @var list<UnqualifiedName> */ | ||
private array $columnNames = []; | ||
|
||
private bool $isClustered = false; | ||
|
||
/** @internal Use {@link UniqueConstraint::editor()} or {@link UniqueConstraint::edit()} to create an instance */ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public function setName(?UnqualifiedName $name): self | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
public function setColumnNames(UnqualifiedName $firstColumName, UnqualifiedName ...$otherColumnNames): self | ||
{ | ||
$this->columnNames = array_merge([$firstColumName], array_values($otherColumnNames)); | ||
|
||
return $this; | ||
} | ||
|
||
public function setIsClustered(bool $isClustered): self | ||
{ | ||
$this->isClustered = $isClustered; | ||
|
||
return $this; | ||
} | ||
|
||
public function create(): UniqueConstraint | ||
{ | ||
if (count($this->columnNames) < 1) { | ||
throw InvalidUniqueConstraintDefinition::columnNamesAreNotSet(); | ||
} | ||
|
||
return new UniqueConstraint( | ||
$this->name?->toString() ?? '', | ||
array_map(static fn (UnqualifiedName $columnName) => $columnName->toString(), $this->columnNames), | ||
$this->isClustered ? ['clustered'] : [], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Schema; | ||
|
||
use Doctrine\DBAL\Schema\Exception\InvalidUniqueConstraintDefinition; | ||
use Doctrine\DBAL\Schema\Name\Identifier; | ||
use Doctrine\DBAL\Schema\Name\UnqualifiedName; | ||
use Doctrine\DBAL\Schema\UniqueConstraint; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class UniqueConstraintEditorTest extends TestCase | ||
{ | ||
public function testEmptyColumnNames(): void | ||
{ | ||
$this->expectException(InvalidUniqueConstraintDefinition::class); | ||
|
||
UniqueConstraint::editor() | ||
->create(); | ||
} | ||
|
||
public function testSetIsClustered(): void | ||
{ | ||
$columnName = new UnqualifiedName(Identifier::unquoted('user_id')); | ||
|
||
$editor = UniqueConstraint::editor() | ||
->setColumnNames($columnName); | ||
|
||
$constraint = $editor->create(); | ||
self::assertFalse($constraint->isClustered()); | ||
|
||
$constraint = $editor | ||
->setIsClustered(true) | ||
->create(); | ||
self::assertTrue($constraint->isClustered()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters