-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Introduce table editor #6660
Merged
Merged
Introduce table editor #6660
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Schema\Exception; | ||
|
||
use Doctrine\DBAL\Schema\SchemaException; | ||
use LogicException; | ||
|
||
/** @psalm-immutable */ | ||
final class InvalidTableDefinition extends LogicException implements SchemaException | ||
{ | ||
public static function nameNotSet(): self | ||
{ | ||
return new self('Table name is 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
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 |
---|---|---|
|
@@ -72,6 +72,9 @@ class Table extends AbstractNamedObject | |
private int $maxIdentifierLength; | ||
|
||
/** | ||
* @internal Use {@link Table::editor()} to instantiate an editor and {@link TableEditor::create()} | ||
* to create a table. | ||
* | ||
* @param array<Column> $columns | ||
* @param array<Index> $indexes | ||
* @param array<UniqueConstraint> $uniqueConstraints | ||
|
@@ -847,6 +850,31 @@ public function getComment(): ?string | |
return $this->_options['comment'] ?? null; | ||
} | ||
|
||
/** | ||
* Instantiates a new table editor. | ||
*/ | ||
public static function editor(): TableEditor | ||
{ | ||
return new TableEditor(); | ||
} | ||
|
||
/** | ||
* Instantiates a new table editor and initializes it with the table's properties. | ||
*/ | ||
public function edit(): TableEditor | ||
{ | ||
return self::editor() | ||
->setName($this->getObjectName()->toString()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even though |
||
->setColumns($this->_columns) | ||
->setIndexes($this->_indexes) | ||
->setUniqueConstraints($this->uniqueConstraints) | ||
->setForeignKeyConstraints($this->_fkConstraints) | ||
->setOptions($this->_options) | ||
->setConfiguration( | ||
new TableConfiguration($this->maxIdentifierLength), | ||
); | ||
} | ||
|
||
/** | ||
* @param array<string|int, string> $columns | ||
* @param array<int, string> $flags | ||
|
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,105 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Schema; | ||
|
||
use Doctrine\DBAL\Schema\Exception\InvalidTableDefinition; | ||
|
||
final class TableEditor | ||
{ | ||
private ?string $name = null; | ||
|
||
/** @var array<Column> */ | ||
private array $columns = []; | ||
|
||
/** @var array<Index> */ | ||
private array $indexes = []; | ||
|
||
/** @var array<UniqueConstraint> */ | ||
private array $uniqueConstraints = []; | ||
|
||
/** @var array<ForeignKeyConstraint> */ | ||
private array $foreignKeyConstraints = []; | ||
|
||
/** @var array<string, mixed> */ | ||
private array $options = []; | ||
|
||
private ?TableConfiguration $configuration = null; | ||
|
||
/** @internal Use {@link Table::editor()} or {@link Table::edit()} to create an instance */ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public function setName(string $name): self | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<Column> $columns */ | ||
public function setColumns(array $columns): self | ||
{ | ||
$this->columns = $columns; | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<Index> $indexes */ | ||
public function setIndexes(array $indexes): self | ||
{ | ||
$this->indexes = $indexes; | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<UniqueConstraint> $uniqueConstraints */ | ||
public function setUniqueConstraints(array $uniqueConstraints): self | ||
{ | ||
$this->uniqueConstraints = $uniqueConstraints; | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<ForeignKeyConstraint> $foreignKeyConstraints */ | ||
public function setForeignKeyConstraints(array $foreignKeyConstraints): self | ||
{ | ||
$this->foreignKeyConstraints = $foreignKeyConstraints; | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<string, mixed> $options */ | ||
public function setOptions(array $options): self | ||
{ | ||
$this->options = $options; | ||
|
||
return $this; | ||
} | ||
|
||
public function setConfiguration(TableConfiguration $configuration): self | ||
{ | ||
$this->configuration = $configuration; | ||
|
||
return $this; | ||
} | ||
|
||
public function create(): Table | ||
{ | ||
if ($this->name === null) { | ||
throw InvalidTableDefinition::nameNotSet(); | ||
} | ||
|
||
return new Table( | ||
$this->name, | ||
$this->columns, | ||
$this->indexes, | ||
$this->uniqueConstraints, | ||
$this->foreignKeyConstraints, | ||
$this->options, | ||
$this->configuration, | ||
); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Schema; | ||
|
||
use Doctrine\DBAL\Schema\Exception\InvalidTableDefinition; | ||
use Doctrine\DBAL\Schema\Table; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class TableEditorTest extends TestCase | ||
{ | ||
public function testSetName(): void | ||
{ | ||
$table = (new Table('accounts')) | ||
->edit() | ||
->setName('contacts') | ||
->create(); | ||
|
||
self::assertSame('contacts', $table->getName()); | ||
} | ||
|
||
public function testNameNotSet(): void | ||
{ | ||
$this->expectException(InvalidTableDefinition::class); | ||
|
||
Table::editor()->create(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This contract enables this expression:
dbal/src/Schema/Table.php
Line 867 in 7a25c5d