Merge branch 'trunk'
This commit is contained in:
commit
327defbee0
3 changed files with 83 additions and 1 deletions
|
|
@ -98,7 +98,7 @@ class ProductInco extends Model
|
|||
{
|
||||
public function getId(): IncoId
|
||||
{
|
||||
return new IncoId($this->id);
|
||||
return new IncoId($this->no_art);
|
||||
}
|
||||
|
||||
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\Product,$this> */
|
||||
|
|
|
|||
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class () extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$incoMap = DB::table('product_incos')
|
||||
->pluck('no_art', 'product_id')
|
||||
->all();
|
||||
|
||||
DB::table('order_lines')
|
||||
->whereNotNull('product')
|
||||
->orderBy('id')
|
||||
->chunk(500, function ($lines) use ($incoMap): void {
|
||||
foreach ($lines as $line) {
|
||||
$data = json_decode((string) $line->product, true);
|
||||
if (! is_array($data) || ! isset($data['original_id'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$productId = (string) $data['original_id'];
|
||||
if (! isset($incoMap[$productId])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$expectedNoArt = (int) $incoMap[$productId];
|
||||
$currentIncoId = isset($data['inco_id']) ? (int) $data['inco_id'] : null;
|
||||
|
||||
if ($currentIncoId !== $expectedNoArt) {
|
||||
$data['inco_id'] = $expectedNoArt;
|
||||
|
||||
DB::table('order_lines')
|
||||
->where('id', $line->id)
|
||||
->update([
|
||||
'product' => json_encode($data, \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES),
|
||||
]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
// Data repair migration, no rollback.
|
||||
}
|
||||
};
|
||||
25
tests/Integration/WithApp/App/Models/ProductIncoTest.php
Normal file
25
tests/Integration/WithApp/App/Models/ProductIncoTest.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Integration\WithApp\App\Models;
|
||||
|
||||
use App\Models\ProductInco;
|
||||
use Domain\Models\ValueObjects\IncoId;
|
||||
|
||||
describe('ProductInco model', function (): void {
|
||||
it('returns IncoId value object using no_art instead of surrogate id', function (): void {
|
||||
// GIVEN
|
||||
$inco = new ProductInco();
|
||||
$inco->id = 258;
|
||||
$inco->no_art = 140317;
|
||||
|
||||
// WHEN
|
||||
$incoId = $inco->getId();
|
||||
|
||||
// THEN
|
||||
expect($incoId)->toBeInstanceOf(IncoId::class)
|
||||
->and($incoId->value)->toBe(140317)
|
||||
->and($incoId->value)->not->toBe(258);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue