【问题标题】:Doctrine Migrations: altering a column to be nullable in productionDoctrine Migrations:将列更改为在生产中可以为空
【发布时间】:2023-04-03 06:19:01
【问题描述】:

我对 Doctrine Migrations 还很陌生,如果这真的很明显,我深表歉意。

更新以提供更多信息

我有一个 Symfony2 实体映射如下:

/**
 * @ORM\Column(type="string")
 */
private $name;

这已添加到迁移中,并在一切正常运行的情况下进行部署。然后需要更新该列以接受空值,因此将其更改为:

/**
 * @ORM\Column(type="string", nullable=true)
 */
private $name;

问题在于这对生成的迁移文件没有影响:

$ php app/console cache:clear
$ php app/console doctrine:migrations:diff
$ tail -50 app/DoctrineMigrations/Version20131028205742.php

<?php

namespace Application\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
class Version20131028205742 extends AbstractMigration
{
    public function up(Schema $schema)
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql", "Migration can only be executed safely on 'mysql'.");

    }

    public function down(Schema $schema)
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql", "Migration can only be executed safely on 'mysql'.");

    }
}

没有添加任何 ALTER 语句来处理空值。我可以删除并重建数据库,但由于已经部署了这个特定的迁移,这将导致生产数据库出现问题。我可以看到这种情况将来会再次发生,所以想多了解一下。

这是 Doctrine 和/或 Symfony2 Doctrine Migrations 捆绑包的限制吗?有没有办法在不编写自定义迁移的情况下解决这个问题?

请注意,我的所有其他迁移工作正常,唯一的问题是向现有字段添加可为空的选项。

【问题讨论】:

  • 您在生成差异之前是否清除缓存?我认为这应该始终是发生学说映射问题时要问的第一个问题:D
  • 是的,我清除了缓存。我不会想到这是我的问题的途径,尽管其他实体更新都被很好地拉了出来,而不是用于可空值的 ALTER 语句。
  • resulting diff file 是什么意思?
  • 不会有diff file - 无论是指什么。您的差异是您发布的内容,然后您运行 doctrine:schema:update 这将更新您的数据库。
  • 正如我在回答中所说:如果您在更改实体后重建数据库,则差异将为空,因为数据库架构适合实体元数据。试试这个实验:1)将您的实体回滚为 nullable=false 2)重建您的数据库 3)将您的实体更改为 nullable=true 4)运行 diff(不重建数据库)。现在差异应该包含一个适当的改变。

标签: php mysql symfony doctrine-orm doctrine-migrations


【解决方案1】:

Doctrine 的差异以这种方式工作,它将您的实体元数据与现有数据库模式进行比较。因此,如果您在运行迁移之后运行doctrine:schema:update - 学说不会注意到任何变化,因为数据库架构已经更改(因此适合实体元数据)。基本上,在您运行迁移之后,您不再需要运行 update

【讨论】:

  • 感谢您的回答,我没有重建数据库,我以为我只使用了 diff 和 migrate 命令,但在回滚一点并按照您的步骤后,我得到了它的工作。我猜在某个时候,我一定是以错误的顺序运行命令并搞砸了。
猜你喜欢
  • 2011-04-03
  • 2018-02-05
  • 1970-01-01
  • 2013-09-10
  • 2015-10-03
  • 2011-08-23
  • 2014-08-16
  • 2011-09-26
  • 1970-01-01
相关资源
最近更新 更多