Skip to content

Commit 2c52952

Browse files
authored
Ignore duplicate tags (#1431)
1 parent 4f9a644 commit 2c52952

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/Storage/DatabaseEntriesRepository.php

+25-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Laravel\Telescope\Storage;
44

55
use DateTimeInterface;
6+
use Illuminate\Database\UniqueConstraintViolationException;
67
use Illuminate\Support\Collection;
78
use Illuminate\Support\Facades\DB;
89
use Laravel\Telescope\Contracts\ClearableRepository;
@@ -189,14 +190,18 @@ protected function storeExceptions(Collection $exceptions)
189190
protected function storeTags(Collection $results)
190191
{
191192
$results->chunk($this->chunkSize)->each(function ($chunked) {
192-
$this->table('telescope_entries_tags')->insert($chunked->flatMap(function ($tags, $uuid) {
193-
return collect($tags)->map(function ($tag) use ($uuid) {
194-
return [
195-
'entry_uuid' => $uuid,
196-
'tag' => $tag,
197-
];
198-
});
199-
})->all());
193+
try {
194+
$this->table('telescope_entries_tags')->insert($chunked->flatMap(function ($tags, $uuid) {
195+
return collect($tags)->map(function ($tag) use ($uuid) {
196+
return [
197+
'entry_uuid' => $uuid,
198+
'tag' => $tag,
199+
];
200+
});
201+
})->all());
202+
} catch (UniqueConstraintViolationException $e) {
203+
// Ignore tags that already exist...
204+
}
200205
});
201206
}
202207

@@ -246,14 +251,18 @@ public function update(Collection $updates)
246251
protected function updateTags($entry)
247252
{
248253
if (! empty($entry->tagsChanges['added'])) {
249-
$this->table('telescope_entries_tags')->insert(
250-
collect($entry->tagsChanges['added'])->map(function ($tag) use ($entry) {
251-
return [
252-
'entry_uuid' => $entry->uuid,
253-
'tag' => $tag,
254-
];
255-
})->toArray()
256-
);
254+
try {
255+
$this->table('telescope_entries_tags')->insert(
256+
collect($entry->tagsChanges['added'])->map(function ($tag) use ($entry) {
257+
return [
258+
'entry_uuid' => $entry->uuid,
259+
'tag' => $tag,
260+
];
261+
})->toArray()
262+
);
263+
} catch (UniqueConstraintViolationException $e) {
264+
// Ignore tags that already exist...
265+
}
257266
}
258267

259268
collect($entry->tagsChanges['removed'])->each(function ($tag) use ($entry) {

0 commit comments

Comments
 (0)