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

Add Command to Truncate Events and Orgs Tables #193

Merged
Merged
Show file tree
Hide file tree
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
60 changes: 60 additions & 0 deletions app/Console/Commands/TruncateOrgsAndEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Console\Commands;

use DateInterval;
use DateTimeInterface;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Isolatable;
use Symfony\Component\Console\Helper\ProgressBar;

class TruncateOrgsAndEvents extends Command implements Isolatable
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:truncate-orgs-and-events';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Truncates the events and orgs tables for debugging purposes.';

/*
* Lock timeout for the command.
*/
public function isolationLockExpiresAt(): DateTimeInterface|DateInterval
{
return now()->addMinutes(1);
}

/**
* Execute the console command.
*/
public function handle()
{
$models_to_truncate = [
"Events" => "App\Models\Event",
"Orgs" => "App\Models\Org"
];

ProgressBar::setFormatDefinition('simplified', '%current% of %max% tables truncated --- %message%');
$progressIndicator = $this->output->createProgressBar(sizeof($models_to_truncate));
$progressIndicator->setFormat('simplified');

$progressIndicator->start();

foreach ($models_to_truncate as $model_name => $model_namespace) {
$progressIndicator->setMessage("Truncating " . $model_name);
$model_namespace::truncate();
$progressIndicator->advance();
}

$progressIndicator->setMessage("Events and Orgs have been successfully truncated.");
$progressIndicator->finish();
}
}
26 changes: 26 additions & 0 deletions tests/Feature/TruncateOrgsAndEventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use App\Console\Commands\TruncateOrgsAndEvents;
use App\Models\Event;
use App\Models\Org;

use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('wipes the events and orgs tables', function () {
$expected_count = 10;

Event::factory()->count($expected_count)->create();

// Check pre-wipe count
expect(Event::count())->toEqual($expected_count);
expect(Org::count())->toEqual($expected_count);


Artisan::call(TruncateOrgsAndEvents::class);

// Ensure tables are squeaky clean
expect(Event::count())->toEqual(0);
expect(Org::count())->toEqual(0);
});
Loading