Skip to content

Commit

Permalink
Ignore duplicate tags (#1431)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints authored Jan 30, 2024
1 parent 4f9a644 commit 2c52952
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/Storage/DatabaseEntriesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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...
}
});
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2c52952

Please sign in to comment.