Skip to content

Commit

Permalink
Merge pull request #11 from triadev/delete_migration_step
Browse files Browse the repository at this point in the history
Delete a migration step.
  • Loading branch information
triadev committed Oct 21, 2018
2 parents 24f9c9b + 9d1bdeb commit 0ee9af5
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/Business/Repository/ElasticsearchMigrationStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ public function find(int $migrationStepId): ?\Triadev\EsMigration\Models\Entity\
->first();
}

/**
* @inheritdoc
*/
public function delete(int $migrationStepId)
{
if ($migrationStep = $this->find($migrationStepId)) {
$migrationStep->delete();
}
}

private function dispatchStatus(\Triadev\EsMigration\Models\Entity\ElasticsearchMigrationStep $migration)
{
switch ($migration->status) {
Expand Down
9 changes: 9 additions & 0 deletions src/Contract/ElasticsearchMigrationContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public function addMigrationStep(
bool $stopOnFailure = true
) : bool;

/**
* Delete migration step
*
* @param int $migrationStepId
* @return bool
*/
public function deleteMigrationStep(int $migrationStepId) : bool;

/**
* Get migration status
*
Expand All @@ -41,6 +49,7 @@ public function addMigrationStep(
* 'status' => STRING,
* 'error' => STRING|NULL,
* 'steps' => [
* 'id' => INTEGER,
* 'type' => STRING,
* 'status' => INTEGER,
* 'error' => STRING|NULL,
Expand Down
1 change: 0 additions & 1 deletion src/Contract/Repository/ElasticsearchMigrationContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function find(string $migration) : ?ElasticsearchMigration;
* Delete
*
* @param string $migration
* @return bool
*
* @throws \Throwable
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@ public function update(
* @return null|ElasticsearchMigrationStep
*/
public function find(int $migrationStepId) : ?ElasticsearchMigrationStep;

/**
* Delete
*
* @param int $migrationStepId
*
* @throws \Throwable
*/
public function delete(int $migrationStepId);
}
23 changes: 19 additions & 4 deletions src/ElasticsearchMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ public function addMigrationStep(
return false;
}

/**
* Delete migration step
*
* @param int $migrationStepId
* @return bool
*/
public function deleteMigrationStep(int $migrationStepId) : bool
{
try {
$this->migrationStepRepository->delete($migrationStepId);
} catch (\Throwable $e) {
return false;
}

return true;
}

/**
* @inheritdoc
*/
Expand All @@ -97,11 +114,9 @@ public function getMigrationStatus(string $migration) : array

foreach ($migrationEntity->migrationSteps()->cursor() as $migrationStep) {
/** @var ElasticsearchMigrationStep $migrationStep */
$migrationStepData = array_except($migrationStep->toArray(), [
'id',
'migration_id'
]);
$migrationStepData = array_except($migrationStep->toArray(), ['migration_id']);

$migrationStepData['id'] = (int)$migrationStepData['id'];
$migrationStepData['status'] = (int)$migrationStepData['status'];
$migrationStepData['params'] = json_decode($migrationStepData['params'], true);
$migrationStepData['priority'] = (int)$migrationStepData['priority'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,20 @@ public function it_finds_a_migration()
$this->repository->find(1)
);
}

/**
* @test
*/
public function it_deletes_a_migration()
{
$this->repository->create(2, 'createIndex', [
'index' => 'phpunit'
]);

$this->assertInstanceOf(ElasticsearchMigrationStep::class, $this->repository->find(1));

$this->repository->delete(1);

$this->assertNull($this->repository->find(1));
}
}
37 changes: 31 additions & 6 deletions tests/integration/ElasticsearchMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ public function it_creates_a_migration()
public function it_adds_migration_steps_with_priority()
{
$this->assertTrue($this->migrationService->createMigration('phpunit'));

$this->assertEquals(
0,
$this->migrationRepository->find('phpunit')->migrationSteps()->count()
);

$this->addMigrationSteps();

$migrationSteps = $this->migrationRepository->find('phpunit')->migrationSteps()->getResults();

$this->assertEquals(7, count($migrationSteps));

$this->assertEquals(MigrationTypes::MIGRATION_TYPE_CREATE_INDEX, $migrationSteps[0]->type);
$this->assertEquals(MigrationTypes::MIGRATION_TYPE_UPDATE_INDEX_MAPPING, $migrationSteps[1]->type);
$this->assertEquals(MigrationTypes::MIGRATION_TYPE_DELETE_INDEX, $migrationSteps[2]->type);
Expand All @@ -90,6 +90,30 @@ public function it_adds_migration_steps_with_priority()
$this->assertEquals(MigrationTypes::MIGRATION_TYPE_UPDATE_BY_QUERY, $migrationSteps[6]->type);
}

/**
* @test
*/
public function it_deletes_a_migration_step()
{
$this->assertTrue($this->migrationService->createMigration('phpunit'));

$this->addMigrationSteps();

$this->assertEquals(
7,
$this->migrationRepository->find('phpunit')->migrationSteps()->count()
);

$this->migrationService->deleteMigrationStep(1);
$this->migrationService->deleteMigrationStep(2);
$this->migrationService->deleteMigrationStep(3);

$this->assertEquals(
4,
$this->migrationRepository->find('phpunit')->migrationSteps()->count()
);
}

/**
* @test
*/
Expand Down Expand Up @@ -144,7 +168,8 @@ public function it_gets_migration_status()
foreach ($result['steps'] as $step) {
$this->assertEquals(MigrationStatus::MIGRATION_STATUS_WAIT, $step['status']);
$this->assertEquals(null, $step['error']);


$this->assertTrue(is_int($step['id']));
$this->assertTrue(is_array($step['params']));
$this->assertTrue(is_int($step['priority']));
$this->assertTrue(is_bool($step['stop_on_failure']));
Expand Down

0 comments on commit 0ee9af5

Please sign in to comment.