Skip to content

Commit

Permalink
Added status to migration steps.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Lorke committed Oct 14, 2018
1 parent 7781bf2 commit d1d33fe
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 17 deletions.
39 changes: 39 additions & 0 deletions src/Business/Repository/ElasticsearchMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Triadev\EsMigration\Business\Repository;

use Triadev\EsMigration\Contract\Repository\ElasticsearchMigrationsContract;
use Triadev\EsMigration\Exception\MigrationsNotExist;

class ElasticsearchMigrations implements ElasticsearchMigrationsContract
{
Expand All @@ -18,12 +19,34 @@ public function create(
$dbMigration->migration_id = $migrationId;
$dbMigration->type = $type;
$dbMigration->index = $index;
$dbMigration->status = self::ELASTICSEARCH_MIGRATION_STEP_STATUS_WAIT;

$dbMigration->saveOrFail();

return $dbMigration;
}

/**
* @inheritdoc
*/
public function update(
int $migrationsId,
int $status
): \Triadev\EsMigration\Models\Entity\ElasticsearchMigrations {
$entity = $this->find($migrationsId);
if (!$entity) {
throw new MigrationsNotExist();
}

if ($this->isStatusValid($status)) {
$entity->status = $status;
}

$entity->saveOrFail();

return $entity;
}

/**
* @inheritdoc
*/
Expand All @@ -32,4 +55,20 @@ public function find(int $migrationsId): ?\Triadev\EsMigration\Models\Entity\Ela
return \Triadev\EsMigration\Models\Entity\ElasticsearchMigrations::where('id', $migrationsId)
->first();
}

private function isStatusValid(int $status) : bool
{
$valid = [
self::ELASTICSEARCH_MIGRATION_STEP_STATUS_WAIT,
self::ELASTICSEARCH_MIGRATION_STEP_STATUS_RUNNING,
self::ELASTICSEARCH_MIGRATION_STEP_STATUS_DONE,
self::ELASTICSEARCH_MIGRATION_STEP_STATUS_ERROR
];

if (in_array($status, $valid)) {
return true;
}

return false;
}
}
26 changes: 25 additions & 1 deletion src/Contract/Repository/ElasticsearchMigrationsContract.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?php
namespace Triadev\EsMigration\Contract\Repository;

use Triadev\EsMigration\Exception\MigrationsNotExist;
use Triadev\EsMigration\Models\Entity\ElasticsearchMigrations;

interface ElasticsearchMigrationsContract
{
const ELASTICSEARCH_MIGRATION_STEP_STATUS_WAIT = 0;
const ELASTICSEARCH_MIGRATION_STEP_STATUS_RUNNING = 1;
const ELASTICSEARCH_MIGRATION_STEP_STATUS_DONE = 2;
const ELASTICSEARCH_MIGRATION_STEP_STATUS_ERROR = 3;

/**
* Create
*
Expand All @@ -15,7 +21,25 @@ interface ElasticsearchMigrationsContract
*
* @throws \Throwable
*/
public function create(int $migrationId, string $type, string $index) : ElasticsearchMigrations;
public function create(
int $migrationId,
string $type,
string $index
) : ElasticsearchMigrations;

/**
* Update
*
* @param int $migrationsId
* @param int $status
* @return ElasticsearchMigrations
*
* @throws MigrationsNotExist
*/
public function update(
int $migrationsId,
int $status
) : ElasticsearchMigrations;

/**
* Find
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function up()
$table->bigInteger('migration_id');
$table->string('type');
$table->string('index');
$table->integer('status');
$table->timestamps();

$table->foreign('migration_id')->references('id')->on('triadev_elasticsearch_migration');
Expand Down
9 changes: 7 additions & 2 deletions src/ElasticsearchMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Triadev\EsMigration\Models\Migrations\Reindex as ReindexModel;

use Triadev\EsMigration\Contract\Repository\ElasticsearchMigrationContract as EsMigrationRepositoryInterface;
use Triadev\EsMigration\Contract\Repository\ElasticsearchMigrationsContract as EsMigrationsRepositoryInterface;

class ElasticsearchMigration implements ElasticsearchMigrationContract
{
Expand All @@ -32,9 +33,12 @@ class ElasticsearchMigration implements ElasticsearchMigrationContract
/** @var string|null */
private $filePathMigrations;

/** @var \Triadev\EsMigration\Contract\Repository\ElasticsearchMigrationContract */
/** @var EsMigrationRepositoryInterface */
private $migrationRepository;

/** @var EsMigrationsRepositoryInterface */
private $migrationsRepository;

/**
* ElasticsearchMigration constructor.
*/
Expand All @@ -45,6 +49,7 @@ public function __construct()
$this->filePathMigrations = config('triadev-elasticsearch-migration.migration.filePath');

$this->migrationRepository = app(EsMigrationRepositoryInterface::class);
$this->migrationsRepository = app(EsMigrationsRepositoryInterface::class);
}

private function buildElasticsearchClient() : Client
Expand Down Expand Up @@ -92,7 +97,7 @@ public function migrate(string $version, string $source = 'file')
}

if (!empty($migrations)) {
foreach ($migrations as $migration) {
foreach ($migrations as $migrationsId => $migration) {
switch (get_class($migration)) {
case CreateIndexModel::class:
(new CreateIndex())->migrate($this->esClient, $migration);
Expand Down
14 changes: 7 additions & 7 deletions src/ElasticsearchMigrationDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function getMigration(string $migration) : array
if ($migrationByType instanceof ElasticsearchMigrationsCreateIndex) {
$settings = $migrationByType->settings;

$migrations[] = MigrationBuilder::createIndex(
$migrations[$dbMigration->id] = MigrationBuilder::createIndex(
$index,
json_decode($migrationByType->mappings, true),
$settings != null ? json_decode($settings, true) : null
Expand All @@ -140,7 +140,7 @@ public function getMigration(string $migration) : array
$mappings = $migrationByType->mappings;
$settings = $migrationByType->settings;

$migrations[] = MigrationBuilder::updateIndex(
$migrations[$dbMigration->id] = MigrationBuilder::updateIndex(
$index,
$mappings != null ? json_decode($mappings, true) : null,
$settings != null ? json_decode($settings, true) : null,
Expand All @@ -149,15 +149,15 @@ public function getMigration(string $migration) : array
}
break;
case self::MIGRATION_TYPE_DELETE_INDEX:
$migrations[] = MigrationBuilder::deleteIndex($index);
$migrations[$dbMigration->id] = MigrationBuilder::deleteIndex($index);
break;
case self::MIGRATION_TYPE_ALIAS:
if ($migrationByType instanceof ElasticsearchMigrationsAlias) {
$add = $migrationByType->add;
$remove = $migrationByType->remove;
$removeIndices = $migrationByType->remove_indices;

$migrations[] = MigrationBuilder::alias(
$migrations[$dbMigration->id] = MigrationBuilder::alias(
$index,
$add != null ? json_decode($add, true) : null,
$remove != null ? json_decode($remove, true) : null,
Expand All @@ -167,7 +167,7 @@ public function getMigration(string $migration) : array
break;
case self::MIGRATION_TYPE_DELETE_BY_QUERY:
if ($migrationByType instanceof ElasticsearchMigrationsDeleteByQuery) {
$migrations[] = MigrationBuilder::deleteByQuery(
$migrations[$dbMigration->id] = MigrationBuilder::deleteByQuery(
$index,
json_decode($migrationByType->query, true),
$migrationByType->type,
Expand All @@ -179,7 +179,7 @@ public function getMigration(string $migration) : array
if ($migrationByType instanceof ElasticsearchMigrationsUpdateByQuery) {
$script = $migrationByType->script;

$migrations[] = MigrationBuilder::updateByQuery(
$migrations[$dbMigration->id] = MigrationBuilder::updateByQuery(
$index,
json_decode($migrationByType->query, true),
$migrationByType->type,
Expand All @@ -190,7 +190,7 @@ public function getMigration(string $migration) : array
break;
case self::MIGRATION_TYPE_REINDEX:
if ($migrationByType instanceof ElasticsearchMigrationsReindex) {
$migrations[] = MigrationBuilder::reindex(
$migrations[$dbMigration->id] = MigrationBuilder::reindex(
$index,
$migrationByType->dest_index,
(bool)$migrationByType->refresh_source_index,
Expand Down
18 changes: 18 additions & 0 deletions src/Exception/MigrationsNotExist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Triadev\EsMigration\Exception;

use Throwable;

class MigrationsNotExist extends \Exception
{
/**
* MigrationsNotExist constructor.
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(string $message = "", int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
1 change: 1 addition & 0 deletions src/Models/Entity/ElasticsearchMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @property integer $migration_id
* @property string $type
* @property string $index
* @property integer $status
*/
class ElasticsearchMigrations extends Model
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,42 @@ public function it_creates_a_migration()
$this->assertEquals(2, ElasticsearchMigrations::where('migration_id', '=', 2)->count());
}

/**
* @test
*/
public function it_updates_a_migration()
{
$this->assertNull($this->repository->find(1));

$this->repository->create(2, 'createIndex', 'phpunit');
$this->assertEquals(ElasticsearchMigrationsContract::ELASTICSEARCH_MIGRATION_STEP_STATUS_WAIT, $this->repository->find(1)->status);

// Running
$this->repository->update(1, ElasticsearchMigrationsContract::ELASTICSEARCH_MIGRATION_STEP_STATUS_RUNNING);
$this->assertEquals(ElasticsearchMigrationsContract::ELASTICSEARCH_MIGRATION_STEP_STATUS_RUNNING, $this->repository->find(1)->status);

// Error
$this->repository->update(1, ElasticsearchMigrationsContract::ELASTICSEARCH_MIGRATION_STEP_STATUS_ERROR);
$this->assertEquals(ElasticsearchMigrationsContract::ELASTICSEARCH_MIGRATION_STEP_STATUS_ERROR, $this->repository->find(1)->status);

// Done
$this->repository->update(1, ElasticsearchMigrationsContract::ELASTICSEARCH_MIGRATION_STEP_STATUS_DONE);
$this->assertEquals(ElasticsearchMigrationsContract::ELASTICSEARCH_MIGRATION_STEP_STATUS_DONE, $this->repository->find(1)->status);

// Done => invalid status id
$this->repository->update(1, 999);
$this->assertEquals(ElasticsearchMigrationsContract::ELASTICSEARCH_MIGRATION_STEP_STATUS_DONE, $this->repository->find(1)->status);
}

/**
* @test
* @expectedException \Triadev\EsMigration\Exception\MigrationsNotExist
*/
public function it_throws_an_exception_when_migrations_not_exist_at_update()
{
$this->repository->update(1, ElasticsearchMigrationsContract::ELASTICSEARCH_MIGRATION_STEP_STATUS_DONE);
}

/**
* @test
*/
Expand Down
14 changes: 7 additions & 7 deletions tests/integration/ElasticsearchMigrationDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,12 @@ public function it_gets_migration()

$this->assertCount(7, $migrations);

$this->assertInstanceOf(CreateIndex::class, $migrations[0]);
$this->assertInstanceOf(UpdateIndex::class, $migrations[1]);
$this->assertInstanceOf(DeleteIndex::class, $migrations[2]);
$this->assertInstanceOf(Alias::class, $migrations[3]);
$this->assertInstanceOf(DeleteByQuery::class, $migrations[4]);
$this->assertInstanceOf(UpdateByQuery::class, $migrations[5]);
$this->assertInstanceOf(Reindex::class, $migrations[6]);
$this->assertInstanceOf(CreateIndex::class, $migrations[2]);
$this->assertInstanceOf(UpdateIndex::class, $migrations[3]);
$this->assertInstanceOf(DeleteIndex::class, $migrations[4]);
$this->assertInstanceOf(Alias::class, $migrations[5]);
$this->assertInstanceOf(DeleteByQuery::class, $migrations[6]);
$this->assertInstanceOf(UpdateByQuery::class, $migrations[7]);
$this->assertInstanceOf(Reindex::class, $migrations[8]);
}
}

0 comments on commit d1d33fe

Please sign in to comment.