Skip to content

Commit

Permalink
[5.x] Enhance Telescope Clear Method (#1507)
Browse files Browse the repository at this point in the history
* improve the `clear` method in `DatabaseEntriesRepository.php`

* improve the `clear` method in `DatabaseEntriesRepository.php`

* improve the `clear` method in `DatabaseEntriesRepository.php`

* wip

* wip

* Update DatabaseEntriesRepository.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
a1383n and taylorotwell committed Aug 20, 2024
1 parent e518029 commit c3a794a
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/Storage/DatabaseEntriesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Laravel\Telescope\Storage;

use DateTimeInterface;
use Illuminate\Database\QueryException;
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Laravel\Telescope\Contracts\ClearableRepository;
use Laravel\Telescope\Contracts\EntriesRepository as Contract;
use Laravel\Telescope\Contracts\PrunableRepository;
Expand Down Expand Up @@ -378,13 +380,22 @@ public function prune(DateTimeInterface $before, $keepExceptions)
*/
public function clear()
{
do {
$deleted = $this->table('telescope_entries')->take($this->chunkSize)->delete();
} while ($deleted !== 0);

do {
$deleted = $this->table('telescope_monitoring')->take($this->chunkSize)->delete();
} while ($deleted !== 0);
try {
Schema::disableForeignKeyConstraints();

$this->table('telescope_entries')->truncate();
$this->table('telescope_monitoring')->truncate();
} catch (QueryException) {
do {
$deleted = $this->table('telescope_entries')->take($this->chunkSize)->delete();
} while ($deleted !== 0);

do {
$deleted = $this->table('telescope_monitoring')->take($this->chunkSize)->delete();
} while ($deleted !== 0);
} finally {
Schema::enableForeignKeyConstraints();
}
}

/**
Expand Down

0 comments on commit c3a794a

Please sign in to comment.