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

[DBAL-1087] Length of fixed string type (char) is ignored on Postgre schema update #751

Closed
wants to merge 2 commits into from
Closed

Conversation

SenseException
Copy link
Member

This PR should fix an issue for the fixed string type columns using PostgreSQL.

I've created a basic Symfony 2.6. Application and using a PostgreSQL 9.3. database. In this dummy project, I've created the following Entity:

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="test")
 */
class Test
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="id")
     * @ORM\GeneratedValue
     */
    public $id;

    /**
     * @ORM\Column(type="string", name="name", length=2, options={"fixed" = true})
     */
    public $name;
}

The table is successfully created after a schema update, but when another schema update is executed without any changes to the entity, an ALTER query is executed:

ALTER TABLE test ALTER name TYPE CHAR(2);

After I searched in the code of doctrine dbal, I discovered that the length of the char column is not fetched correctly in PostgreSqlSchemaManager::_getPortableTableColumnDefinition(). In my example, length results as null and will be later set to 255 in the Comparator, which isn't the correct length from the database column. The comparation of the schemas results in a change of char-length.

For simplicity, I extended the if-condition in PostgreSqlSchemaManager to:

if (strtolower($tableColumn['type']) === 'varchar' || strtolower($tableColumn['type']) === 'bpchar') {

The fix I've contributed does help to parse the length in my case, but when I look into PostgreSqlPlatform::initializeDoctrineTypeMappings() there could be other types with the same problem, that are mapped to the doctrine string type.

// ... part of PostgreSqlPlatform::initializeDoctrineTypeMappings()
            'varchar'       => 'string',
            'interval'      => 'string',
            '_varchar'      => 'string',
            'char'          => 'string',
            'bpchar'        => 'string',
            'inet'          => 'string',

I guess we need to cover char and the others too.

PostgreSqlSchemaManagerTest also doesn't cover existing code. How is this going to be tested? Does the SchemaManagers need some tests in general?

@doctrinebot
Copy link

Hello,

thank you for creating this pull request. I have automatically opened an issue
on our Jira Bug Tracker for you. See the issue link:

http://www.doctrine-project.org/jira/browse/DBAL-1087

We use Jira to track the state of pull requests and the versions they got
included in.

@SenseException SenseException changed the title Length of fixed string type (char) is ignored on Postgre schema update {DBAL-1087] Length of fixed string type (char) is ignored on Postgre schema update Dec 19, 2014
@SenseException SenseException changed the title {DBAL-1087] Length of fixed string type (char) is ignored on Postgre schema update [DBAL-1087] Length of fixed string type (char) is ignored on Postgre schema update Dec 19, 2014
@stof
Copy link
Member

stof commented Dec 19, 2014

Please add a test covering this

@SenseException
Copy link
Member Author

@stof It sure need a test for my case, but there need to be more tests for the SchemaManagers at all.

But I'm going to add one for this issue first.

@SenseException
Copy link
Member Author

@stof A test for my case is now added.

@deeky666 deeky666 closed this in 7d88ff7 Dec 31, 2014
@deeky666
Copy link
Member

Adjusted the test case and merged manually, thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants