Skip to content

Commit

Permalink
refactor(typesense): remove unused exists checks
Browse files Browse the repository at this point in the history
In laravel#820 we introduced an exists check method, which then lead to static state issues as described in laravel#845

Those where addressed in laravel#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.
  • Loading branch information
saibotk committed Jul 12, 2024
1 parent 8d73e7d commit 20e4507
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,26 +497,17 @@ public function deleteIndex($name)
protected function getOrCreateCollectionFromModel($model, bool $indexOperation = true): TypesenseCollection
{
$method = $indexOperation ? 'indexableAs' : 'searchableAs';
$collectionName = $model->{$method}();
$collection = $this->typesense->getCollections()->{$collectionName};

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

$collectionExists = false;

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

try {
$this->typesense->collections[$collectionName]->retrieve();

$collectionExists = true;
} catch (TypesenseClientError $e) {
//
}
}
// Determine if the collection exists in Typesense...
try {
$collection->retrieve();

if ($collectionExists) {
return $this->typesense->getCollections()->{$collectionName};
// No error means this exists on the server
return $collection
} catch (TypesenseClientError $e) {
//
}

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

0 comments on commit 20e4507

Please sign in to comment.