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 NULL get dropped when changing column type #4111

Open
unixslayer opened this issue Jun 26, 2020 · 1 comment
Open

DEFAULT NULL get dropped when changing column type #4111

unixslayer opened this issue Jun 26, 2020 · 1 comment

Comments

@unixslayer
Copy link

Bug Report

Q A
Version 2.10.2
Platform PostgreSQL 11.7

Summary

Changing column type from string to custom which extends string makes doctrine/migrations generate migration which drops DEFAULT NULL. It also ignores that length was not changed.

Initial column annotation:

/**
 * @ORM\Column(name="context", type="string", length=64, nullable=true)
 */
public ?string $context;

I created custom type wchich extends \Doctrine\DBAL\Types\StringType overwriting only convertToPHPValue and convertToDatabaseValue. Annotation was changed into this:

/**
 * @ORM\Column(name="context", type="attachment_context", length=64, nullable=true)
 */
public ?AttachmentContext $context;

Current behaviour

Running doctrine:migrations:diff command generates following SQLs:

ALTER TABLE attachment.attachments ALTER context TYPE VARCHAR(64);
ALTER TABLE attachment.attachments ALTER context DROP DEFAULT;

Expected behaviour

I expect that columns default value will not be changed.

Possible solution

I found this part in \Doctrine\DBAL\Platforms\PostgreSqlPlatform

//Doctrine\DBAL\Platforms\PostgreSqlPlatform:558
if ($columnDiff->hasChanged('default') || $this->typeChangeBreaksDefaultValue($columnDiff)) {
    $defaultClause = $column->getDefault() === null
        ? ' DROP DEFAULT'
        : ' SET' . $this->getDefaultValueDeclarationSQL($column->toArray());
    $query         = 'ALTER ' . $oldColumnName . $defaultClause;
    $sql[]         = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
}

Debugging shows that default did not changed and of course type did. But comparing $column->getDefault() with null when in fact it is null and then dropping it seems odd. Removing that comparison seems to generate proper SQL:

if ($columnDiff->hasChanged('default') || $this->typeChangeBreaksDefaultValue($columnDiff)) {
    $defaultClause = ' SET' . $this->getDefaultValueDeclarationSQL($column->toArray());
    $query         = 'ALTER ' . $oldColumnName . $defaultClause;
    $sql[]         = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
}
@allan-simon
Copy link
Contributor

I'm hitting the same issue with GENERATED column

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

No branches or pull requests

4 participants