Skip to content

Commit

Permalink
[Typesense] Sync server state in getOrCreateCollectionFromModel #845 (#…
Browse files Browse the repository at this point in the history
…846)

* feat(typesense): sync server state in getOrCreateCollectionFromModel

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.

* style: fix styleCI errors

refactor: remove uneeded cast of flag

The collectionExists flag was already cast as false, so recasting it
serves no purpose

* Update TypesenseEngine.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
tharropoulos and taylorotwell authored Jul 10, 2024
1 parent 3174219 commit b3783c2
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,23 @@ protected function getOrCreateCollectionFromModel($model, bool $indexOperation =

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

if ($collection->exists() === true) {
return $collection;
$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) {
//
}
}

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

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

0 comments on commit b3783c2

Please sign in to comment.