Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 47 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| IndexHasChangedValidator | |
0.00% |
0 / 47 |
|
0.00% |
0 / 4 |
182 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| validate | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
110 | |||
| compareSettings | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| compareMapping | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace CirrusSearch\Maintenance\Validators; |
| 4 | |
| 5 | use CirrusSearch\Maintenance\ConfigUtils; |
| 6 | use CirrusSearch\Maintenance\Printer; |
| 7 | use Elastica\Client; |
| 8 | use Elastica\Index; |
| 9 | use MediaWiki\Status\Status; |
| 10 | |
| 11 | class IndexHasChangedValidator extends Validator { |
| 12 | |
| 13 | private Index $oldIndex; |
| 14 | private Index $newIndex; |
| 15 | private ConfigUtils $configUtils; |
| 16 | |
| 17 | public function __construct( Client $client, Index $oldIndex, Index $newIndex, Printer $out ) { |
| 18 | parent::__construct( $out ); |
| 19 | |
| 20 | $this->oldIndex = $oldIndex; |
| 21 | $this->newIndex = $newIndex; |
| 22 | $this->configUtils = new ConfigUtils( $client, $out ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @return Status |
| 27 | */ |
| 28 | public function validate() { |
| 29 | $this->out->outputIndented( "Validating new index is different..." ); |
| 30 | if ( !$this->oldIndex->exists() ) { |
| 31 | $this->out->output( "ok\n" ); |
| 32 | return Status::newGood( true ); |
| 33 | } |
| 34 | |
| 35 | $alias = $this->oldIndex->getName(); |
| 36 | $replacement = $this->newIndex->getName(); |
| 37 | |
| 38 | $status = $this->configUtils->isIndex( $alias ); |
| 39 | if ( !$status->isGood() ) { |
| 40 | $this->out->output( "error\n" ); |
| 41 | return $status; |
| 42 | } |
| 43 | if ( $status->getValue() ) { |
| 44 | $this->out->output( "error\n" ); |
| 45 | return Status::newFatal( "Primary index was expected to be an alias: $alias" ); |
| 46 | } |
| 47 | $status = $this->configUtils->isIndex( $replacement ); |
| 48 | if ( !$status->isGood() ) { |
| 49 | $this->out->output( "error\n" ); |
| 50 | return $status; |
| 51 | } |
| 52 | if ( !$status->getValue() ) { |
| 53 | $this->out->output( "error\n" ); |
| 54 | return Status::newFatal( |
| 55 | "Replacement index was expected to be a real index: {$replacement}" |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | $status = $this->configUtils->getIndicesWithAlias( $alias ); |
| 60 | if ( !$status->isGood() ) { |
| 61 | $this->out->output( "error\n" ); |
| 62 | return $status; |
| 63 | } |
| 64 | $liveIndices = $status->getValue(); |
| 65 | if ( in_array( $replacement, $liveIndices ) ) { |
| 66 | // If old and new are the same index we are doing something like --justMapping |
| 67 | // and this validator is irrelevant. |
| 68 | $this->out->output( "same index\n" ); |
| 69 | return Status::newGood( true ); |
| 70 | } |
| 71 | |
| 72 | $equivalent = $this->compareSettings() && $this->compareMapping(); |
| 73 | if ( $equivalent ) { |
| 74 | $this->output( "no change\n" ); |
| 75 | } else { |
| 76 | $this->output( "ok\n" ); |
| 77 | } |
| 78 | return Status::newGood( !$equivalent ); |
| 79 | } |
| 80 | |
| 81 | private function compareSettings(): bool { |
| 82 | $old = $this->oldIndex->getSettings()->get(); |
| 83 | unset( $old['provided_name'], $old['creation_date'], $old['uuid'] ); |
| 84 | $new = $this->newIndex->getSettings()->get(); |
| 85 | unset( $new['provided_name'], $new['creation_date'], $new['uuid'] ); |
| 86 | return $old == $new; |
| 87 | } |
| 88 | |
| 89 | private function compareMapping(): bool { |
| 90 | $old = $this->oldIndex->getMapping(); |
| 91 | $new = $this->newIndex->getMapping(); |
| 92 | return $old == $new; |
| 93 | } |
| 94 | } |