Updated Registration Column name and distance achievement units
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$this->renameDepartureBoardColumn('registration', 'aircraft_registration');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
$this->renameDepartureBoardColumn('aircraft_registration', 'registration');
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk every user's settings JSON and rename a value inside
|
||||
* departure_board_columns, if present.
|
||||
*/
|
||||
private function renameDepartureBoardColumn(string $from, string $to): void
|
||||
{
|
||||
DB::table('users')
|
||||
->select('id', 'settings')
|
||||
->whereNotNull('settings')
|
||||
->orderBy('id')
|
||||
->chunkById(500, function ($users) use ($from, $to) {
|
||||
foreach ($users as $user) {
|
||||
$settings = json_decode($user->settings, true);
|
||||
|
||||
// Skip rows with malformed/empty JSON rather than blowing up the migration.
|
||||
if (! is_array($settings) || ! isset($settings['departure_board_columns'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! is_array($settings['departure_board_columns'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$columns = $settings['departure_board_columns'];
|
||||
$key = array_search($from, $columns, true);
|
||||
|
||||
if ($key === false) {
|
||||
continue; // nothing to change for this user
|
||||
}
|
||||
|
||||
$columns[$key] = $to;
|
||||
$settings['departure_board_columns'] = $columns;
|
||||
|
||||
DB::table('users')
|
||||
->where('id', $user->id)
|
||||
->update([
|
||||
'settings' => json_encode($settings),
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user