-
-
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.
Implement an EnumType for MySQL/MariaDB
- Loading branch information
Showing
10 changed files
with
170 additions
and
12 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
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
|
||
final class EnumType extends Type | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string | ||
{ | ||
return $platform->getEnumDeclarationSQL($column); | ||
} | ||
} |
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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; | ||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Types\EnumType; | ||
use Doctrine\DBAL\Types\Type; | ||
use Doctrine\DBAL\Types\Types; | ||
|
||
final class EnumTypeTest extends FunctionalTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
if (! $this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) { | ||
self::markTestSkipped('This test requires MySQL or MariaDB.'); | ||
} | ||
|
||
$this->connection->executeStatement('DROP TABLE IF EXISTS my_enum_table'); | ||
} | ||
|
||
public function testIntrospectEnum(): void | ||
{ | ||
$this->connection->executeStatement(<<< 'SQL' | ||
CREATE TABLE my_enum_table ( | ||
id BIGINT NOT NULL PRIMARY KEY, | ||
suit ENUM('hearts', 'diamonds', 'clubs', 'spades') NOT NULL DEFAULT 'hearts' | ||
); | ||
SQL); | ||
|
||
$schemaManager = $this->connection->createSchemaManager(); | ||
$table = $schemaManager->introspectTable('my_enum_table'); | ||
|
||
self::assertCount(2, $table->getColumns()); | ||
self::assertTrue($table->hasColumn('suit')); | ||
self::assertInstanceOf(EnumType::class, $table->getColumn('suit')->getType()); | ||
self::assertSame(['hearts', 'diamonds', 'clubs', 'spades'], $table->getColumn('suit')->getValues()); | ||
self::assertSame('hearts', $table->getColumn('suit')->getDefault()); | ||
} | ||
|
||
public function testDeployEnum(): void | ||
{ | ||
$schemaManager = $this->connection->createSchemaManager(); | ||
$schema = new Schema(schemaConfig: $schemaManager->createSchemaConfig()); | ||
$table = $schema->createTable('my_enum_table'); | ||
$table->addColumn('id', Types::BIGINT, ['notnull' => true]); | ||
$table->addColumn('suit', Types::ENUM, [ | ||
'values' => ['hearts', 'diamonds', 'clubs', 'spades'], | ||
'notnull' => true, | ||
'default' => 'hearts', | ||
]); | ||
$table->setPrimaryKey(['id']); | ||
|
||
$schemaManager->createSchemaObjects($schema); | ||
|
||
$introspectedTable = $schemaManager->introspectTable('my_enum_table'); | ||
|
||
self::assertTrue($schemaManager->createComparator()->compareTables($table, $introspectedTable)->isEmpty()); | ||
|
||
$this->connection->insert('my_enum_table', ['id' => 1, 'suit' => 'hearts'], ['suit' => Types::ENUM]); | ||
$this->connection->insert('my_enum_table', ['id' => 2, 'suit' => 'diamonds'], ['suit' => Type::getType(Types::ENUM)]); | ||
Check warning on line 64 in tests/Functional/Types/EnumTypeTest.php GitHub Actions / Coding Standards / Coding Standards (PHP: 8.3)
|
||
|
||
self::assertSame( | ||
[['id' => 1, 'suit' => 'hearts'], ['id' => 2, 'suit' => 'diamonds']], | ||
$this->connection->fetchAllAssociative('SELECT id, suit FROM my_enum_table'), | ||
); | ||
} | ||
} |
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