-
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.
Merge pull request #1 from triadev/field_datatype_migration
Validate the update index mapping changes.
- Loading branch information
Showing
9 changed files
with
337 additions
and
5 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
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,111 @@ | ||
<?php | ||
namespace Triadev\EsMigration\Business\Validation; | ||
|
||
use Triadev\EsMigration\Exception\FieldDatatypeMigrationFailed; | ||
|
||
class FieldDatatypeMigration | ||
{ | ||
/** @var array */ | ||
private $string = ['text', 'keyword']; | ||
|
||
/** @var array */ | ||
private $numeric = ['long', 'integer', 'short', 'byte', 'double', 'float', 'half_float', 'scaled_float']; | ||
|
||
/** @var array */ | ||
private $date = ['date']; | ||
|
||
/** @var array */ | ||
private $boolean = ['boolean']; | ||
|
||
/** @var array */ | ||
private $binary = ['binary']; | ||
|
||
/** @var array */ | ||
private $range = ['integer_range', 'float_range', 'long_range', 'double_range', 'date_range']; | ||
|
||
/** @var array */ | ||
private $validMigrations = [ | ||
'string' => [], | ||
'numeric' => ['string'], | ||
'date' => ['string'], | ||
'boolean' => ['string'], | ||
'binary' => [], | ||
'range' => [], | ||
'object' => [], | ||
'nested' => [], | ||
'geo_point' => [], | ||
'geo_shape' => [], | ||
'ip' => [], | ||
'completion' => [], | ||
'token_count' => [], | ||
'murmur3' => [], | ||
'attachment' => [], | ||
'percolator' => [] | ||
]; | ||
|
||
/** | ||
* Validate | ||
* | ||
* @param array $esMapping | ||
* @param array $migrationMapping | ||
* | ||
* @throws FieldDatatypeMigrationFailed | ||
*/ | ||
public function validate(array $esMapping, array $migrationMapping) | ||
{ | ||
$tmpEsMapping = array_dot($esMapping); | ||
$tmpMigrationMapping = array_dot($migrationMapping); | ||
|
||
$failedFields = []; | ||
|
||
foreach ($tmpEsMapping as $key => $value) { | ||
if (preg_match('/^.*type$/', $key) && array_key_exists($key, $tmpMigrationMapping)) { | ||
$esMappingDatatype = $this->getFieldDatatype($tmpEsMapping[$key]); | ||
$migrationMappingDatatype = $this->getFieldDatatype($tmpMigrationMapping[$key]); | ||
|
||
if ($esMappingDatatype == $migrationMappingDatatype) { | ||
continue; | ||
} | ||
|
||
if (!in_array($migrationMappingDatatype, $this->validMigrations[$esMappingDatatype])) { | ||
$failedFields[] = $key; | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
if (!empty($failedFields)) { | ||
throw new FieldDatatypeMigrationFailed(json_encode($failedFields)); | ||
} | ||
} | ||
|
||
private function getFieldDatatype(string $datatype) : ?string | ||
{ | ||
$datatypes = [ | ||
'string' => $this->string, | ||
'numeric' => $this->numeric, | ||
'date' => $this->date, | ||
'boolean' => $this->boolean, | ||
'binary' => $this->binary, | ||
'range' => $this->range, | ||
'object' => ['object'], | ||
'nested' => ['nested'], | ||
'geo_point' => ['geo_point'], | ||
'geo_shape' => ['geo_shape'], | ||
'ip' => ['ip'], | ||
'completion' => ['completion'], | ||
'token_count' => ['token_count'], | ||
'murmur3' => ['murmur3'], | ||
'attachment' => ['attachment'], | ||
'percolator' => ['percolator'] | ||
]; | ||
|
||
foreach ($datatypes as $key => $d) { | ||
if (in_array($datatype, $d)) { | ||
return $key; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
namespace Triadev\EsMigration\Exception; | ||
|
||
use Throwable; | ||
|
||
class FieldDatatypeMigrationFailed extends \Exception | ||
{ | ||
/** | ||
* FieldDatatypeMigrationFailed 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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/Resources/Migrations/field_datatype_migration_failed/migrations.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,23 @@ | ||
<?php | ||
|
||
return [ | ||
[ | ||
'index' => 'phpunit', | ||
'type' => 'update', | ||
'mappings' => [ | ||
'phpunit' => [ | ||
'properties' => [ | ||
'title' => [ | ||
'type' => 'integer' | ||
], | ||
'count' => [ | ||
'type' => 'text' | ||
], | ||
'description' => [ | ||
'type' => 'integer' | ||
] | ||
] | ||
] | ||
] | ||
] | ||
]; |
142 changes: 142 additions & 0 deletions
142
tests/integration/Business/Validation/FieldDatatypeMigrationTest.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,142 @@ | ||
<?php | ||
namespace Tests\Integration\Business\Validation; | ||
|
||
use Tests\TestCase; | ||
use Triadev\EsMigration\Exception\FieldDatatypeMigrationFailed; | ||
|
||
class FieldDatatypeMigrationTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @expectedException \Triadev\EsMigration\Exception\FieldDatatypeMigrationFailed | ||
*/ | ||
public function it_validates_field_datatype_migrations() | ||
{ | ||
$esMapping = [ | ||
'phpunit' => [ | ||
'properties' => [ | ||
'text' => [ | ||
'type' => 'text' | ||
], | ||
'keyword' => [ | ||
'type' => 'keyword' | ||
], | ||
'date' => [ | ||
'type' => 'date' | ||
], | ||
'boolean' => [ | ||
'type' => 'boolean' | ||
], | ||
'binary' => [ | ||
'type' => 'binary' | ||
], | ||
'range' => [ | ||
'type' => 'integer_range' | ||
], | ||
'object' => [ | ||
'type' => 'object' | ||
], | ||
'nested' => [ | ||
'type' => 'nested' | ||
], | ||
'geo_point' => [ | ||
'type' => 'geo_point' | ||
], | ||
'geo_shape' => [ | ||
'type' => 'geo_shape' | ||
], | ||
'ip' => [ | ||
'type' => 'ip' | ||
], | ||
'completion' => [ | ||
'type' => 'completion' | ||
], | ||
'token_count' => [ | ||
'type' => 'token_count' | ||
], | ||
'murmur3' => [ | ||
'type' => 'murmur3' | ||
], | ||
'attachment' => [ | ||
'type' => 'attachment' | ||
], | ||
'percolator' => [ | ||
'type' => 'percolator' | ||
], | ||
'valid' => [ | ||
'type' => 'text' | ||
] | ||
] | ||
] | ||
]; | ||
|
||
$migrationMapping = [ | ||
'phpunit' => [ | ||
'properties' => [ | ||
'text' => [ | ||
'type' => 'long' | ||
], | ||
'keyword' => [ | ||
'type' => 'long' | ||
], | ||
'date' => [ | ||
'type' => 'long' | ||
], | ||
'boolean' => [ | ||
'type' => 'long' | ||
], | ||
'binary' => [ | ||
'type' => 'long' | ||
], | ||
'range' => [ | ||
'type' => 'long' | ||
], | ||
'object' => [ | ||
'type' => 'long' | ||
], | ||
'nested' => [ | ||
'type' => 'long' | ||
], | ||
'geo_point' => [ | ||
'type' => 'long' | ||
], | ||
'geo_shape' => [ | ||
'type' => 'long' | ||
], | ||
'ip' => [ | ||
'type' => 'long' | ||
], | ||
'completion' => [ | ||
'type' => 'long' | ||
], | ||
'token_count' => [ | ||
'type' => 'long' | ||
], | ||
'murmur3' => [ | ||
'type' => 'long' | ||
], | ||
'attachment' => [ | ||
'type' => 'long' | ||
], | ||
'percolator' => [ | ||
'type' => 'long' | ||
], | ||
'valid' => [ | ||
'type' => 'keyword' | ||
] | ||
] | ||
] | ||
]; | ||
|
||
try { | ||
(new \Triadev\EsMigration\Business\Validation\FieldDatatypeMigration())->validate( | ||
$esMapping, | ||
$migrationMapping | ||
); | ||
} catch (FieldDatatypeMigrationFailed $e) { | ||
$this->assertCount(16, json_decode($e->getMessage())); | ||
|
||
throw $e; | ||
} | ||
} | ||
} |
Oops, something went wrong.