From 2c5295261d1459e4f9b157c407a663a6685f3ddf Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Tue, 30 Jan 2024 16:41:45 +0100 Subject: [PATCH] Ignore duplicate tags (#1431) --- src/Storage/DatabaseEntriesRepository.php | 41 ++++++++++++++--------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/Storage/DatabaseEntriesRepository.php b/src/Storage/DatabaseEntriesRepository.php index d09295e38..a63a600f7 100644 --- a/src/Storage/DatabaseEntriesRepository.php +++ b/src/Storage/DatabaseEntriesRepository.php @@ -3,6 +3,7 @@ namespace Laravel\Telescope\Storage; use DateTimeInterface; +use Illuminate\Database\UniqueConstraintViolationException; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; use Laravel\Telescope\Contracts\ClearableRepository; @@ -189,14 +190,18 @@ protected function storeExceptions(Collection $exceptions) protected function storeTags(Collection $results) { $results->chunk($this->chunkSize)->each(function ($chunked) { - $this->table('telescope_entries_tags')->insert($chunked->flatMap(function ($tags, $uuid) { - return collect($tags)->map(function ($tag) use ($uuid) { - return [ - 'entry_uuid' => $uuid, - 'tag' => $tag, - ]; - }); - })->all()); + try { + $this->table('telescope_entries_tags')->insert($chunked->flatMap(function ($tags, $uuid) { + return collect($tags)->map(function ($tag) use ($uuid) { + return [ + 'entry_uuid' => $uuid, + 'tag' => $tag, + ]; + }); + })->all()); + } catch (UniqueConstraintViolationException $e) { + // Ignore tags that already exist... + } }); } @@ -246,14 +251,18 @@ public function update(Collection $updates) protected function updateTags($entry) { if (! empty($entry->tagsChanges['added'])) { - $this->table('telescope_entries_tags')->insert( - collect($entry->tagsChanges['added'])->map(function ($tag) use ($entry) { - return [ - 'entry_uuid' => $entry->uuid, - 'tag' => $tag, - ]; - })->toArray() - ); + try { + $this->table('telescope_entries_tags')->insert( + collect($entry->tagsChanges['added'])->map(function ($tag) use ($entry) { + return [ + 'entry_uuid' => $entry->uuid, + 'tag' => $tag, + ]; + })->toArray() + ); + } catch (UniqueConstraintViolationException $e) { + // Ignore tags that already exist... + } } collect($entry->tagsChanges['removed'])->each(function ($tag) use ($entry) {