2015-09-28 7 views
6

私は、開発環境で本番DBのコピーを持っていないプロジェクトに取り組んでいます。Doctrineの移行をテストするには?

時々私達はDBの移行に問題があります - devのDBを渡しますが、生産/テストに失敗します。

デベロッパー環境データは、最新のエンティティを使用するフィクスチャからロードされるため、すべてのテーブルが正しく埋められます。

Doctrineの移行が本番環境で確実に行われるようにする簡単な方法はありますか?

実稼働/テスト用のDBをダウンロードせずに手動で移行を実行しなくても、データが正しく移行されるように自動テストを行う方法はありますか?

私は生産/テスト用のDBをマシンにダウンロードしないようにしたいので、DBにはプライベートなデータが入っていて、かなり大きい可能性があります。

+0

Gitを使用している場合は、最後の製品バージョンに戻って、db、fuxture、移行を再作成し、最新のmigraを実行するために必要なバージョンに移動してください。しかし、信頼性の高い唯一のテストは、残念なことにプロデュースされたデータです。 –

答えて

2

まず、あなたは、移行前の状態にあるサンプル・データベースのダンプを作成する必要があります。 MySQLの場合はmysqldumpを使用します。 Postgresのpg_dumpは、例えばの場合:

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); 
    } 
} 

例のマイグレーション試験:

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に統合テスト用に別のデータベースを設定していると仮定)のための抽象クラスを作成

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 
    } 
} 
1

私はDoctrine Migrationsの簡単な「スモークテスト」を理解しました。

IはPHPUnitのテストperfoming以下のステップを有する:

  • 落下試験DBを
  • (スキーマを作成する)
  • 負荷器具(生産データを模倣)試験DBを
  • ロード移行を作成
  • 移行古いバージョンに戻す
  • 最新バージョンにマイグレーション

このように私は主要な問題をテストすることができます。 PHPUnitテストの

例は、私のブログで見ることができます:http://damiansromek.pl/2015/09/29/how-to-test-doctrine-migrations/

+2

あなたのリンクは404エラーをスローします... – Hornth

関連する問題