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))]; } }