-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the possibility to create migrations with files.
- Loading branch information
Christopher Lorke
committed
Dec 3, 2018
1 parent
9f14141
commit 0239698
Showing
10 changed files
with
381 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
return [ | ||
'type' => \Triadev\EsMigration\Business\Mapper\MigrationTypes::MIGRATION_TYPE_CREATE_INDEX, | ||
'params' => [ | ||
'index' => 'index', | ||
'body' => [ | ||
'mappings' => [ | ||
'phpunit' => [ | ||
'dynamic' => 'strict', | ||
'properties' => [ | ||
'title' => [ | ||
'type' => 'text' | ||
], | ||
'count' => [ | ||
'type' => 'integer' | ||
] | ||
] | ||
] | ||
], | ||
'settings' => [ | ||
'refresh_interval' => "30s" | ||
] | ||
] | ||
], | ||
'priority' => 1, | ||
'stopOnFailure' => true | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
return [ | ||
'filePath' => [] | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
namespace Triadev\EsMigration\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Facades\Validator; | ||
use Triadev\EsMigration\Business\Mapper\MigrationTypes; | ||
use Triadev\EsMigration\Contract\ElasticsearchMigrationContract; | ||
|
||
class ImportFileMigrations extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'triadev:es-migration:import-file-migrations {migration} {filePath}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Import file migrations.'; | ||
|
||
/** @var ElasticsearchMigrationContract */ | ||
private $elasticsearchMigrationService; | ||
|
||
/** | ||
* ImportFileMigrations constructor. | ||
* @param ElasticsearchMigrationContract $elasticsearchMigrationService | ||
*/ | ||
public function __construct(ElasticsearchMigrationContract $elasticsearchMigrationService) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->elasticsearchMigrationService = $elasticsearchMigrationService; | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function handle() | ||
{ | ||
$migration = (string)$this->argument('migration'); | ||
$filePathSelector = (string)$this->argument('filePath'); | ||
|
||
$filePath = config('triadev-elasticsearch-migration.filePath'); | ||
if (!array_has($filePath, $filePathSelector)) { | ||
throw new \Exception("No migration file path was defined."); | ||
} | ||
|
||
$migrationSteps = $this->getMigrationSteps($migration, $filePath[$filePathSelector]); | ||
if (!empty($migrationSteps)) { | ||
$this->elasticsearchMigrationService->createMigration($migration); | ||
|
||
$this->importMigrationSteps( | ||
$migration, | ||
$migrationSteps | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $migration | ||
* @param string $filePath | ||
* @return array | ||
* @throws \Exception | ||
*/ | ||
private function getMigrationSteps(string $migration, string $filePath) : array | ||
{ | ||
$filePath = $filePath . DIRECTORY_SEPARATOR . $migration; | ||
if (!is_dir($filePath)) { | ||
throw new \Exception("The migration directory does not exist."); | ||
} | ||
|
||
$files = []; | ||
|
||
foreach (scandir($filePath) as $key => $value) { | ||
if (!in_array($value, ['.', '..'])) { | ||
$files[] = $filePath . DIRECTORY_SEPARATOR . $value; | ||
} | ||
} | ||
|
||
return $files; | ||
} | ||
|
||
/** | ||
* @param string $migration | ||
* @param array $migrationSteps | ||
*/ | ||
private function importMigrationSteps(string $migration, array $migrationSteps) | ||
{ | ||
try { | ||
foreach ($this->getValidMigrationSteps($migrationSteps) as $validMigrationStep) { | ||
$this->elasticsearchMigrationService->addMigrationStep( | ||
$migration, | ||
array_get($validMigrationStep, 'type'), | ||
array_get($validMigrationStep, 'params'), | ||
array_get($validMigrationStep, 'priority', 1), | ||
array_get($validMigrationStep, 'stopOnFailure', true) | ||
); | ||
} | ||
} catch (\Exception $e) { | ||
Log::error("The migration steps could not be imported."); | ||
} | ||
} | ||
|
||
/** | ||
* @param array $migrationSteps | ||
* @return array | ||
* @throws \Exception | ||
*/ | ||
private function getValidMigrationSteps(array $migrationSteps) : array | ||
{ | ||
$migrationTypes = new MigrationTypes(); | ||
|
||
$validMigrationSteps = []; | ||
|
||
foreach ($migrationSteps as $migrationStep) { | ||
$step = require $migrationStep; | ||
|
||
$validator = Validator::make($step, [ | ||
'type' => 'required|string', | ||
'params' => 'required|array', | ||
'priority' => 'integer', | ||
'stopOnFailure' => 'boolean' | ||
]); | ||
|
||
if ($validator->fails()) { | ||
throw new \Exception("The migration step is invalid."); | ||
} | ||
|
||
if (!$migrationTypes->isMigrationTypeValid(array_get($step, 'type'))) { | ||
throw new \Exception("The migration step type is invalid."); | ||
} | ||
|
||
$validMigrationSteps[] = $step; | ||
} | ||
|
||
return $validMigrationSteps; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
tests/integration/Console/Commands/ImportFileMigrationsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
namespace Tests\Integration\Console\Commands; | ||
|
||
use Tests\TestCase; | ||
use Triadev\EsMigration\Contract\Repository\ElasticsearchMigrationContract; | ||
use Triadev\EsMigration\Contract\Repository\ElasticsearchMigrationStepContract; | ||
|
||
class ImportFileMigrationsTest extends TestCase | ||
{ | ||
/** @var ElasticsearchMigrationContract */ | ||
private $repositoryMigrations; | ||
|
||
/** @var ElasticsearchMigrationStepContract */ | ||
private $repositoryMigrationSteps; | ||
|
||
/** | ||
* Setup the test environment. | ||
*/ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->repositoryMigrations = app(ElasticsearchMigrationContract::class); | ||
$this->repositoryMigrationSteps = app(ElasticsearchMigrationStepContract::class); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage No migration file path was defined. | ||
*/ | ||
public function it_throws_an_exception_if_no_migration_file_path_was_defined() | ||
{ | ||
$this->artisan('triadev:es-migration:import-file-migrations', [ | ||
'migration' => 'phpunit', | ||
'filePath' => 'phpunit' | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage The migration directory does not exist. | ||
*/ | ||
public function it_throws_an_exception_if_migration_directory_not_exist() | ||
{ | ||
config()->set('triadev-elasticsearch-migration.filePath', [ | ||
'phpunit' => __DIR__ . '/Migrations' | ||
]); | ||
|
||
$this->artisan('triadev:es-migration:import-file-migrations', [ | ||
'migration' => 'not_found', | ||
'filePath' => 'phpunit' | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_imports_file_migrations() | ||
{ | ||
$this->assertNull($this->repositoryMigrations->find('phpunit')); | ||
|
||
$this->assertNull($this->repositoryMigrationSteps->find(1)); | ||
$this->assertNull($this->repositoryMigrationSteps->find(2)); | ||
|
||
config()->set('triadev-elasticsearch-migration.filePath', [ | ||
'phpunit' => __DIR__ . '/Migrations' | ||
]); | ||
|
||
$this->artisan('triadev:es-migration:import-file-migrations', [ | ||
'migration' => 'phpunit', | ||
'filePath' => 'phpunit' | ||
]); | ||
|
||
$this->assertNotNull($this->repositoryMigrations->find('phpunit')); | ||
|
||
$this->assertNotNull($this->repositoryMigrationSteps->find(1)); | ||
$this->assertNotNull($this->repositoryMigrationSteps->find(2)); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/integration/Console/Commands/Migrations/invalid/invalid.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
return [ | ||
'type' => \Triadev\EsMigration\Business\Mapper\MigrationTypes::MIGRATION_TYPE_CREATE_INDEX, | ||
'params' => 'DATA_TYPE_IS_INVALID' | ||
]; |
28 changes: 28 additions & 0 deletions
28
tests/integration/Console/Commands/Migrations/invalid_type/invalid_type.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
return [ | ||
'type' => 'INVALID', | ||
'params' => [ | ||
'index' => 'index', | ||
'body' => [ | ||
'mappings' => [ | ||
'phpunit' => [ | ||
'dynamic' => 'strict', | ||
'properties' => [ | ||
'title' => [ | ||
'type' => 'text' | ||
], | ||
'count' => [ | ||
'type' => 'integer' | ||
] | ||
] | ||
] | ||
], | ||
'settings' => [ | ||
'refresh_interval' => "30s" | ||
] | ||
] | ||
], | ||
'priority' => 1, | ||
'stopOnFailure' => true | ||
]; |
Oops, something went wrong.