Files
compras/scripts/check-inventory-conversion-pilot.php
T
nala 96b30368bc
tests / ci (push) Canceled after 0s
commit compras5 2911
2026-11-29 19:49:19 +02:00

85 lines
5.1 KiB
PHP

<?php
use App\Services\InventoryConversionRuntime;
use App\Services\ReceiptStockMovement;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\DB;
require dirname(__DIR__).'/vendor/autoload.php';
if (count($argv) !== 2 || ! in_array($argv[1], ['--read-only', '--installed-read-only'], true)) {
throw new LogicException('Explicit read-only preflight required.');
}
$root = dirname(__DIR__);
$installed = $argv[1] === '--installed-read-only';
$sourcePath = 'C:/laragon/data/sgf-tempo/secrets/payables-demo-compras.json';
$sourceBytes = file_get_contents($sourcePath);
$source = json_decode($sourceBytes, true, 32, JSON_THROW_ON_ERROR);
$sourceHash = hash('sha256', $sourceBytes);
$publicHash = hash_file('sha256', $root.'/public/build/manifest.json');
if ($source['APP_ENV'] !== 'payable-testing' || $source['DB_HOST'] !== '127.0.0.1' || (string) $source['DB_PORT'] !== '5432'
|| realpath($source['HOST_ROOT']) !== realpath($root)
|| ! preg_match('/^compras_receipts_test_([a-f0-9]{12})$/D', $source['DB_DATABASE'], $match)
|| $source['DB_USERNAME'] !== 'payable_qa_'.$match[1]
|| $source['COMPRAS_RUNTIME_INSTALLATION_ID'] !== '0602a856-0413-480f-9ef3-1a1f3238bb90'
|| $source['COMPRAS_RUNTIME_SHA256'] !== '80483320a54af6f123498e43c1435a20922455f65860be88bd259e90efcbc0b7'
|| ! in_array($source['COMPRAS_INVENTORY_CONVERSION_ENABLED'] ?? 'false', $installed ? ['true', true] : ['false', false], true)) {
throw new LogicException('Pilot source differs from reviewed installation.');
}
$candidate = $installed ? 'C:/laragon/data/compras/runtime/inventory-8608e6551dea6448' : $root.'/packages/inventory-write/releases/0.2.0-rc.1';
if ($installed && ($source['COMPRAS_INVENTORY_ROOT'] ?? null) !== $candidate) {
throw new LogicException('Installed inventory location differs.');
}
InventoryConversionRuntime::verifyArtifact($candidate);
$environment = [...$source, 'APP_ENV' => 'pilot-readonly', 'APP_DEBUG' => 'false',
'DB_CONNECTION' => 'pgsql', 'DB_URL' => '', 'PGOPTIONS' => '-c default_transaction_read_only=on',
'COMPRAS_INVENTORY_CONVERSION_ENABLED' => 'true', 'COMPRAS_INVENTORY_ROOT' => $candidate,
'CACHE_STORE' => 'array', 'SESSION_DRIVER' => 'array', 'QUEUE_CONNECTION' => 'null', 'MAIL_MAILER' => 'array',
'APP_CONFIG_CACHE' => $root.'/storage/framework/absent-inventory-readonly-config.php', 'LOG_CHANNEL' => 'stderr'];
foreach ($environment as $key => $value) {
putenv($key.'='.$value);
$_ENV[$key] = $_SERVER[$key] = $value;
}
$app = require $root.'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
if (DB::selectOne('SHOW default_transaction_read_only')->default_transaction_read_only !== 'on') {
throw new LogicException('Database read-only enforcement required.');
}
// Conservation belongs to this inspection. Installation-time conservation remains
// in the immutable installation proof; an operating application may advance its queue.
DB::beginTransaction();
DB::statement('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY');
$frames = static function (): array {
$result = [];
foreach (DB::select("SELECT tablename FROM pg_tables WHERE schemaname='public' ORDER BY tablename") as $table) {
$quoted = '"'.str_replace('"', '""', $table->tablename).'"';
$rows = DB::select('SELECT row_to_json(t)::text AS row FROM '.$quoted.' t ORDER BY row_to_json(t)::text');
$result[$table->tablename] = hash('sha256', json_encode(array_column($rows, 'row'), JSON_THROW_ON_ERROR));
}
return $result;
};
$baseline = $frames();
if (! app(InventoryConversionRuntime::class)->available()) {
throw new LogicException('Candidate unavailable with prospective configuration.');
}
app(ReceiptStockMovement::class)->load();
$converted = DB::table('goods_receipt_items')->whereNotNull('snapshot->quantity_conversion')->count();
if ($frames() !== $baseline) {
throw new LogicException('Inventory inspection changed pilot data.');
}
$preserved = count($baseline);
DB::commit();
if (! hash_equals($sourceHash, hash_file('sha256', $sourcePath))
|| ! hash_equals($publicHash, hash_file('sha256', $root.'/public/build/manifest.json'))) {
throw new LogicException('Concurrent pilot settings or frontend change.');
}
$result = ['date' => gmdate('c'), 'state' => 'passed', 'mode' => $installed ? 'installed_read_only_postcheck' : 'prospective_read_only_preflight',
'database_enforced_read_only' => true, 'candidate_available_with_installed_financial_runtime' => true,
'inventory_manifest_sha256' => InventoryConversionRuntime::MANIFEST_SHA256,
'active_settings_sha256' => $sourceHash, 'public_manifest_sha256' => $publicHash,
'existing_converted_receipt_lines' => $converted, 'active_settings_changed' => false,
'database_tables_preserved_during_readonly_check' => $preserved,
'inventory_component_installed_or_activated' => $installed, 'migrations_or_business_commands' => false];
file_put_contents($root.'/docs/proofs/inventory-conversion-pilot-'.($installed ? 'current-check' : 'preflight').'.json', json_encode($result, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL);
echo json_encode($result, JSON_THROW_ON_ERROR).PHP_EOL;