Skip to content

Commit

Permalink
Clear command improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Nov 8, 2023
1 parent bed1d8f commit 5b07bc1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
23 changes: 9 additions & 14 deletions src/Commands/ClearCommand.php → src/Commands/PurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Database\DatabaseManager;
use Laravel\Pulse\Contracts\Storage;
use Laravel\Pulse\Pulse;
use Laravel\Pulse\Queries\Concerns\InteractsWithConnection;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'pulse:clear')]
class ClearCommand extends Command
#[AsCommand(name: 'pulse:purge')]
class PurgeCommand extends Command
{
use ConfirmableTrait, InteractsWithConnection;
use ConfirmableTrait;

/**
* The database mananger.
Expand All @@ -34,35 +35,29 @@ class ClearCommand extends Command
*
* @var string
*/
public $signature = 'pulse:clear {--force : Force the operation to run when in production}';
public $signature = 'pulse:purge {--force : Force the operation to run when in production}';

/**
* The command's description.
*
* @var string
*/
public $description = 'Clear Pulse data';
public $description = 'Purge Pulse data';

/**
* Handle the command.
*/
public function handle(
Pulse $pulse,
Repository $config,
DatabaseManager $db,
Storage $storage,
): int {
$this->db = $db;
$this->config = $config;

if (! $this->confirmToProceed()) {
return Command::FAILURE;
}

$pulse->tables()->each(function ($table) {
$this->info("Clearing {$table}...");
$storage->purge($pulse->tables());

$this->connection()->table($table)->truncate();
});
$this->components->info("Tables purged.");

return Command::SUCCESS;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Contracts/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ public function store(Collection $items): void;
* @param \Illuminate\Support\Collection<int, string> $tables
*/
public function trim(Collection $tables): void;

/**
* Purge the stored entries from the given tables.
*
* @param \Illuminate\Support\Collection<int, string> $tables
*/
public function purge(Collection $tables): void;
}
2 changes: 1 addition & 1 deletion src/PulseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ protected function registerCommands(): void
Commands\CheckCommand::class,
Commands\RestartCommand::class,
Commands\RegroupCommand::class,
Commands\ClearCommand::class,
Commands\PurgeCommand::class,
]);
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/Storage/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public function store(Collection $items): void
}

/**
* Trim the stored entries.
* Trim the stored entries from the given tables.
*
* @param \Illuminate\Support\Collection<int, string> $tables
*/
public function trim(Collection $tables): void
{
Expand All @@ -79,6 +81,18 @@ public function trim(Collection $tables): void
->delete());
}

/**
* Purge the stored entries from the given tables.
*
* @param \Illuminate\Support\Collection<int, string> $tables
*/
public function purge(Collection $tables): void
{
$tables->each(fn (string $table) => $this->connection()
->table($table)
->truncate());
}

/**
* The interval to trim the storage to.
*/
Expand Down

0 comments on commit 5b07bc1

Please sign in to comment.