mysql - 为什么 Doctrine 2 迁移在 Symfony 项目中悄无声息地失败了?

标签 mysql doctrine-orm doctrine symfony

我的问题是:

  1. 为什么 Symfony 中的 doctrine 迁移命令无法报告我在下面列出的 SQL 错误
  2. 我怎样才能让它报告此类错误,而不是看起来它已经成功了。

明确一点 - 我知道 SQL 本身失败的原因。这是最简单的部分。失败的无声本质就是问题所在。

我在 PHP 文件中有一个 Doctrine 迁移,如下所示:

[... boilerplate ...]

public function up(Schema $schema)
{
    $this->addSql(<<<SQL

    -- This runs fine
    CREATE TABLE [[redacted - this part runs fine]];

    -- This fails with a unique key constraint
    INSERT INTO `users_orgs` (`org_id`, `user_id`, `active`)
    SELECT `oid`, `uid`, 1 as `active` FROM `orgs`;

    -- Never gets here
    ALTER TABLE `orgs` CHANGE `uid` `primary_user_id` int(11) unsigned DEFAULT NULL;

SQL
    );
}

[... boilerplate ...]

实际结果

第二条语句因唯一键约束而失败(例如“键‘PRIMARY’的重复条目‘6-3’”——这是一个复合键)但未报告错误。我知道它失败了,因为很明显最终语句永远不会运行,如果我手动运行有问题的语句,它就会失败。当我运行时:

./bin/console doctrine:migrations:migrate

在我的 Symfony 应用程序中,调用 doctrine 迁移代码,它报告:

$ ./bin/console doctrine:migrations:migrate

                Application Migrations                    


WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)y
Migrating up to 20160822090333 from 20160820232349

  ++ migrating 20160822090333

     -> CREATE TABLE [[redacted - works fine]];

INSERT INTO `users_orgs` (`org_id`, `user_id`, `active`)
SELECT `oid`, `uid`, 1 as `active` FROM `orgs`;

ALTER TABLE `orgs` CHANGE `uid` `primary_user_id` int(11) unsigned  DEFAULT NULL;


  ++ migrated (0.1s)

  ------------------------

  ++ finished in 0.1s
  ++ 1 migrations executed
  ++ 1 sql queries

预期结果

我期待它报告错误。作为测试,我创建了一个带有主键的单列表。我试图插入一条记录,该记录会在迁移中生成唯一的约束冲突。当我执行迁移时, Doctrine 报告如下:

Migration 20160908112332 failed during Execution. Error An exception occurred while executing '        insert into FOO values (1)':

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'

我的配置

这是我安装的(通过 composer):

doctrine/annotations                 v1.2.7             Docblock Annotations Parser
doctrine/cache                       v1.6.0             Caching library offering an object-oriented API for many cache backends
doctrine/collections                 v1.3.0             Collections Abstraction library
doctrine/common                      v2.6.1             Common Library for Doctrine projects
doctrine/dbal                        v2.5.4             Database Abstraction Layer
doctrine/doctrine-bundle             1.6.4              Symfony DoctrineBundle
doctrine/doctrine-cache-bundle       1.3.0              Symfony Bundle for Doctrine Cache
doctrine/doctrine-migrations-bundle  v1.2.0             Symfony DoctrineMigrationsBundle
doctrine/inflector                   v1.1.0             Common String Manipulations with regard to casing and singular/plural rules.
doctrine/lexer                       v1.0.1             Base library for a lexer that can be used in Top-Down, Recursive Descent Pa...
doctrine/migrations                  1.4.1              Database Schema migrations using Doctrine DBAL
symfony/symfony                      v3.0.9

MySQL:

mysql Ver 14.14 Distrib 5.6.27, for Linux (x86_64) using  EditLine wrapper

我尝试过的

我在 google 和这里​​搜索了“silent doctrine failure”、“silent doctrine migration failure”,但没有看到任何内容。任何见解将不胜感激。

最佳答案

这与在一个查询中执行多个语句有关。

Doctrine 迁移 documentation说:

Internally the addSql call are passed to the dbal executeQuery method.

我尝试使用 executeQuery 执行多个语句,它的行为方式与您相同。 (我有不同的包版本,但只是补丁级别不同)。当我有多个语句时,它只会在第一个语句失败时抛出错误。

考虑这个例子:

/** @var \Doctrine\DBAL\Connection $connection */
$connection = $this->getDoctrine()->getConnection();

$createTable = "CREATE TABLE test (org_id int not null, user_id int not null, PRIMARY KEY(org_id, user_id));";

$insert = "INSERT INTO test (org_id, user_id) VALUES (1, 1);";

// this will create the table (if doesn't exist) and add a record, the second insert silently fails.
$connection->executeQuery("{$createTable}{$insert}{$insert}"); 

但是:

// ... same as above except for executeQuery
$connection->executeQuery("{$createTable}");
$connection->executeQuery("{$insert}");
$connection->executeQuery("{$insert}"); // <- duplicate key error is thrown

我想要达到的目的是为每个语句使用单独的 addSql() 语句。

[... boilerplate ...]

public function up(Schema $schema)
{
    $this->addSql("CREATE TABLE [[redacted - this part runs fine]];");

    $this->addSql("INSERT INTO `users_orgs` (`org_id`, `user_id`, `active`) SELECT `oid`, `uid`, 1 as `active` FROM `orgs`;");

    $this->addSql("ALTER TABLE `orgs` CHANGE `uid` `primary_user_id` int(11) unsigned DEFAULT NULL;");
}

[... boilerplate ...]

关于mysql - 为什么 Doctrine 2 迁移在 Symfony 项目中悄无声息地失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39395262/

相关文章:

MySQL - 在 SELECT 子句中

php - 正确使用MySQL JOIN

mysql - 在sql中按月分离租金

php - 如果不存在更高的id,如何设置mysql where子句返回到id=1

php - Doctrine :渴望加载懒惰关系的关系?

php - ManyToMany 没有删除

symfony - 学说架构更新或学说迁移

php - PDO 如何知道 MySQL 中最后插入的 id?

symfony - 在 symfony2 中寻找 Controller Action 的@transaction

schema - Doctrine schema.yml 生成器