-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fill in dummy types to deal with DC2:... column comments (#102)
We're using the `SchemaManager` to simplify listing tables, views and so on. But, the information provided by it also includes the DBAL types used for columns, and that information may be parsed from column comments in the database. The problem is that a table definition like so: ```sql CREATE TABLE `test` ( `some_col` int NOT NULL COMMENT '(DC2Type:some_type)' ) ``` ... `slimdump` fail fail with an exception because it does not know about the `some_type`. This type may be available and defined in your application, but we cannot put it into the `slimdump` command. Now, in fact, we don't care about the type and its conversion capabilities at all. So, this PR adds a runtime "just in type" registration of a `DummyType` that serves as a placeholder to avoid issues when loading the schema. Fixes #96.
- Loading branch information
Showing
5 changed files
with
93 additions
and
13 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,27 @@ | ||
<?php | ||
|
||
namespace Webfactory\Slimdump\Doctrine; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\Type; | ||
use LogicException; | ||
|
||
class DummyType extends Type | ||
{ | ||
public const NAME = 'dummy_type'; | ||
|
||
public function convertToPHPValue($value, AbstractPlatform $platform) | ||
{ | ||
throw new LogicException('this should not be called in the first place'); | ||
} | ||
|
||
public function getSQLDeclaration(array $column, AbstractPlatform $platform) | ||
{ | ||
throw new LogicException('this should not be called in the first place'); | ||
} | ||
|
||
public function getName() | ||
{ | ||
return self::NAME; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Webfactory/Slimdump/Doctrine/DummyTypeRegistrationEventSubscriber.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,50 @@ | ||
<?php | ||
|
||
namespace Webfactory\Slimdump\Doctrine; | ||
|
||
use Doctrine\Common\EventSubscriber; | ||
use Doctrine\DBAL\Event\SchemaColumnDefinitionEventArgs; | ||
use Doctrine\DBAL\Events; | ||
use Doctrine\DBAL\Schema\AbstractSchemaManager; | ||
use Doctrine\DBAL\Types\Type; | ||
|
||
class DummyTypeRegistrationEventSubscriber implements EventSubscriber | ||
{ | ||
/** | ||
* @var AbstractSchemaManager | ||
*/ | ||
private $schemaManager; | ||
|
||
public function __construct(AbstractSchemaManager $schemaManager) | ||
{ | ||
$this->schemaManager = $schemaManager; | ||
} | ||
|
||
public function getSubscribedEvents(): array | ||
{ | ||
return [Events::onSchemaColumnDefinition]; | ||
} | ||
|
||
public function onSchemaColumnDefinition(SchemaColumnDefinitionEventArgs $event): void | ||
{ | ||
$tableColumn = array_change_key_case($event->getTableColumn(), \CASE_LOWER); | ||
$dbType = strtolower($tableColumn['type']); | ||
$dbType = strtok($dbType, '(), '); | ||
|
||
if (isset($tableColumn['comment'])) { | ||
$type = $this->schemaManager->extractDoctrineTypeFromComment($tableColumn['comment'], ''); | ||
|
||
if ($type && !Type::hasType($type)) { | ||
Type::addType($type, DummyType::class); | ||
} | ||
} | ||
|
||
$databasePlatform = $this->schemaManager->getDatabasePlatform(); | ||
if (!$databasePlatform->hasDoctrineTypeMappingFor($dbType)) { | ||
if (!Type::hasType(DummyType::NAME)) { | ||
Type::addType(DummyType::NAME, DummyType::class); | ||
} | ||
$databasePlatform->registerDoctrineTypeMapping($dbType, DummyType::NAME); | ||
} | ||
} | ||
} |
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