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

Default value when Doctrine\DBAL\Schema\AbstractSchemaManager::listTableColumns #3172

Closed
Yelfive opened this issue Jun 4, 2018 · 4 comments

Comments

@Yelfive
Copy link

Yelfive commented Jun 4, 2018

Bug Report

Q A
BC Break no
Version 2.6.3

Summary

Current behavior

When MySQL column has default values, the PDO returns a quoted value like 'some default value',
and this becomes an issue when comparing db and entity, the migration file will escape the quotes too:

 $this->addSql('ALTER TABLE dt_coupon_gas CHANGE id id INT UNSIGNED AUTO_INCREMENT NOT NULL, CHANGE name name VARCHAR(255) DEFAULT \'\'hello\'\' NOT NULL COLLATE utf8mb4_unicode_ci');

and the \'hello\' then results into exception.

How to reproduce

  1. Create an entity with a column which is varchar and has default string value
  2. run migrations:diff
  3. run migrations:migrate
  4. Change the entity column, e.g. remove the default value
  5. run migrations:diff
  6. run migrations:migrate

Expected behavior

This might be MySQL's issue, but it becomes troublesome. I'm expecting that the default value read from MySQL database could be specially handled, single quotes stripped

if (preg_match('#^\'(.*)\'$#', $default $match)) $default = $match[1];
@morozov
Copy link
Member

morozov commented Jun 4, 2018

@Yelfive there was a recent effort to improve handling default values in #2850 but it wasn't finished and got eventually reverted. Please feel free to submit a patch.

@Yelfive
Copy link
Author

Yelfive commented Jun 5, 2018

Good to know that and i have a quick fix for my project(in case some need to deal with too)

$config = Setup::createAnnotationMetadataConfiguration($paths, getenv('APP_DEBUG') === 'true');

$connection['driverClass'] = MyDriverClass::class;

EntityManager::create($connection, $config);
class MyDriverClass extends \Doctrine\DBAL\Driver\PDOMySql\Driver
{

    /**
     * Replace default doctrine MySqlSchemaManager
     * {@inheritdoc}
     */
    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
    {
        return new MySqlSchemaManager($conn);
    }
}
class MySqlSchemaManager extends \Doctrine\DBAL\Schema\MySqlSchemaManager
{
    public function listTableColumns($table, $database = null)
    {
        if (!$database) {
            $database = $this->_conn->getDatabase();
        }

        $sql = $this->_platform->getListTableColumnsSQL($table, $database);

        $tableColumns = $this->_conn->fetchAll($sql);

        array_walk($tableColumns, function (&$tableColumn) {
            if (preg_match('#^\'(.*)\'$#', $tableColumn['Default'], $match)) $tableColumn['Default'] = $match[1];
        });

        return $this->_getPortableTableColumnList($table, $database, $tableColumns);
    }
}

@morozov
Copy link
Member

morozov commented Dec 12, 2021

Should be addressed by #2960.

@morozov morozov closed this as completed Dec 12, 2021
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants