phpsymfonydoctrine-migrationsdoctrine-dbal

Doctrine DBAL, diff command and enum type


I am working with symfony 5.1, doctrine-bundle 2.1.2 and doctrine-migrations-bundle 2.2. I am NOT working with ORM and define my own Schemas. To add Enum types, I am using the following code:

abstract class EnumType extends Type
{
    protected string $name;

    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        $values = $this->getValues();
        $maxLength = max(array_map('strlen', $values));
        $columnName = $fieldDeclaration['name'];

        $implodedValues = implode(', ', array_map(function($value) {return "'$value'";}, $values));

        if ($platform instanceof MySqlPlatform) {
            return "ENUM($implodedValues)";
        }

        if (
            $platform instanceof SQLServer2012Platform
            || $platform instanceof PostgreSQL94Platform
        ) {
            return "VARCHAR($maxLength) CHECK ({$columnName} IN ($implodedValues))";
        }

        if ($platform instanceof SqlitePlatform) {
            return "TEXT CHECK ({$columnName} IN ($implodedValues))";
        }

        throw DBALException::invalidPlatformType($platform);
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        return $value;
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        if (!in_array($value, $this->getValues())) {
            throw new \InvalidArgumentException("Invalid '" . $this->name . "' value: " . (string)$value);
        }
        return $value;
    }

    public function getName()
    {
        return $this->name;
    }

    public function requiresSQLCommentHint(AbstractPlatform $platform)
    {
        return true;
    }

    abstract function getValues(): array;
}

Each enum then extends this abstract class to set the values.

Creating is no issue. When I run the migration diff command, I am getting the following error message:

Unknown database type enum requested, Doctrine\DBAL\Platforms\MySQL57Platform may not support it.

Any ideas how I can create a diff that also includes any changes to the enum itself?


Solution

  • I solved this by creating my own diff which firstly adds the 'enum' type if not already there using:

    if ($connection->getDatabasePlatform() instanceof MySqlPlatform) {
        if (!Type::hasType('enum')) {
            Type::addType('enum', StringType::class);
        }
        $connection->getDatabasePlatform()->registerDoctrineTypeMapping('enum', Types::STRING);
    }
    

    After that I try to read all availabble tables, and catch any exception for a type that is not defined, define it as a string and then try again:

    $schemaManager = $this->connection->getSchemaManager();
    do {
        try {
            return new Schema($schemaManager->listTables(), [], $schemaManager->createSchemaConfig());
        } catch (Exception $exception) {
            $hasErrors = true;
            $message = $exception->getMessage();
            $parts = explode('"', $message);
            // convert any removed custom type to string
            Type::addType($parts[1], StringType::class);
        }
    } while ($hasErrors);
    

    This will return the current db schema. From which a diff can be created with the new schema.