Skip to content

Commit d58e16f

Browse files
refactor(typesense): remove unused exists checks (#847)
* refactor(typesense): remove unused exists checks In #820 we introduced an exists check method, which then lead to static state issues as described in #845 Those where addressed in #846 But now that the function always double-checks the existence, we can remove the exists check entirely and only rely on the Typesense server response for this state. This "fixes" issues where the server already has a collection and the client would try to recreate it. E.g. when it has flushed the index and another process or worker then creates the collection. * Update TypesenseEngine.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 0a6bbc4 commit d58e16f

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

src/Engines/TypesenseEngine.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -498,25 +498,19 @@ protected function getOrCreateCollectionFromModel($model, bool $indexOperation =
498498
{
499499
$method = $indexOperation ? 'indexableAs' : 'searchableAs';
500500

501-
$collection = $this->typesense->getCollections()->{$model->{$method}()};
501+
$collectionName = $model->{$method}();
502+
$collection = $this->typesense->getCollections()->{$collectionName};
502503

503-
$collectionExists = false;
504-
505-
if ($collection->exists()) {
506-
// Also determine if the collection exists in Typesense...
507-
$collectionName = $model->{$method}();
508-
509-
try {
510-
$this->typesense->collections[$collectionName]->retrieve();
504+
// Determine if the collection exists in Typesense...
505+
try {
506+
$collection->retrieve();
511507

512-
$collectionExists = true;
513-
} catch (TypesenseClientError $e) {
514-
//
515-
}
516-
}
508+
// No error means this collection exists on the server...
509+
$collection->setExists(true);
517510

518-
if ($collectionExists) {
519-
return $this->typesense->getCollections()->{$collectionName};
511+
return $collection;
512+
} catch (TypesenseClientError $e) {
513+
//
520514
}
521515

522516
$schema = config('scout.typesense.model-settings.'.get_class($model).'.collection-schema') ?? [];

0 commit comments

Comments
 (0)