-
-
Notifications
You must be signed in to change notification settings - Fork 45
Add ENUM column
#1107
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
+287
−61
Merged
Add ENUM column
#1107
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
4b75975
start
vjik 222c486
improve
vjik 87713e0
Improve
vjik a11a7c0
improve
vjik 1943711
improve
vjik cb24aa6
Merge branch 'master' into enum-col
vjik cc15505
fix
vjik d21e71a
fix
vjik b4ea416
improve
vjik 2f9e2a8
refactor
vjik 9b38bfe
improve
vjik 93cb42d
Update src/QueryBuilder/AbstractColumnDefinitionBuilder.php
vjik e776c16
improve tests
vjik bb82459
improve
vjik 3148886
fix
vjik cd70611
fix enum parsing
vjik f0546ba
fix ci
vjik 81abe25
improve
vjik 1d31505
improve test
vjik 60ee6bf
Merge branch 'master' into enum-col
vjik 1088ee3
fix
vjik 2b46745
improve
vjik c2d2cc5
improve
vjik 48437d2
more tests
vjik 67f8675
changelog
vjik fd07769
Update src/Schema/Column/ColumnBuilder.php
vjik 59fe5f0
improve builder
vjik 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,43 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Schema\Column; | ||
|
|
||
| use LogicException; | ||
| use Yiisoft\Db\Constant\ColumnType; | ||
|
|
||
| final class EnumColumn extends StringColumn | ||
| { | ||
| protected const DEFAULT_TYPE = ColumnType::ENUM; | ||
|
|
||
| /** | ||
| * @var string[]|null $values The list of possible values for an ENUM column. | ||
| * @psalm-var non-empty-list<string>|null | ||
| */ | ||
| protected ?array $values = null; | ||
|
|
||
| /** | ||
| * @param string[] $values The list of possible values for the `ENUM` column. | ||
| * @psalm-param non-empty-list<string> $values | ||
| */ | ||
| public function values(array $values): static | ||
| { | ||
| $this->values = $values; | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @return string[] The enum values of the column. | ||
| * @psalm-return non-empty-list<string> | ||
| * | ||
| * @see values() | ||
| */ | ||
| public function getValues(): array | ||
| { | ||
| if ($this->values === null) { | ||
| throw new LogicException('Enum values have not been set.'); | ||
| } | ||
| return $this->values; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,109 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Tests\Common; | ||
|
|
||
| use PHPUnit\Framework\Attributes\TestWith; | ||
| use Yiisoft\Db\Schema\Column\EnumColumn; | ||
| use Yiisoft\Db\Schema\TableSchemaInterface; | ||
| use Yiisoft\Db\Tests\Support\IntegrationTestCase; | ||
|
|
||
| abstract class CommonEnumColumnTest extends IntegrationTestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| $this->executeStatements(...$this->dropDatabaseObjectsStatements()); | ||
| $this->executeStatements(...$this->createDatabaseObjectsStatements()); | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| $this->executeStatements(...$this->dropDatabaseObjectsStatements()); | ||
| } | ||
|
|
||
| public function testSchema(): void | ||
| { | ||
| $db = $this->getSharedConnection(); | ||
|
|
||
| $column = $db->getTableSchema('tbl_enum')->getColumn('status'); | ||
|
|
||
| $this->assertInstanceOf(EnumColumn::class, $column, 'Column class is "' . $column::class . '"'); | ||
| $this->assertEqualsCanonicalizing( | ||
| ['active', 'unactive', 'pending'], | ||
| $column->getValues(), | ||
| ); | ||
| } | ||
|
|
||
| public function testInsertAndQuery(): void | ||
| { | ||
| $db = $this->getSharedConnection(); | ||
|
|
||
| $db->createCommand()->insertBatch('tbl_enum', [ | ||
| ['id' => 1, 'status' => 'active'], | ||
| ['id' => 2, 'status' => 'pending'], | ||
| ['id' => 3, 'status' => 'pending'], | ||
| ['id' => 4, 'status' => 'unactive'], | ||
| ])->execute(); | ||
|
|
||
| $rows = $db->select('status')->from('tbl_enum')->orderBy('id ASC')->column(); | ||
|
|
||
| $this->assertSame( | ||
| ['active', 'pending', 'pending', 'unactive'], | ||
| $rows, | ||
| ); | ||
| } | ||
|
|
||
| #[TestWith([['active', 'inactive', 'pending']])] | ||
| #[TestWith([['[one]', 'the [two]', 'the [three] to']])] | ||
| #[TestWith([['(one)', 'the (two)', 'the (three) to']])] | ||
| #[TestWith([["hello''world''", "the '[feature']"]])] | ||
| public function testCreateTable(array $items): void | ||
| { | ||
| $this->dropTable('test_enum_table'); | ||
|
|
||
| $db = $this->getSharedConnection(); | ||
| $columnBuilder = $db->getColumnBuilderClass(); | ||
|
|
||
| $db->createCommand() | ||
| ->createTable( | ||
| 'test_enum_table', | ||
| [ | ||
| 'id' => $columnBuilder::integer(), | ||
| 'status' => $columnBuilder::enum($items), | ||
| ], | ||
| ) | ||
| ->execute(); | ||
|
|
||
| $tableSchema = $db->getTableSchema('test_enum_table'); | ||
| $this->assertInstanceOf(TableSchemaInterface::class, $tableSchema); | ||
|
|
||
| $columns = $tableSchema->getColumns(); | ||
| $this->assertEqualsCanonicalizing(['id', 'status'], array_keys($columns)); | ||
|
|
||
| $column = $columns['status']; | ||
| $this->assertInstanceOf(EnumColumn::class, $column, $column::class); | ||
| $this->assertEqualsCanonicalizing($items, $column->getValues()); | ||
|
|
||
| $this->dropTable('test_enum_table'); | ||
| } | ||
|
|
||
| /** | ||
| * SQL statements for create all database objects needed for the test. | ||
| * | ||
| * Table "tbl_enum" with columns: | ||
| * - column "id" of type integer; | ||
| * - enum column "status" that accepts values 'active', 'unactive', 'pending'. | ||
| * | ||
| * @return string[] | ||
| */ | ||
| abstract protected function createDatabaseObjectsStatements(): array; | ||
|
|
||
| /** | ||
| * SQL statements for remove all database objects created for the test. | ||
| * Take account that object can be absent. | ||
| * | ||
| * @return string[] | ||
| */ | ||
| abstract protected function dropDatabaseObjectsStatements(): array; | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.