Skip to content

Commit

Permalink
Added status to migrations table.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Lorke committed Oct 14, 2018
1 parent d8f3527 commit 2a417e8
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 307 deletions.
32 changes: 31 additions & 1 deletion src/Business/Repository/ElasticsearchMigration.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Triadev\EsMigration\Business\Repository;

use Illuminate\Database\Eloquent\Collection;
use Triadev\EsMigration\Contract\Repository\ElasticsearchMigrationContract;

class ElasticsearchMigration implements ElasticsearchMigrationContract
Expand All @@ -9,7 +10,8 @@ class ElasticsearchMigration implements ElasticsearchMigrationContract
* @inheritdoc
*/
public function createOrUpdate(
string $migration
string $migration,
int $status = self::ELASTICSEARCH_MIGRATION_STATUS_WAIT
): \Triadev\EsMigration\Models\Entity\ElasticsearchMigration {
$dbMigration = $this->find($migration);

Expand All @@ -18,6 +20,10 @@ public function createOrUpdate(
$dbMigration->migration = $migration;
}

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

$dbMigration->saveOrFail();

return $dbMigration;
Expand All @@ -42,4 +48,28 @@ public function delete(string $migration)
$migration->delete();
}
}

/**
* @inheritdoc
*/
public function all(array $columns = ['*']): Collection
{
return \Triadev\EsMigration\Models\Entity\ElasticsearchMigration::all($columns);
}

private function isStatusValid(int $status) : bool
{
$valid = [
self::ELASTICSEARCH_MIGRATION_STATUS_WAIT,
self::ELASTICSEARCH_MIGRATION_STATUS_RUNNING,
self::ELASTICSEARCH_MIGRATION_STATUS_DONE,
self::ELASTICSEARCH_MIGRATION_STATUS_ERROR
];

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

return false;
}
}
56 changes: 0 additions & 56 deletions src/Business/Repository/ElasticsearchMigrationStatus.php

This file was deleted.

13 changes: 9 additions & 4 deletions src/Console/Commands/ShowMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Triadev\EsMigration\Console\Commands;

use Illuminate\Console\Command;
use Triadev\EsMigration\Contract\Repository\ElasticsearchMigrationStatusContract;
use Triadev\EsMigration\Contract\Repository\ElasticsearchMigrationContract;

class ShowMigration extends Command
{
Expand All @@ -25,13 +25,18 @@ class ShowMigration extends Command
/**
* Execute the console command.
*
* @param ElasticsearchMigrationStatusContract $elasticsearchMigrationStatusRepository
* @param ElasticsearchMigrationContract $elasticsearchMigrationStatusRepository
*/
public function handle(ElasticsearchMigrationStatusContract $elasticsearchMigrationStatusRepository)
public function handle(ElasticsearchMigrationContract $elasticsearchMigrationStatusRepository)
{
$sortField = $this->argument('sortField');

$migrations = $elasticsearchMigrationStatusRepository->all(['migration', 'status', 'created_at', 'updated_at']);
$migrations = $elasticsearchMigrationStatusRepository->all([
'migration',
'status',
'created_at',
'updated_at'
]);

switch ($this->argument('sortOrder')) {
case 'asc':
Expand Down
20 changes: 19 additions & 1 deletion src/Contract/Repository/ElasticsearchMigrationContract.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
<?php
namespace Triadev\EsMigration\Contract\Repository;

use Illuminate\Database\Eloquent\Collection;
use Triadev\EsMigration\Models\Entity\ElasticsearchMigration;

interface ElasticsearchMigrationContract
{
const ELASTICSEARCH_MIGRATION_STATUS_WAIT = 0;
const ELASTICSEARCH_MIGRATION_STATUS_RUNNING = 1;
const ELASTICSEARCH_MIGRATION_STATUS_DONE = 2;
const ELASTICSEARCH_MIGRATION_STATUS_ERROR = 3;

/**
* Create or update
*
* @param string $migration
* @param int $status
* @return ElasticsearchMigration
*
* @throws \Throwable
*/
public function createOrUpdate(string $migration) : ElasticsearchMigration;
public function createOrUpdate(
string $migration,
int $status = self::ELASTICSEARCH_MIGRATION_STATUS_WAIT
) : ElasticsearchMigration;

/**
* Find
Expand All @@ -32,4 +42,12 @@ public function find(string $migration) : ?ElasticsearchMigration;
* @throws \Throwable
*/
public function delete(string $migration);

/**
* All
*
* @param array $columns
* @return Collection
*/
public function all(array $columns = ['*']) : Collection;
}
45 changes: 0 additions & 45 deletions src/Contract/Repository/ElasticsearchMigrationStatusContract.php

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function up()
Schema::create('triadev_elasticsearch_migration', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('migration');
$table->integer('status');
$table->timestamps();
});
}
Expand Down
21 changes: 14 additions & 7 deletions src/ElasticsearchMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Triadev\EsMigration\Models\Migrations\UpdateByQuery as UpdateByQueryModel;
use Triadev\EsMigration\Models\Migrations\Reindex as ReindexModel;

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

class ElasticsearchMigration implements ElasticsearchMigrationContract
{
/** @var Client */
Expand All @@ -30,7 +32,7 @@ class ElasticsearchMigration implements ElasticsearchMigrationContract
/** @var string|null */
private $filePathMigrations;

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

/**
Expand All @@ -42,9 +44,7 @@ public function __construct()

$this->filePathMigrations = config('triadev-elasticsearch-migration.migration.filePath');

$this->migrationRepository = app(
\Triadev\EsMigration\Contract\Repository\ElasticsearchMigrationStatusContract::class
);
$this->migrationRepository = app(EsMigrationRepositoryInterface::class);
}

private function buildElasticsearchClient() : Client
Expand All @@ -71,7 +71,8 @@ private function buildElasticsearchClient() : Client
public function migrate(string $version, string $source = 'file')
{
$migration = $this->migrationRepository->find($version);
if ($migration && $migration->status == 'done') {
if ($migration &&
$migration->status == EsMigrationRepositoryInterface::ELASTICSEARCH_MIGRATION_STATUS_DONE) {
throw new MigrationAlreadyDone();
}

Expand Down Expand Up @@ -119,10 +120,16 @@ public function migrate(string $version, string $source = 'file')
}
}

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

throw $e;
}
Expand Down
1 change: 1 addition & 0 deletions src/Models/Entity/ElasticsearchMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/**
* @property integer $id
* @property string $migration
* @property string $status
*/
class ElasticsearchMigration extends Model
{
Expand Down
18 changes: 0 additions & 18 deletions src/Models/Entity/ElasticsearchMigrationStatus.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Provider/ElasticsearchMigrationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ public function boot()
ShowMigration::class
]);
}

$this->app->bind(
\Triadev\EsMigration\Contract\Repository\ElasticsearchMigrationStatusContract::class,
function () {
return app()->make(
\Triadev\EsMigration\Business\Repository\ElasticsearchMigrationStatus::class
);
}
);

$this->app->bind(
\Triadev\EsMigration\Contract\Repository\ElasticsearchMigrationContract::class,
Expand Down
Loading

0 comments on commit 2a417e8

Please sign in to comment.