commit 30/9
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-09-30 09:05:43 +02:00
parent 67dad461d9
commit 3dc7c5c649
75 changed files with 4123 additions and 107 deletions
+14
View File
@@ -0,0 +1,14 @@
<?php
namespace App\Services\Mne;
use App\Models\ExistingBusiness;
use Illuminate\Database\Eloquent\Builder;
interface BusinessRepository
{
/** @return Builder<ExistingBusiness> */
public function query(): Builder;
public function find(string $id, bool $forUpdate = false): ExistingBusiness;
}
@@ -0,0 +1,73 @@
<?php
namespace App\Services\Mne;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;
final class CompatibilityBackfill
{
/** @return array<string, int> */
public function run(int $batchSize = 100): array
{
$counts = [];
foreach (['identity' => ['users', 'identity_links', 'user_id'], 'sector' => ['sectors', 'sector_links', 'sector_id'],
'business' => ['existing_businesses', 'business_links', 'legacy_business_id']] as $domain => [$source, $target, $key]) {
// Start again on each invocation to detect edits behind a previous checkpoint.
$last = null;
$processed = 0;
do {
$batch = DB::transaction(function () use ($domain, $source, $target, $key, $last, $processed, $batchSize) {
DB::select('SELECT pg_advisory_xact_lock(290926,170004)');
$query = DB::table('public.'.$source)->orderBy('id')->limit($batchSize)->lockForUpdate();
if ($last !== null) {
$query->where('id', '>', $last);
}
$rows = $query->get();
$checksums = [];
foreach ($rows as $row) {
$checksum = hash('sha256', json_encode((array) $row, JSON_THROW_ON_ERROR));
$checksums[] = $checksum;
$stableId = (string) Uuid::uuid5(Uuid::NAMESPACE_URL, 'investmoz:'.$domain.':'.$row->id);
$data = ['id' => $stableId, $key => $row->id, 'source_system' => 'public.'.$source,
'source_checksum' => $checksum, 'status' => 'pending'];
if ($domain === 'sector') {
$data['canonical_sector_id'] = $stableId;
$data['source_code'] = $row->slug;
$data['status'] = $row->is_active && $row->deleted_at === null ? 'active' : 'pending';
}
$existing = DB::table('mne.'.$target)->where($key, $row->id)->first();
if (! $existing) {
DB::table('mne.'.$target)->insert($data);
} elseif ($existing->source_checksum !== $checksum) {
// Changed sources invalidate previous reconciliation; never activate by inference.
DB::table('mne.'.$target)->where($key, $row->id)->update(['source_checksum' => $checksum, 'status' => 'pending']);
}
if ($domain !== 'sector' && (! $existing || $existing->status !== 'active' || $existing->source_checksum !== $checksum)) {
DB::table('mne.compatibility_rejections')->upsert([[
'id' => $stableId, 'domain' => $domain, 'source_key' => (string) $row->id,
'code' => 'CANONICAL_MAPPING_REQUIRED', 'cause' => 'Explicit canonical mapping and reconciliation required',
'resolved_at' => null, 'resolution' => null,
]], ['domain', 'source_key', 'code'], ['cause', 'resolved_at', 'resolution']);
}
}
if ($rows->isNotEmpty() || $processed === 0) {
DB::table('mne.compatibility_checkpoints')->updateOrInsert(['domain' => $domain], [
'last_key' => $rows->isEmpty() ? $last : (string) $rows->last()->id,
'processed' => $processed + $rows->count(), 'checksum' => hash('sha256', implode('', $checksums)), 'updated_at' => now(),
]);
}
return $rows;
});
$processed += $batch->count();
if ($batch->isNotEmpty()) {
$last = $batch->last()->id;
}
} while ($batch->count() === $batchSize);
$counts[$domain] = $processed;
}
return $counts;
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace App\Services\Mne;
use App\Jobs\CompareMneBusiness;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
final class CompatibilityShadow
{
/** Invoke only after authorization; optional comparison must never affect the response. */
public function schedule(string $businessId): void
{
if (! config('existing-business.compatibility_shadow', false)) {
return;
}
try {
$control = DB::transaction(fn () => DB::table('mne.compatibility_controls')->where('capability', 'shadow')->first());
if (! $control || ! $control->enabled || (hexdec(substr(hash('sha256', $businessId), 0, 6)) % 100) >= $control->percentage) {
return;
}
$connection = (string) config('existing-business.shadow_queue', 'database');
if (config('queue.connections.'.$connection.'.driver') !== 'database') {
Log::info('mne.compatibility.shadow', ['outcome' => 'async_queue_required']);
return;
}
CompareMneBusiness::dispatch($businessId)->onConnection($connection)->onQueue('mne-shadow')->afterCommit();
} catch (\Throwable) {
Log::info('mne.compatibility.shadow', ['outcome' => 'dispatch_error']);
}
}
}
@@ -0,0 +1,23 @@
<?php
namespace App\Services\Mne;
use App\Models\ExistingBusiness;
use Illuminate\Database\Eloquent\Builder;
final class CompatibleBusinessRepository implements BusinessRepository
{
public function __construct(private LegacyBusinessRepository $legacy) {}
/** @return Builder<ExistingBusiness> */
public function query(): Builder
{
return $this->legacy->query();
}
public function find(string $id, bool $forUpdate = false): ExistingBusiness
{
// No configuration can transfer writes or bypass existing policies.
return $this->legacy->find($id, $forUpdate);
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace App\Services\Mne;
use Illuminate\Support\Facades\DB;
final class IdentityResolver
{
public function resolve(int $userId): ?string
{
$id = DB::table('mne.identity_links as l')->join('mne.parties as p', 'p.id', '=', 'l.party_id')
->join('public.users as u', 'u.id', '=', 'l.user_id')
->where('l.user_id', $userId)->where('l.status', 'active')->where('p.party_type', 'person')
->whereNull('p.deleted_at')->where('l.valid_from', '<=', now())
->where(fn ($q) => $q->whereNull('l.valid_until')->orWhere('l.valid_until', '>', now()))
->value('l.party_id');
return is_string($id) ? $id : null;
}
}
@@ -0,0 +1,25 @@
<?php
namespace App\Services\Mne;
use App\Models\ExistingBusiness;
use Illuminate\Database\Eloquent\Builder;
final class LegacyBusinessRepository implements BusinessRepository
{
/** @return Builder<ExistingBusiness> */
public function query(): Builder
{
return ExistingBusiness::query();
}
public function find(string $id, bool $forUpdate = false): ExistingBusiness
{
$query = $this->query()->whereKey($id);
if ($forUpdate) {
$query->lockForUpdate();
}
return $query->firstOrFail();
}
}
@@ -0,0 +1,25 @@
<?php
namespace App\Services\Mne;
use Illuminate\Support\Facades\DB;
/** Read-only comparison adapter. Does not hydrate a legacy model or grant authority. */
final class MneBusinessRepository
{
/** @return array<string, mixed>|null */
public function comparison(string $legacyId): ?array
{
$row = DB::table('mne.business_links as l')
->join('mne.existing_businesses as b', 'b.id', '=', 'l.canonical_business_id')
->join('mne.business_modalities as m', 'm.id', '=', 'b.modality_id')
->join('mne.workflow_states as s', 's.id', '=', 'b.status_id')
->join('mne.data_classifications as c', 'c.id', '=', 'b.confidentiality_id')
->where('l.legacy_business_id', $legacyId)->where('l.status', 'active')
->where('l.valid_from', '<=', now())->where(fn ($q) => $q->whereNull('l.valid_until')->orWhere('l.valid_until', '>', now()))
->whereNull('b.deleted_at')->first(['b.public_code as code', 'm.code as modality', 's.code as status',
'c.code as classification', 'b.version as version_no']);
return $row ? (array) $row : null;
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace App\Services\Mne;
use Illuminate\Support\Facades\DB;
final class SectorResolver
{
public const VERSION = 1;
public function resolve(int $legacyId): ?string
{
$id = DB::table('mne.sector_links as l')->join('public.sectors as s', 's.id', '=', 'l.sector_id')
->where('l.sector_id', $legacyId)->where('l.resolver_version', self::VERSION)
->where('l.status', 'active')->where('s.is_active', true)->whereNull('s.deleted_at')
->whereColumn('l.source_code', 's.slug')->where('l.valid_from', '<=', now())
->where(fn ($q) => $q->whereNull('l.valid_until')->orWhere('l.valid_until', '>', now()))
->value('l.canonical_sector_id');
return is_string($id) ? $id : null;
}
}