Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Ignore duplicate tags #1431

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading