dbal icon indicating copy to clipboard operation
dbal copied to clipboard

Preventing default in getting drop table SQL results in null value

Open J-roen opened this issue 6 years ago • 1 comments
trafficstars

When calling $eventArgs->preventDefault() in a drop table event subscriber, \Doctrine\DBAL\Platforms\AbstractPlatform::getDropTableSQL() returns null. This null value is then added to the $sql array, which is unwanted.

Maybe a solution is to let \Doctrine\DBAL\Platforms\AbstractPlatform::getDropTableSQL() return an (in this case, empty) array just like \Doctrine\DBAL\Platforms\AbstractPlatform::getCreateTableSQL() does, and replace $sql[] = $platform->getDropTableSQL($table); with $sql = array_merge($sql, $platform->getDropTableSQL($table)); in \Doctrine\DBAL\Schema\SchemaDiff::_toSql()?

Example event subscriber for reproduction:

<?php

use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\Event\SchemaDropTableEventArgs;
use Doctrine\DBAL\Events;

class SchemaEventSubscriber implements EventSubscriber
{
    public function onSchemaDropTable(
        SchemaDropTableEventArgs $eventArgs
    ): void
    {
        $eventArgs->preventDefault();
    }

    public function getSubscribedEvents(): array
    {
        return [
            Events::onSchemaDropTable,
        ];
    }
}

J-roen avatar Jul 06 '19 20:07 J-roen

This is a tricky one because while SchemaDropTableEventArgs allows a nullable on SQL, the rest of the application is expecting a string. Changing the return type to array will take more effort into a fix and it will also target the next major release. For a fix in DBAL 2.x, the return type of getDropTableSQL() has to be a string.

SenseException avatar Jul 10 '19 20:07 SenseException