Files
Investidor-V2/app/Services/TechnicalSupportProfileSchema.php
T

41 lines
2.4 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/** One schema interpretation shared by the installer and runtime capability. */
final class TechnicalSupportProfileSchema
{
public const MIGRATION = '2026_11_03_080000_add_technical_support_profile';
/** @return array{has_table:bool,ledger:bool,state:string,installed:bool,consistent:bool,repairable:bool,fingerprint:string} */
public function inspect(): array
{
$table = Schema::hasTable('portal_role_assignments');
$ledger = Schema::hasTable('migrations') && DB::table('migrations')->where('migration', self::MIGRATION)->exists();
$constraint = $table ? DB::selectOne("SELECT pg_get_constraintdef(c.oid) AS definition,c.convalidated AS validated FROM pg_constraint c JOIN pg_class t ON t.oid=c.conrelid JOIN pg_namespace n ON n.oid=t.relnamespace WHERE n.nspname='public' AND t.relname='portal_role_assignments' AND c.conname='portal_role_name' AND c.contype='c'") : null;
$state = $table ? 'unrecognised' : 'absent';
if ($constraint && $constraint->validated === true && is_string($constraint->definition)) {
preg_match_all("/'([^']+)'/", $constraint->definition, $matches);
$roles = $matches[1];
sort($roles);
$shape = preg_replace(["/'[a-z_]+'/", '/::character varying(?:\[\])?/', '/::text(?:\[\])?/', '/\s+/', '/[()]/'], ['?', '', '', '', ''], $constraint->definition);
$base = ['auditor', 'promoter', 'publisher', 'validator'];
$support = ['auditor', 'promoter', 'publisher', 'technical_support', 'validator'];
if ($shape === 'CHECKrole=ANYARRAY['.implode(',', array_fill(0, count($roles), '?')).']') {
if ($roles === $base) {
$state = 'base';
} elseif ($roles === $support) {
$state = 'support';
}
}
}
return ['has_table' => $table, 'ledger' => $ledger, 'state' => $state, 'installed' => $ledger && $state === 'support',
'consistent' => (! $table && ! $ledger) || ($state === 'base' && ! $ledger) || ($state === 'support' && $ledger), 'repairable' => $ledger && $state === 'base',
'fingerprint' => hash('sha256', json_encode([$constraint?->definition, $constraint?->validated], JSON_THROW_ON_ERROR))];
}
}