fix(database): clean orders employee service code and legacy site names
- Strip site prefix and non-digits from service code in orders.employee JSON snapshot - Normalize legacy site names with spaces (LDC SERVICES, LDC VOLAILLES, LDC TER) in orders.employee and employees tables
This commit is contained in:
parent
e0c4b34ed8
commit
30c4483ce9
1 changed files with 68 additions and 0 deletions
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
return new class () extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
DB::table('orders')
|
||||
->whereNotNull('employee')
|
||||
->orderBy('id')
|
||||
->chunk(500, function ($orders): void {
|
||||
foreach ($orders as $order) {
|
||||
$data = json_decode((string) $order->employee, true);
|
||||
if (! is_array($data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$site = $data['site'] ?? '';
|
||||
$site = match ($site) {
|
||||
'LDC SERVICES' => 'LDCSER',
|
||||
'LDC VOLAILLES' => 'LDCVOL',
|
||||
'LDC TER' => 'LDCTER',
|
||||
default => $site,
|
||||
};
|
||||
$data['site'] = $site;
|
||||
|
||||
$service = $data['service'] ?? '';
|
||||
$data['service'] = (string) Str::of((string) $service)
|
||||
->replaceFirst((string) $site, '')
|
||||
->replaceMatches('/[^\d]/', '');
|
||||
|
||||
DB::table('orders')
|
||||
->where('id', $order->id)
|
||||
->update([
|
||||
'employee' => json_encode($data, JSON_THROW_ON_ERROR),
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
DB::table('employees')
|
||||
->where('site', 'LDC SERVICES')
|
||||
->update(['site' => 'LDCSER']);
|
||||
|
||||
DB::table('employees')
|
||||
->where('site', 'LDC VOLAILLES')
|
||||
->update(['site' => 'LDCVOL']);
|
||||
|
||||
DB::table('employees')
|
||||
->where('site', 'LDC TER')
|
||||
->update(['site' => 'LDCTER']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
throw new \Error('WONT ROLLBACK');
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue