Skip to content

Commit

Permalink
🧑‍💻 add migration script for stopovers (#2226)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored Dec 20, 2023
1 parent 3dba5a9 commit e562c25
Showing 1 changed file with 7 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
<?php

namespace App\Console\Commands;

use App\Models\TrainCheckin;
use Illuminate\Console\Command;
use Illuminate\Database\Migrations\Migration;

/**
* @deprecated Just created and marked as deprecated, because it is only needed for migrating old checkins.
* Can be deleted after migration.
*/
class MigrateStopovers extends Command
return new class extends Migration
{

protected $signature = 'app:migrate-stopovers';
protected $description = 'Calculate missing stopover relations for train checkins. Currently only needed for migrating old checkins.';

public function handle(): int {
public function up(): void {
while (TrainCheckin::whereNull('origin_stopover_id')->orWhereNull('destination_stopover_id')->count() > 0) {
TrainCheckin::with(['HafasTrip.stopovers', 'originStation', 'destinationStation'])
->whereNull('origin_stopover_id')
Expand All @@ -31,22 +21,20 @@ public function handle(): int {
->first();

if ($originStopover === null) {
$this->error("Could not find origin stopover for checkin {$checkin->id}");
echo "ERROR: Could not find origin stopover for checkin {$checkin->id}\n";
return;
}
if ($destinationStopover === null) {
$this->error("Could not find destination stopover for checkin {$checkin->id}");
echo "ERROR: Could not find destination stopover for checkin {$checkin->id}\n";
return;
}

$checkin->update([
'origin_stopover_id' => $originStopover->id,
'destination_stopover_id' => $destinationStopover->id,
]);

$this->info("Migrated stopover ids for checkin {$checkin->id}");
echo ".";
});
}
return Command::SUCCESS;
}
}
};

0 comments on commit e562c25

Please sign in to comment.