Skip to content

Commit

Permalink
feat(typesense): sync server state in getOrCreateCollectionFromModel
Browse files Browse the repository at this point in the history
This commit updates the `getOrCreateCollectionFromModel` method in
`TypesenseEngine` to verify if a collection exists on the server. If not
found, it is created. This ensures consistency between the worker and
server states.
  • Loading branch information
tharropoulos committed Jul 10, 2024
1 parent 3174219 commit 38e0423
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,20 @@ protected function getOrCreateCollectionFromModel($model, bool $indexOperation =

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

if ($collection->exists() === true) {
return $collection;
$collectionExists = false;
if ($collection->exists()) {
// Also check if the collection exists in Typesense to avoid potential errors
$collectionName = $model->{$method}();
try {
$this->typesense->collections[$collectionName]->retrieve();
$collectionExists = true;
} catch (TypesenseClientError $e){
$collectionExists = false;
}
}

if ($collectionExists) {
return $this->typesense->getCollections()->{$collectionName};
}

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

0 comments on commit 38e0423

Please sign in to comment.