From ed0c5e3e33560ef8ef6e7e4e576b5fddc5dc0a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kr=C3=B6ll?= Date: Fri, 12 Jul 2024 13:47:10 +0200 Subject: [PATCH] refactor(typesense): remove unused exists checks In https://github.com/laravel/scout/pull/820 we introduced an exists check method, which then lead to static state issues as described in https://github.com/laravel/scout/issues/845 Those where addressed in https://github.com/laravel/scout/pull/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. --- src/Engines/TypesenseEngine.php | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index f0ae76ff..0c5e1cba 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -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') ?? [];