From b3783c26b791afb0d6e63a77a297d411f9ad2591 Mon Sep 17 00:00:00 2001 From: Fanis <93339170+tharropoulos@users.noreply.github.com> Date: Wed, 10 Jul 2024 21:58:20 +0300 Subject: [PATCH] [Typesense] Sync server state in getOrCreateCollectionFromModel #845 (#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 --- src/Engines/TypesenseEngine.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index 835aac83..7060ac01 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -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') ?? [];