Skip to content

Commit

Permalink
refactor(typesense): remove unused exists checks (#847)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
saibotk and taylorotwell authored Aug 5, 2024
1 parent 0a6bbc4 commit d58e16f
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,25 +498,19 @@ protected function getOrCreateCollectionFromModel($model, bool $indexOperation =
{
$method = $indexOperation ? 'indexableAs' : 'searchableAs';

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

$collectionExists = false;

if ($collection->exists()) {
// Also determine if the collection exists in Typesense...
$collectionName = $model->{$method}();

try {
$this->typesense->collections[$collectionName]->retrieve();
// Determine if the collection exists in Typesense...
try {
$collection->retrieve();

$collectionExists = true;
} catch (TypesenseClientError $e) {
//
}
}
// No error means this collection exists on the server...
$collection->setExists(true);

if ($collectionExists) {
return $this->typesense->getCollections()->{$collectionName};
return $collection;
} catch (TypesenseClientError $e) {
//
}

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

0 comments on commit d58e16f

Please sign in to comment.