9

我正在开发一个在开发环境中没有生产数据库副本的项目。

有时我们会遇到数据库迁移问题——它们传递了开发数据库,​​但在生产/测试中失败。

这通常是因为开发环境数据是从使用最新实体的夹具加载的 - 正确填充所有表。

有什么简单的方法可以确保 Doctrine Migration(s) 在生产中通过?

您是否有/知道任何方法来编写自动测试,以确保数据将被正确迁移,而无需下载生产/测试数据库并手动运行迁移?

我想避免将生产/测试数据库下载到开发机器,这样我就可以检查迁移,因为数据库包含私有数据并且它可能非常大。

4

2 回答 2

5

首先,您需要在迁移前的状态下创建示例数据库转储。对于 MySQL,请使用 mysqldump。对于 postgres pg_dump,例如:

mysqldump -u root -p mydatabase > dump-2018-02-20.sql
pg_dump -Upostgres --inserts --encoding utf8 -f dump-2018-02-20.sql mydatabase

然后为所有迁移测试创建一个抽象类(我假设您已经为集成测试配置了一个单独的数据库config_test.yml):

abstract class DatabaseMigrationTestCase extends WebTestCase {
    /** @var ResettableContainerInterface */
    protected $container;
    /** @var Application */
    private $application;

    protected function setUp() {
        $this->container = self::createClient()->getContainer();
        $kernel = $this->container->get('kernel');
        $this->application = new Application($kernel);
        $this->application->setAutoExit(false);
        $this->application->setCatchExceptions(false);

        $em = $this->container->get(EntityManagerInterface::class);
        $this->executeCommand('doctrine:schema:drop --force');
        $em->getConnection()->exec('DROP TABLE IF EXISTS public.migration_versions');
    }

    protected function loadDump(string $name) {
        $em = $this->container->get(EntityManagerInterface::class);
        $em->getConnection()->exec(file_get_contents(__DIR__ . '/dumps/dump-' . $name . '.sql'));
    }

    protected function executeCommand(string $command): string {
        $input = new StringInput("$command --env=test");
        $output = new BufferedOutput();
        $input->setInteractive(false);
        $returnCode = $this->application->run($input, $output);
        if ($returnCode != 0) {
            throw new \RuntimeException('Failed to execute command. ' . $output->fetch());
        }
        return $output->fetch();
    }

    protected function migrate(string $toVersion = '') {
        $this->executeCommand('doctrine:migrations:migrate ' . $toVersion);
    }
}

迁移测试示例:

class Version20180222232445_MyMigrationTest extends DatabaseMigrationTestCase {
    /** @before */
    public function prepare() {
        $this->loadDump('2018-02-20');
        $this->migrate('20180222232445');
    }

    public function testMigratedSomeData() {
        $em = $this->container->get(EntityManagerInterface::class);
        $someRow = $em->getConnection()->executeQuery('SELECT * FROM myTable WHERE id = 1')->fetch();
        $this->assertEquals(1, $someRow['id']);
        // check other stuff if it has been migrated correctly
    }
}
于 2018-02-20T20:29:06.437 回答
1

我已经为 Doctrine Migrations 找到了简单的“烟雾测试”。

我有 PHPUnit 测试执行以下步骤:

  • 跌落测试数据库
  • 创建测试数据库
  • 加载迁移(创建架构)
  • 加载夹具(模仿生产数据)
  • 迁移到一些旧版本
  • 迁移回最新版本

这样我就可以测试我们最近遇到的主要问题。

PHPUnit 测试的示例可以在我的博客上找到:http: //damiansromek.pl/2015/09/29/how-to-test-doctrine-migrations/

于 2015-09-29T12:36:39.843 回答