Skip to content
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

Fill in dummy types to deal with DC2:... column comments #102

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
],
"require": {
"php": "^7.2|8.0.*|8.1.*",
"ext-SimpleXML": "*",
"ext-libxml": "*",
"ext-pdo": "*",
"ext-SimpleXML": "*",
"doctrine/dbal": "^2.5.2",
"doctrine/event-manager": "^1.0",
"fakerphp/faker": "^1.6",
"symfony/console": "^4.0|^5.0|^6.0",
"symfony/service-contracts": "^2.1.2"
Expand Down
23 changes: 12 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/Webfactory/Slimdump/Doctrine/DummyType.php
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');
Copy link
Member

@janopae janopae Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could have a better description in this file on why it exists. Maybe these error messages would be a good place for such a description.

It could be something like This Doctrine type assumes that the types won't be used to convert any data – it's just there to allow the rest of DBAL's functionality to work without throwing errors. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

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;
}
}
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);
}
}
}
3 changes: 2 additions & 1 deletion src/Webfactory/Slimdump/SlimdumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Webfactory\Slimdump\Database\CsvOutputFormatDriver;
use Webfactory\Slimdump\Database\MysqlOutputFormatDriver;
use Webfactory\Slimdump\Database\OutputFormatDriverInterface;
use Webfactory\Slimdump\Doctrine\DummyTypeRegistrationEventSubscriber;

final class SlimdumpCommand extends Command
{
Expand Down Expand Up @@ -74,7 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$progressOutput = $this->createProgressOutput($input, $output);

$connection = $this->createConnection($input);
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
$connection->getEventManager()->addEventSubscriber(new DummyTypeRegistrationEventSubscriber($connection->getSchemaManager()));
$this->setMaxExecutionTimeUnlimited($connection, $progressOutput);

$config = ConfigBuilder::createConfigurationFromConsecutiveFiles($input->getArgument('config'));
Expand Down