1414use PDPhilip \Elasticsearch \Eloquent \Model ;
1515use PDPhilip \Elasticsearch \Schema \Blueprint ;
1616use PDPhilip \Elasticsearch \Schema \Builder as SchemaBuilder ;
17+ use PDPhilip \Elasticsearch \Utils \Helpers ;
1718
1819class ReIndexCommand extends Command
1920{
@@ -29,6 +30,8 @@ class ReIndexCommand extends Command
2930
3031 private string $ connectionName ;
3132
33+ private Connection $ connection ;
34+
3235 private SchemaBuilder $ schema ;
3336
3437 private Closure $ mappingDefinition ;
@@ -248,8 +251,8 @@ private function setupFromModel(Model $model): void
248251 $ this ->connectionName = $ model ->getConnectionName ();
249252
250253 /** @var Connection $connection */
251- $ connection = DB ::connection ($ this ->connectionName );
252- $ this ->schema = $ connection ->getSchemaBuilder ();
254+ $ this -> connection = DB ::connection ($ this ->connectionName );
255+ $ this ->schema = $ this -> connection ->getSchemaBuilder ();
253256
254257 $ this ->mappingDefinition = fn (Blueprint $ index ) => $ model ::mappingDefinition ($ index );
255258 }
@@ -406,7 +409,8 @@ private function copyToTemp(): bool
406409 return false ;
407410 }
408411
409- $ this ->omni ->success ('Copy complete — ' .($ result ['created ' ] ?? 0 ).' docs created ' );
412+ $ this ->tempCount = (int ) ($ result ['created ' ] ?? 0 );
413+ $ this ->omni ->success ('Copy complete — ' .$ this ->tempCount .' docs created ' );
410414
411415 return true ;
412416 } catch (Exception $ e ) {
@@ -425,8 +429,6 @@ private function verifyTemp(): bool
425429 {
426430 $ this ->omni ->divider ('Phase 4: Verify Temp ' );
427431
428- $ this ->tempCount = $ this ->countDocs ($ this ->tempIndexName );
429-
430432 $ this ->omni ->tableHeader ('Metric ' , 'Value ' );
431433 $ this ->omni ->tableRow ('Original count ' , number_format ($ this ->originalCount ));
432434 $ this ->omni ->tableRow ('Temp count ' , number_format ($ this ->tempCount ));
@@ -516,7 +518,8 @@ private function copyBack(): bool
516518
517519 $ failures = $ result ['failures ' ] ?? [];
518520 if (empty ($ failures )) {
519- $ this ->omni ->success ('Copy back complete — ' .($ result ['created ' ] ?? 0 ).' docs ' );
521+ $ this ->finalCount = (int ) ($ result ['created ' ] ?? 0 );
522+ $ this ->omni ->success ('Copy back complete — ' .$ this ->finalCount .' docs ' );
520523
521524 return true ;
522525 }
@@ -544,8 +547,6 @@ private function verifyFinal(): bool
544547 {
545548 $ this ->omni ->divider ('Phase 8: Verify Final ' );
546549
547- $ this ->finalCount = $ this ->countDocs ($ this ->indexName );
548-
549550 $ this ->omni ->tableHeader ('Metric ' , 'Value ' );
550551 $ this ->omni ->tableRow ('Original snapshot ' , number_format ($ this ->originalCount ));
551552 $ this ->omni ->tableRow ('Temp count ' , number_format ($ this ->tempCount ));
@@ -561,8 +562,8 @@ private function verifyFinal(): bool
561562 $ this ->omni ->warning ('Final count mismatch ( ' .$ this ->finalCount .' vs ' .$ this ->tempCount .') — running catch-up ' );
562563
563564 try {
564- $ this ->schema ->reindex ($ this ->tempIndexName , $ this ->indexName );
565- $ this ->finalCount = $ this -> countDocs ( $ this -> indexName );
565+ $ result = $ this ->schema ->reindex ($ this ->tempIndexName , $ this ->indexName );
566+ $ this ->finalCount = ( int ) ( $ result [ ' created ' ] ?? 0 ) + ( int ) ( $ result [ ' updated ' ] ?? 0 );
566567
567568 if ($ this ->countsMatch ($ this ->tempCount , $ this ->finalCount )) {
568569 $ this ->omni ->success ('Catch-up resolved the mismatch ' );
@@ -668,7 +669,9 @@ private function mappingMismatches(): array
668669 {
669670 $ currentMapping = $ this ->schema ->getFieldsMapping ($ this ->indexName );
670671
671- $ blueprint = new Blueprint ($ this ->indexName );
672+ $ blueprint = Helpers::getLaravelCompatabilityVersion () >= 12
673+ ? new Blueprint ($ this ->connection , $ this ->indexName )
674+ : new Blueprint ($ this ->indexName );
672675 ($ this ->mappingDefinition )($ blueprint );
673676
674677 $ mismatches = [];
0 commit comments