-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🗃️ prepare checkins table to use relation instead of duplicate data (#…
- Loading branch information
1 parent
3a9f12d
commit e31071e
Showing
6 changed files
with
203 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\TrainCheckin; | ||
use Illuminate\Console\Command; | ||
|
||
/** | ||
* @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 | ||
{ | ||
|
||
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 { | ||
while (TrainCheckin::whereNull('origin_stopover_id')->orWhereNull('destination_stopover_id')->count() > 0) { | ||
TrainCheckin::with(['HafasTrip.stopovers', 'originStation', 'destinationStation']) | ||
->whereNull('origin_stopover_id') | ||
->orWhereNull('destination_stopover_id') | ||
->limit(100) | ||
->each(function(TrainCheckin $checkin) { | ||
$originStopover = $checkin->HafasTrip->stopovers->where('train_station_id', $checkin->originStation->id) | ||
->where('departure_planned', $checkin->departure) | ||
->first(); | ||
$destinationStopover = $checkin->HafasTrip->stopovers->where('train_station_id', $checkin->destinationStation->id) | ||
->where('arrival_planned', $checkin->arrival) | ||
->first(); | ||
$checkin->update([ | ||
'origin_stopover_id' => $originStopover->id, | ||
'destination_stopover_id' => $destinationStopover->id, | ||
]); | ||
|
||
$this->info("Migrated stopover ids for checkin {$checkin->id}"); | ||
}); | ||
} | ||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...tions/2023_12_18_000000_add_origin_and_destination_stopover_foreign_to_train_checkins.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
/** | ||
* Currently we have origin, destination, departure and arrival in the train_checkins table. | ||
* This is not well-designed, because we have to store the same information multiple times. | ||
* This migration adds two new columns for a foreign key to the stopovers table, so we can remove the duplicate | ||
* information in the future. | ||
* | ||
* This is intentionally kept so small to make migration easier. | ||
* The double columns should be gradually changed in the code. | ||
*/ | ||
return new class extends Migration | ||
{ | ||
public function up(): void { | ||
Schema::table('train_checkins', static function(Blueprint $table) { | ||
$table->unsignedBigInteger('origin_stopover_id')->nullable()->after('origin'); | ||
$table->unsignedBigInteger('destination_stopover_id')->nullable()->after('destination'); | ||
|
||
$table->foreign('origin_stopover_id')->references('id')->on('train_stopovers'); | ||
$table->foreign('destination_stopover_id')->references('id')->on('train_stopovers'); | ||
}); | ||
} | ||
|
||
public function down(): void { | ||
Schema::table('train_checkins', static function(Blueprint $table) { | ||
$table->dropForeign(['origin_stopover_id']); | ||
$table->dropForeign(['destination_stopover_id']); | ||
|
||
$table->dropColumn('origin_stopover_id'); | ||
$table->dropColumn('destination_stopover_id'); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters