Skip to content

Commit

Permalink
Catch an exception and update the migration status to error.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Lorke committed Sep 16, 2018
1 parent 942eceb commit e6cbe74
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
55 changes: 32 additions & 23 deletions src/ElasticsearchMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,36 +60,45 @@ private function buildElasticsearchClient() : Client
*/
public function migrate(string $version)
{
if ($this->migrationRepository->find($version)) {
$migration = $this->migrationRepository->find($version);
if ($migration && $migration->getAttribute('status') == 'done') {
throw new MigrationAlreadyDone();
}

$migrations = $this->buildMigrations($version);

foreach ($migrations as $migration) {
if ($migration->getType()) {
switch ($migration->getType()) {
case 'create':
(new CreateIndex())->migrate($this->esClient, $migration);
break;
case 'update':
(new UpdateIndex())->migrate($this->esClient, $migration);
break;
case 'delete':
(new DeleteIndex())->migrate($this->esClient, $migration);
break;
default:
break;
try {
$migrations = $this->buildMigrations($version);

if (!empty($migrations)) {
foreach ($migrations as $migration) {
if ($migration->getType()) {
switch ($migration->getType()) {
case 'create':
(new CreateIndex())->migrate($this->esClient, $migration);
break;
case 'update':
(new UpdateIndex())->migrate($this->esClient, $migration);
break;
case 'delete':
(new DeleteIndex())->migrate($this->esClient, $migration);
break;
default:
break;
}
}

(new \Triadev\EsMigration\Business\Migration\Alias())->migrate($this->esClient, $migration);
(new \Triadev\EsMigration\Business\Migration\Reindex())->migrate($this->esClient, $migration);
(new \Triadev\EsMigration\Business\Migration\DeleteByQuery())->migrate($this->esClient, $migration);
(new \Triadev\EsMigration\Business\Migration\UpdateByQuery())->migrate($this->esClient, $migration);
}

$this->migrationRepository->createOrUpdate($version, 'done');
}
} catch (\Exception $e) {
$this->migrationRepository->createOrUpdate($version, 'error');

(new \Triadev\EsMigration\Business\Migration\Alias())->migrate($this->esClient, $migration);
(new \Triadev\EsMigration\Business\Migration\Reindex())->migrate($this->esClient, $migration);
(new \Triadev\EsMigration\Business\Migration\DeleteByQuery())->migrate($this->esClient, $migration);
(new \Triadev\EsMigration\Business\Migration\UpdateByQuery())->migrate($this->esClient, $migration);
throw $e;
}

$this->migrationRepository->createOrUpdate($version, 'done');
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/ElasticsearchMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,24 @@ public function it_throws_an_exception_if_migration_already_done()

$this->service->migrate('1.0.0');
}

/**
* @test
*/
public function it_updates_migration_status_from_error_to_done()
{
$this->migrationRepository->createOrUpdate('1.0.0', 'error');

$this->assertEquals(
'error',
$this->migrationRepository->find('1.0.0')->getAttribute('status')
);

$this->service->migrate('1.0.0');

$this->assertEquals(
'done',
$this->migrationRepository->find('1.0.0')->getAttribute('status')
);
}
}

0 comments on commit e6cbe74

Please sign in to comment.