ADD EV3-473 producers:alias-backfill - visszamenőleges alias-feltöltés
Két forrásból dolgozik: az archivált gyártók note mezőjéből (láncolást is követve, ha a célrekord maga is tovább olvadt), illetve a producers:dedupe --report JSON jelentéseiből, névalapú feloldással - a jelentésekben szereplő ID-k környezetenként eltérnek. Ütközés esetén kihagyja és jelenti a jelöltet, kétszeri futtatás nem hoz létre duplikált aliast.
This commit is contained in:
parent
dfa8c8725e
commit
5347ac5518
269
app/Console/Commands/ProducersAliasBackfill.php
Normal file
269
app/Console/Commands/ProducersAliasBackfill.php
Normal file
@ -0,0 +1,269 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Enums\DbStatusFieldEnum;
|
||||||
|
use App\Models\Producer;
|
||||||
|
use App\Models\ProducerAlias;
|
||||||
|
use App\Support\NameNormalizer;
|
||||||
|
use App\Support\ProducerAliasResolver;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visszamenőleges alias-feltöltés a már lezajlott EV3-357 gyártó-összevonásokból.
|
||||||
|
*
|
||||||
|
* Két forrásból dolgozik:
|
||||||
|
*
|
||||||
|
* 1. Az archivált gyártók `note` mezője ("Összevonva ide: #<id> (...)") - ez MINDIG
|
||||||
|
* a jelenlegi adatbázis saját, konzisztens ID-ire mutat, láncolást igényelhet
|
||||||
|
* (egy 1. körös célrekord a 2. körben maga is beolvadhatott).
|
||||||
|
* 2. A producers:dedupe --report kimeneteként keletkezett JSON jelentések (a döntési
|
||||||
|
* lapok, EV3-357 ticket-mappa). FONTOS: ezekben az ID-k környezetenként (fejlesztői/
|
||||||
|
* d2d/éles) eltérnek, ezért a feloldás NÉVALAPÚ - a jelentés `final_name` mezőjét
|
||||||
|
* a jelenlegi adatbázis aktív gyártói között normalizált egyezéssel keressük meg.
|
||||||
|
*/
|
||||||
|
class ProducersAliasBackfill extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'producers:alias-backfill
|
||||||
|
{reports?* : dedupe-jelentés JSON fájl(ok) útvonala (producers:dedupe --report kimenete)}
|
||||||
|
{--skip-archives : az archivált gyártók note mezejének feldolgozásának kihagyása}
|
||||||
|
{--apply : ténylegesen beszúrja az aliasokat (alapértelmezés: csak kimutatás)}';
|
||||||
|
|
||||||
|
protected $description = 'Alias rekordok visszamenőleges feltöltése a korábbi gyártó-összevonásokból';
|
||||||
|
|
||||||
|
public function handle(ProducerAliasResolver $resolver): int
|
||||||
|
{
|
||||||
|
$candidates = [];
|
||||||
|
|
||||||
|
if (! $this->option('skip-archives')) {
|
||||||
|
$candidates = array_merge($candidates, $this->collectFromArchivedProducers());
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->argument('reports') as $path) {
|
||||||
|
$candidates = array_merge($candidates, $this->collectFromReport($path));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($candidates === []) {
|
||||||
|
$this->info('Nincs feldolgozandó jelölt.');
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
[$toInsert, $alreadyExists, $conflicts] = $this->classify($resolver, $candidates);
|
||||||
|
|
||||||
|
$this->summarize($toInsert, $alreadyExists, $conflicts);
|
||||||
|
|
||||||
|
if (! $this->option('apply')) {
|
||||||
|
$this->newLine();
|
||||||
|
$this->info('Ez csak kimutatás volt. A végrehajtáshoz: --apply');
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
DB::transaction(function () use ($toInsert) {
|
||||||
|
foreach ($toInsert as $row) {
|
||||||
|
ProducerAlias::create([
|
||||||
|
'producer_id' => $row['producer_id'],
|
||||||
|
'name' => $row['name'],
|
||||||
|
'normalized_name' => NameNormalizer::normalize($row['name']),
|
||||||
|
'source' => 'backfill',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->newLine();
|
||||||
|
$this->info(count($toInsert) . ' alias létrehozva.');
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array{producer_id: int, name: string, origin: string}>
|
||||||
|
*/
|
||||||
|
private function collectFromArchivedProducers(): array
|
||||||
|
{
|
||||||
|
$candidates = [];
|
||||||
|
|
||||||
|
$archived = Producer::query()->where('status', DbStatusFieldEnum::archive)->get(['id', 'name', 'note']);
|
||||||
|
|
||||||
|
foreach ($archived as $producer) {
|
||||||
|
$visited = [];
|
||||||
|
$keeperId = $this->resolveActiveKeeperId($producer->id, $visited);
|
||||||
|
|
||||||
|
if ($keeperId === null) {
|
||||||
|
$this->warn(" ! #{$producer->id} (\"{$producer->name}\") note-ja nem oldható fel aktív gyártóra.");
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$candidates[] = [
|
||||||
|
'producer_id' => $keeperId,
|
||||||
|
'name' => (string) $producer->name,
|
||||||
|
'origin' => "archív #{$producer->id}",
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $candidates;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Egy archivált rekord note-jából kiolvassa a célrekordot, és ha az maga is
|
||||||
|
* archivált (2. körben tovább olvadt), a láncot az aktuális aktív rekordig követi.
|
||||||
|
*/
|
||||||
|
private function resolveActiveKeeperId(int $id, array &$visited): ?int
|
||||||
|
{
|
||||||
|
if (isset($visited[$id])) {
|
||||||
|
return null; // ciklusvédelem
|
||||||
|
}
|
||||||
|
$visited[$id] = true;
|
||||||
|
|
||||||
|
$producer = Producer::find($id);
|
||||||
|
|
||||||
|
if (! $producer) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! in_array($producer->status, [DbStatusFieldEnum::archive, DbStatusFieldEnum::deleted], true)) {
|
||||||
|
return $producer->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! preg_match('/Összevonva ide:\s*#(\d+)/u', (string) $producer->note, $matches)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->resolveActiveKeeperId((int) $matches[1], $visited);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array{producer_id: int, name: string, origin: string}>
|
||||||
|
*/
|
||||||
|
private function collectFromReport(string $path): array
|
||||||
|
{
|
||||||
|
if (! is_file($path)) {
|
||||||
|
$this->error("A jelentésfájl nem található: {$path}");
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$report = json_decode((string) file_get_contents($path), true);
|
||||||
|
|
||||||
|
if (! is_array($report) || ! isset($report['merges'])) {
|
||||||
|
$this->error("A fájl nem érvényes producers:dedupe jelentés: {$path}");
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$activeByNormalized = $this->activeProducersByNormalizedName();
|
||||||
|
$restoreProducers = $report['restore']['producers'] ?? [];
|
||||||
|
$candidates = [];
|
||||||
|
|
||||||
|
foreach ($report['merges'] as $merge) {
|
||||||
|
$finalName = (string) ($merge['final_name'] ?? '');
|
||||||
|
$targetId = $activeByNormalized[NameNormalizer::normalize($finalName)] ?? null;
|
||||||
|
|
||||||
|
if ($targetId === null) {
|
||||||
|
$this->warn(" ! \"{$finalName}\" ({$path}) nem található aktív gyártóként ebben a környezetben.");
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$names = array_column($merge['from'] ?? [], 'name');
|
||||||
|
|
||||||
|
// A jelentésben a keeper eredeti (átnevezés előtti) neve csak a
|
||||||
|
// restore.producers pillanatképben maradt meg - a merges tömb nem
|
||||||
|
// tartalmazza.
|
||||||
|
$keeperOriginalName = $restoreProducers[$merge['keeper_id']]['name'] ?? null;
|
||||||
|
if ($keeperOriginalName !== null) {
|
||||||
|
$names[] = (string) $keeperOriginalName;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (array_unique($names) as $name) {
|
||||||
|
if (NameNormalizer::normalize($name) === NameNormalizer::normalize($finalName)) {
|
||||||
|
continue; // ugyanaz, mint az élő név - felesleges alias lenne
|
||||||
|
}
|
||||||
|
|
||||||
|
$candidates[] = [
|
||||||
|
'producer_id' => $targetId,
|
||||||
|
'name' => $name,
|
||||||
|
'origin' => basename($path),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $candidates;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function activeProducersByNormalizedName(): array
|
||||||
|
{
|
||||||
|
return Producer::query()
|
||||||
|
->whereNotIn('status', [DbStatusFieldEnum::archive, DbStatusFieldEnum::deleted])
|
||||||
|
->get(['id', 'name'])
|
||||||
|
->keyBy(fn ($p) => NameNormalizer::normalize($p->name))
|
||||||
|
->map->id
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Szétválogatja a jelölteket: mit kell beszúrni, mi van már fent (idempotencia -
|
||||||
|
* a parancs többször is lefuttatható ugyanazokkal a bemenetekkel), és mi ütközik.
|
||||||
|
*
|
||||||
|
* @return array{0: array, 1: array, 2: array}
|
||||||
|
*/
|
||||||
|
private function classify(ProducerAliasResolver $resolver, array $candidates): array
|
||||||
|
{
|
||||||
|
$toInsert = [];
|
||||||
|
$alreadyExists = [];
|
||||||
|
$conflicts = [];
|
||||||
|
$seen = []; // normalized_name => true, hogy egy futáson belül ne duplázzunk
|
||||||
|
|
||||||
|
foreach ($candidates as $candidate) {
|
||||||
|
$normalized = NameNormalizer::normalize($candidate['name']);
|
||||||
|
|
||||||
|
if (isset($seen[$normalized])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$existing = ProducerAlias::where('normalized_name', $normalized)
|
||||||
|
->where('producer_id', $candidate['producer_id'])
|
||||||
|
->exists();
|
||||||
|
|
||||||
|
if ($existing) {
|
||||||
|
$alreadyExists[] = $candidate;
|
||||||
|
$seen[$normalized] = true;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$resolver->assertNameAvailable($candidate['name'], $candidate['producer_id']);
|
||||||
|
$toInsert[] = $candidate;
|
||||||
|
} catch (RuntimeException $e) {
|
||||||
|
$conflicts[] = $candidate + ['reason' => $e->getMessage()];
|
||||||
|
}
|
||||||
|
|
||||||
|
$seen[$normalized] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [$toInsert, $alreadyExists, $conflicts];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function summarize(array $toInsert, array $alreadyExists, array $conflicts): void
|
||||||
|
{
|
||||||
|
if ($toInsert !== []) {
|
||||||
|
$this->table(
|
||||||
|
['Forrás', 'Név', 'Célrekord'],
|
||||||
|
array_map(fn ($c) => [$c['origin'], $c['name'], '#' . $c['producer_id']], $toInsert)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->line('Beszúrandó: ' . count($toInsert)
|
||||||
|
. ' | már megvan: ' . count($alreadyExists)
|
||||||
|
. ' | ütközés: ' . count($conflicts));
|
||||||
|
|
||||||
|
foreach ($conflicts as $conflict) {
|
||||||
|
$this->warn(" ! \"{$conflict['name']}\" ({$conflict['origin']}): {$conflict['reason']}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
161
tests/Feature/ProducerAliasBackfillTest.php
Normal file
161
tests/Feature/ProducerAliasBackfillTest.php
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Enums\DbStatusFieldEnum;
|
||||||
|
use App\Models\Producer;
|
||||||
|
use App\Models\ProducerAlias;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
uses(TestCase::class, RefreshDatabase::class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array{name: string, origin: string}>
|
||||||
|
*/
|
||||||
|
function writeDedupeReportFixture(string $path, int $keeperId, string $finalName, array $fromNames, ?string $keeperOriginalName = null): void
|
||||||
|
{
|
||||||
|
$report = [
|
||||||
|
'created_at' => now()->toDateTimeString(),
|
||||||
|
'database' => 'fixture',
|
||||||
|
'merges' => [[
|
||||||
|
'keeper_id' => $keeperId,
|
||||||
|
'final_name' => $finalName,
|
||||||
|
'from' => array_map(fn ($n, $i) => ['id' => 1000 + $i, 'name' => $n], $fromNames, array_keys($fromNames)),
|
||||||
|
]],
|
||||||
|
'restore' => [
|
||||||
|
'producers' => $keeperOriginalName !== null
|
||||||
|
? [$keeperId => ['id' => $keeperId, 'name' => $keeperOriginalName]]
|
||||||
|
: [],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
file_put_contents($path, json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||||
|
}
|
||||||
|
|
||||||
|
test('az archivált gyártó note-jából aliast hoz létre a célrekordhoz --apply mellett', function () {
|
||||||
|
$keeper = Producer::create(['name' => 'Kőröstej Kft.', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]);
|
||||||
|
Producer::create([
|
||||||
|
'name' => 'Kőröstej',
|
||||||
|
'status' => DbStatusFieldEnum::archive,
|
||||||
|
'canSee' => 0,
|
||||||
|
'note' => "Összevonva ide: #{$keeper->id} (Kőröstej Kft.) – 2026-09-15 producers:dedupe",
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->artisan('producers:alias-backfill', ['--apply' => true])->assertExitCode(0);
|
||||||
|
|
||||||
|
expect(ProducerAlias::where('producer_id', $keeper->id)->pluck('name')->all())->toBe(['Kőröstej']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('--apply nélkül csak kimutat, nem ír adatbázist', function () {
|
||||||
|
$keeper = Producer::create(['name' => 'Danone', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]);
|
||||||
|
Producer::create([
|
||||||
|
'name' => 'Danone ',
|
||||||
|
'status' => DbStatusFieldEnum::archive,
|
||||||
|
'canSee' => 0,
|
||||||
|
'note' => "Összevonva ide: #{$keeper->id} (Danone) – 2026-09-15 producers:dedupe",
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->artisan('producers:alias-backfill')->assertExitCode(0);
|
||||||
|
|
||||||
|
expect(ProducerAlias::count())->toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a láncolt archiválást a jelenlegi aktív rekordig követi', function () {
|
||||||
|
// 1. kör: "Régi A" -> #B, de #B maga is beolvad a 2. körben #C-be.
|
||||||
|
$c = Producer::create(['name' => 'Végleges C', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]);
|
||||||
|
$b = Producer::create([
|
||||||
|
'name' => 'Köztes B',
|
||||||
|
'status' => DbStatusFieldEnum::archive,
|
||||||
|
'canSee' => 0,
|
||||||
|
'note' => "Összevonva ide: #{$c->id} (Végleges C) – 2026-09-16 producers:dedupe",
|
||||||
|
]);
|
||||||
|
Producer::create([
|
||||||
|
'name' => 'Régi A',
|
||||||
|
'status' => DbStatusFieldEnum::archive,
|
||||||
|
'canSee' => 0,
|
||||||
|
'note' => "Összevonva ide: #{$b->id} (Köztes B) – 2026-09-15 producers:dedupe",
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->artisan('producers:alias-backfill', ['--apply' => true])->assertExitCode(0);
|
||||||
|
|
||||||
|
expect(ProducerAlias::where('producer_id', $c->id)->pluck('name')->sort()->values()->all())
|
||||||
|
->toBe(['Köztes B', 'Régi A']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a feloldhatatlan note-ot kihagyja, nem szakítja meg a futást', function () {
|
||||||
|
Producer::create([
|
||||||
|
'name' => 'Árva Rekord',
|
||||||
|
'status' => DbStatusFieldEnum::archive,
|
||||||
|
'canSee' => 0,
|
||||||
|
'note' => 'Nincs értelmezhető formátum',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->artisan('producers:alias-backfill', ['--apply' => true])->assertExitCode(0);
|
||||||
|
|
||||||
|
expect(ProducerAlias::count())->toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a jelentésfájlból névalapon oldja fel a célrekordot', function () {
|
||||||
|
$keeper = Producer::create(['name' => 'Voyagex', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]);
|
||||||
|
$path = storage_path('app/private/teszt_alias_backfill_report.json');
|
||||||
|
// A "VOYAGEX" szándékosan hiányzik a várt eredményből: normalizálva megegyezik az
|
||||||
|
// élő "Voyagex" névvel, tehát a parancsnak redundánsként ki kell hagynia.
|
||||||
|
writeDedupeReportFixture($path, keeperId: 999, finalName: 'Voyagex', fromNames: ['Voyagex Kft', 'VOYAGEX']);
|
||||||
|
|
||||||
|
$this->artisan('producers:alias-backfill', ['reports' => [$path], '--skip-archives' => true, '--apply' => true])
|
||||||
|
->assertExitCode(0);
|
||||||
|
|
||||||
|
expect(ProducerAlias::where('producer_id', $keeper->id)->pluck('name')->sort()->values()->all())
|
||||||
|
->toBe(['Voyagex Kft']);
|
||||||
|
|
||||||
|
@unlink($path);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a jelentésben az átnevezett keeper eredeti neve is aliassá válik', function () {
|
||||||
|
$keeper = Producer::create(['name' => 'Végleges Név Kft', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]);
|
||||||
|
$path = storage_path('app/private/teszt_alias_backfill_rename.json');
|
||||||
|
writeDedupeReportFixture(
|
||||||
|
$path,
|
||||||
|
keeperId: 999,
|
||||||
|
finalName: 'Végleges Név Kft',
|
||||||
|
fromNames: ['Elavult Név'],
|
||||||
|
keeperOriginalName: 'Régi Saját Név'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->artisan('producers:alias-backfill', ['reports' => [$path], '--skip-archives' => true, '--apply' => true])
|
||||||
|
->assertExitCode(0);
|
||||||
|
|
||||||
|
expect(ProducerAlias::where('producer_id', $keeper->id)->pluck('name')->sort()->values()->all())
|
||||||
|
->toBe(['Elavult Név', 'Régi Saját Név']);
|
||||||
|
|
||||||
|
@unlink($path);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('ütköző jelöltet nem szúr be, csak jelenti', function () {
|
||||||
|
Producer::create(['name' => 'Bonduelle', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]);
|
||||||
|
$keeper = Producer::create(['name' => 'Danone', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]);
|
||||||
|
Producer::create([
|
||||||
|
'name' => 'Bonduelle', // ugyanaz a név, mint egy MÁSIK élő gyártóé - ütközik
|
||||||
|
'status' => DbStatusFieldEnum::archive,
|
||||||
|
'canSee' => 0,
|
||||||
|
'note' => "Összevonva ide: #{$keeper->id} (Danone) – 2026-09-15 producers:dedupe",
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->artisan('producers:alias-backfill', ['--apply' => true])->assertExitCode(0);
|
||||||
|
|
||||||
|
expect(ProducerAlias::count())->toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('kétszeri futtatás nem hoz létre duplikált aliast (idempotens)', function () {
|
||||||
|
$keeper = Producer::create(['name' => 'Danone', 'status' => DbStatusFieldEnum::active, 'canSee' => 1]);
|
||||||
|
Producer::create([
|
||||||
|
'name' => 'Danone ',
|
||||||
|
'status' => DbStatusFieldEnum::archive,
|
||||||
|
'canSee' => 0,
|
||||||
|
'note' => "Összevonva ide: #{$keeper->id} (Danone) – 2026-09-15 producers:dedupe",
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->artisan('producers:alias-backfill', ['--apply' => true])->assertExitCode(0);
|
||||||
|
$this->artisan('producers:alias-backfill', ['--apply' => true])->assertExitCode(0);
|
||||||
|
|
||||||
|
expect(ProducerAlias::count())->toBe(1);
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue
Block a user