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 1 commit
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/ClearOrgsAndEvents.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 ClearOrgsAndEvents extends Command implements Isolatable
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:clear-orgs-and-events';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Clears out the events and orgs tables for debugging purposes.';
ThorntonMatthewD marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

$progressIndicator->start();

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

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

use App\Console\Commands\ClearOrgsAndEvents;
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;

// This will also create 10 orgs
Event::factory()->count($expected_count)->create();
allella marked this conversation as resolved.
Show resolved Hide resolved

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


Artisan::call(ClearOrgsAndEvents::class);

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