Skip to content

Commit

Permalink
Add test, fix cleanup command for polylines
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrLevin committed Oct 11, 2024
1 parent 46aba6f commit af4886f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
30 changes: 17 additions & 13 deletions app/Console/Commands/DatabaseCleaner/Polylines.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,29 @@ class Polylines extends Command
protected $description = 'Find and delete unused and old polylines from database';

public function handle(): int {
$start = microtime(true);
$rows = DB::table('poly_lines')
->selectRaw('poly_lines.id, poly_lines.parent_id')
->leftJoin('hafas_trips', 'poly_lines.id', '=', 'hafas_trips.polyline_id')
->leftJoin(
'poly_lines AS parent_poly_lines',
'poly_lines.id',
'=',
'parent_poly_lines.parent_id'
)
->whereRaw('hafas_trips.polyline_id IS NULL AND parent_poly_lines.parent_id IS NULL')
->get();
$start = microtime(true);
$rows = DB::table('poly_lines')
->selectRaw('poly_lines.id, poly_lines.parent_id')
->leftJoin('hafas_trips', 'poly_lines.id', '=', 'hafas_trips.polyline_id')
->leftJoin(
'poly_lines AS parent_poly_lines',
'poly_lines.id',
'=',
'parent_poly_lines.parent_id'
)
->whereRaw('hafas_trips.polyline_id IS NULL AND parent_poly_lines.parent_id IS NULL')
->get();
$this->info('Found ' . $rows->count() . ' unused polylines.');
$affectedRows = 0;

// get 100 rows at a time
foreach ($rows->chunk(100) as $chunk) {
$ids = $chunk->pluck('id')->toArray();
$affectedRows += PolyLine::whereIn('id', $ids)->delete();
$row = PolyLine::whereIn('id', $ids)->get();
foreach ($row as $polyline) {
$polyline->delete();
$affectedRows++;
}
$this->output->write('.');
}
$this->output->newLine();
Expand Down
4 changes: 2 additions & 2 deletions app/Models/PolyLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public function __get($key) {
return parent::__get($key);
}

public function delete() {
public function delete(): ?bool {
$this->polylineStorageService->delete($this->hash);
return parent::delete(); // TODO: Change the autogenerated stub
return parent::delete();
}
}
18 changes: 14 additions & 4 deletions tests/Feature/Commands/CleanUpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use App\Models\Trip;
use App\Models\User;
use App\Notifications\StatusLiked;
use App\Services\PolylineStorageService;
use Illuminate\Console\Command;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Str;
use PHPUnit\Framework\MockObject\Exception;
use Tests\FeatureTestCase;

class CleanUpTest extends FeatureTestCase
Expand Down Expand Up @@ -96,17 +98,25 @@ public function testUsersThatHaventAcceptedPrivacyPolicyWithinADayAreRemoved():
$this->assertDatabaseCount('users', 0);
}

/**
* @throws Exception
*/
public function testPolylineWithoutAnyReferenceAreDeleted(): void {
$this->assertDatabaseCount('poly_lines', 0);
$service = new PolylineStorageService();

PolyLine::create([
'hash' => Str::uuid(),
'polyline' => json_encode(['some json data']),
]);
$polyline = PolyLine::create([
'hash' => Str::uuid(),
'polyline' => json_encode(['some json data']),
]);
$content = $polyline->polyline; // this will store the polyline in the storage
$hash = $polyline->hash;
$this->assertDatabaseCount('poly_lines', 1);
$this->assertSame($content, $service->get($hash));

$this->artisan('app:clean-db:polylines')->assertExitCode(Command::SUCCESS);
$this->assertDatabaseCount('poly_lines', 0);
$this->assertSame('', $service->get($hash));

//create a polyline with a reference and a parent
//Checkin Factory creates a trip which creates a polyline
Expand Down

0 comments on commit af4886f

Please sign in to comment.