From fad8412c0f4623a558ef27967995d7a5474d7978 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 17 Jul 2026 09:46:27 +0100 Subject: [PATCH] Revert "Opt-in config for surfacing import failures (#33)" This reverts commit 1b0afbc6683b1d839f926fef5eba52a7cf402174. --- config/statamic-typesense.php | 6 -- src/Exceptions/ImportFailedException.php | 23 ----- src/Typesense/Index.php | 24 +----- tests/Unit/IndexTest.php | 102 ----------------------- 4 files changed, 1 insertion(+), 154 deletions(-) delete mode 100644 src/Exceptions/ImportFailedException.php diff --git a/config/statamic-typesense.php b/config/statamic-typesense.php index 75e3cf5..9ac9414 100644 --- a/config/statamic-typesense.php +++ b/config/statamic-typesense.php @@ -6,10 +6,4 @@ // but be quicker to update large numbers of documents. 'insert_chunk_size' => 100, - // Typesense may reject individual documents during a bulk import - // (e.g. schema type mismatches) while the import itself succeeds. - // By default these failures are logged as a warning. Set this to - // true to throw an exception instead. - 'throw_on_import_failure' => false, - ]; diff --git a/src/Exceptions/ImportFailedException.php b/src/Exceptions/ImportFailedException.php deleted file mode 100644 index a41b11f..0000000 --- a/src/Exceptions/ImportFailedException.php +++ /dev/null @@ -1,23 +0,0 @@ -index; - } - - public function failures(): array - { - return $this->failures; - } -} diff --git a/src/Typesense/Index.php b/src/Typesense/Index.php index 945a12e..4adcb18 100644 --- a/src/Typesense/Index.php +++ b/src/Typesense/Index.php @@ -3,13 +3,11 @@ namespace StatamicRadPack\Typesense\Typesense; use Illuminate\Support\Collection; -use Illuminate\Support\Facades\Log; use Statamic\Contracts\Search\Searchable; use Statamic\Facades\Blink; use Statamic\Search\Documents; use Statamic\Search\Index as BaseIndex; use Statamic\Support\Arr; -use StatamicRadPack\Typesense\Exceptions\ImportFailedException; use Typesense\Client; use Typesense\Exceptions\ObjectNotFound; use Typesense\Exceptions\TypesenseClientError; @@ -77,27 +75,7 @@ public function exists() protected function insertDocuments(Documents $documents) { - $results = $this->getOrCreateIndex()->documents->import($documents->all(), ['action' => 'upsert', 'return_id' => true]); - - $failures = collect($results)->filter(fn ($result) => ! ($result['success'] ?? false))->values(); - - if ($failures->isEmpty()) { - return; - } - - if (config('statamic-typesense.throw_on_import_failure', false)) { - throw new ImportFailedException($this->name, $failures->all()); - } - - Log::warning(sprintf('Typesense rejected %d document(s) during import into the [%s] index.', $failures->count(), $this->name), [ - 'failures' => $failures - ->take(10) - ->map(fn ($failure) => [ - 'id' => $failure['id'] ?? null, - 'error' => $failure['error'] ?? null, - ]) - ->all(), - ]); + $this->getOrCreateIndex()->documents->import($documents->all(), ['action' => 'upsert']); } protected function deleteIndex() diff --git a/tests/Unit/IndexTest.php b/tests/Unit/IndexTest.php index 63ba697..bce387e 100644 --- a/tests/Unit/IndexTest.php +++ b/tests/Unit/IndexTest.php @@ -2,10 +2,8 @@ namespace StatamicRadPack\Typesense\Tests\Unit; -use Illuminate\Support\Facades\Log; use PHPUnit\Framework\Attributes\Test; use Statamic\Facades; -use StatamicRadPack\Typesense\Exceptions\ImportFailedException; use StatamicRadPack\Typesense\Tests\TestCase; use Typesense\Client; @@ -134,104 +132,4 @@ public function it_sorts_by_specified_order() $this->assertSame(['Entry 2', 'Entry 1'], collect($results['results'])->pluck('title')->all()); } - - #[Test] - public function it_logs_a_warning_when_documents_are_rejected_during_import() - { - $this->configureFailingIndex(); - - Log::spy(); - - Facades\Collection::make() - ->handle('pages') - ->title('Pages') - ->save(); - - Facades\Entry::make() - ->id('test-1') - ->collection('pages') - ->data(['title' => 'Entry 1']) - ->save(); - - Log::shouldHaveReceived('warning') - ->once() - ->withArgs(function ($message, $context) { - return str_contains($message, 'typesense_failing_index') - && str_contains($message, '1 document(s)') - && $context['failures'][0]['id'] === 'entry::test-1' - && ! empty($context['failures'][0]['error']); - }); - } - - #[Test] - public function it_throws_when_documents_are_rejected_during_import_and_strict_mode_is_enabled() - { - config()->set('statamic-typesense.throw_on_import_failure', true); - - $this->configureFailingIndex(); - - Facades\Collection::make() - ->handle('pages') - ->title('Pages') - ->save(); - - try { - Facades\Entry::make() - ->id('test-1') - ->collection('pages') - ->data(['title' => 'Entry 1']) - ->save(); - - $this->fail('ImportFailedException was not thrown.'); - } catch (ImportFailedException $e) { - $this->assertSame('typesense_failing_index', $e->index()); - $this->assertCount(1, $e->failures()); - $this->assertNotEmpty($e->failures()[0]['error']); - } - } - - #[Test] - public function it_does_not_log_or_throw_when_all_documents_import_successfully() - { - config()->set('statamic-typesense.throw_on_import_failure', true); - - Log::spy(); - - Facades\Collection::make() - ->handle('pages') - ->title('Pages') - ->save(); - - Facades\Entry::make() - ->id('test-1') - ->collection('pages') - ->data(['title' => 'Entry 1']) - ->save(); - - Log::shouldNotHaveReceived('warning'); - - $export = collect(json_decode('['.str_replace("\n", ',', Facades\Search::index('typesense_index')->getOrCreateIndex()->documents->export()).']'))->pluck('id'); - - $this->assertContains('entry::test-1', $export); - } - - private function configureFailingIndex() - { - // Typesense will reject documents whose title is a string, - // since the schema declares it as an int32. - config()->set('statamic.search.indexes.typesense_failing_index', [ - 'driver' => 'typesense', - 'searchables' => ['collection:pages'], - 'settings' => [ - 'schema' => [ - 'fields' => [ - [ - 'type' => 'int32', - 'name' => 'title', - ], - ], - ], - ], - ]); - } }