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

112 lines
6.6 KiB
PHP

<?php
use App\Services\OrderCancellationRuntime;
use Symfony\Component\Process\Process;
require dirname(__DIR__).'/vendor/autoload.php';
$root = dirname(__DIR__);
if (count($argv) !== 2 || ! in_array($argv[1], ['--preview', '--apply-authorized'], true)) {
throw new LogicException('Explicit pilot cancellation preview or installation required.');
}
$proposal = $root.'/docs/proofs/order-cancellation-pilot-proposal.json';
$plan = json_decode(file_get_contents($proposal), true, 32, JSON_THROW_ON_ERROR);
$approval = json_decode(file_get_contents($root.'/docs/proofs/order-cancellation-pilot-authorization.json'), true, 32, JSON_THROW_ON_ERROR);
$resultPath = $root.'/docs/proofs/order-cancellation-pilot-installation.json';
$sourcePath = 'C:/laragon/data/sgf-tempo/secrets/payables-demo-compras.json';
$destination = 'C:/laragon/data/compras/runtime/order-cancellation-52df7927aa268959';
if ($approval['state'] !== 'approved' || ! hash_equals($approval['proposal_sha256'], hash_file('sha256', $proposal))
|| is_file($resultPath) || $plan['target_root'] !== $destination || $plan['manifest_sha256'] !== OrderCancellationRuntime::MANIFEST_SHA256) {
throw new LogicException('Cancellation installation authorization or target mismatch.');
}
$before = file_get_contents($sourcePath);
$settings = json_decode($before, true, 32, JSON_THROW_ON_ERROR);
if (! hash_equals($plan['settings_before_sha256'], hash('sha256', $before))) {
throw new LogicException('Cancellation configuration changed since preparation.');
}
$changes = ['COMPRAS_ORDER_CANCELLATION_ENABLED' => 'true', 'COMPRAS_ORDER_CANCELLATION_ROOT' => $destination];
if ($changes !== $plan['proposed_settings_only']) {
throw new LogicException('Unreviewed cancellation configuration refused.');
}
$candidate = $root.'/'.$plan['candidate_root'];
OrderCancellationRuntime::verifyArtifact($candidate);
$preflight = new Process([PHP_BINARY, 'scripts/check-order-cancellation-pilot.php', '--read-only'], $root, timeout: 30);
$preflight->mustRun();
if (json_decode($preflight->getOutput(), true, 32, JSON_THROW_ON_ERROR)['state'] !== 'passed') {
throw new LogicException('Cancellation read-only preflight failed.');
}
if ($argv[1] === '--preview') {
echo json_encode(['state' => 'preview', 'changed_keys' => array_keys($changes), 'database_writes' => false], JSON_THROW_ON_ERROR).PHP_EOL;
exit;
}
if (file_exists($destination)) {
throw new LogicException('Preserve existing cancellation component directory.');
}
$backup = 'C:/laragon/data/compras/backups/cancellation-activation-'.gmdate('Ymd-His').'-'.bin2hex(random_bytes(6));
if (! mkdir($backup, 0700, true) || file_put_contents($backup.'/settings.before.private.json', $before, LOCK_EX) !== strlen($before)
|| ! hash_equals(hash('sha256', $before), hash_file('sha256', $backup.'/settings.before.private.json'))) {
throw new LogicException('Cancellation configuration backup failed.');
}
$db = new PDO('pgsql:host=127.0.0.1;port=5432;dbname='.$settings['DB_DATABASE'], $settings['DB_USERNAME'], $settings['DB_PASSWORD'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$db->exec('SET default_transaction_read_only=on');
$frames = static function () use ($db): array {
$db->beginTransaction();
try {
$db->exec('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY');
$out = [];
foreach ($db->query("SELECT tablename FROM pg_tables WHERE schemaname='public' ORDER BY tablename")->fetchAll(PDO::FETCH_COLUMN) as $table) {
$quoted = '"'.str_replace('"', '""', $table).'"';
$rows = $db->query('SELECT row_to_json(t)::text FROM '.$quoted.' t ORDER BY row_to_json(t)::text')->fetchAll(PDO::FETCH_COLUMN);
$out[$table] = hash('sha256', json_encode($rows, JSON_THROW_ON_ERROR));
}
$db->commit();
return $out;
} finally {
if ($db->inTransaction()) {
$db->rollBack();
}
}
};
$baseline = $frames();
file_put_contents($backup.'/table-hashes.private.json', json_encode($baseline, JSON_THROW_ON_ERROR));
$envHash = hash_file('sha256', $root.'/.env');
$assetsHash = hash_file('sha256', $root.'/public/build/manifest.json');
if (! mkdir($destination, 0700) || ! mkdir($destination.'/src', 0700)) {
throw new LogicException('Cancellation private destination unavailable.');
}
foreach (['manifest.json', 'src/CommitmentReversal.php'] as $file) {
if (! copy($candidate.'/'.$file, $destination.'/'.$file)) {
throw new LogicException('Cancellation component copy failed before activation.');
}
}
OrderCancellationRuntime::verifyArtifact($destination);
$after = [...$settings, ...$changes];
$encoded = json_encode($after, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR).PHP_EOL;
$temporary = $sourcePath.'.cancellation-'.bin2hex(random_bytes(6)).'.tmp';
try {
if (file_put_contents($temporary, $encoded, LOCK_EX) !== strlen($encoded)
|| ! hash_equals(hash('sha256', $before), hash_file('sha256', $sourcePath)) || ! rename($temporary, $sourcePath)) {
throw new LogicException('Cancellation atomic configuration selection failed.');
}
$check = new Process([PHP_BINARY, 'scripts/check-order-cancellation-pilot.php', '--installed-read-only'], $root, timeout: 30);
$check->mustRun();
if (json_decode($check->getOutput(), true, 32, JSON_THROW_ON_ERROR)['state'] !== 'passed'
|| $frames() !== $baseline || hash_file('sha256', $root.'/.env') !== $envHash
|| hash_file('sha256', $root.'/public/build/manifest.json') !== $assetsHash
|| json_decode(file_get_contents($sourcePath), true, 32, JSON_THROW_ON_ERROR) !== $after) {
throw new LogicException('Cancellation installation conservation failed; inspect without reverting business rows.');
}
$report = ['date' => gmdate('c'), 'state' => 'passed', 'target' => 'http://127.0.0.1:8005',
'authorization' => 'order-cancellation-pilot-authorization.json', 'backup_directory' => $backup, 'component_directory' => $destination,
'manifest_sha256' => OrderCancellationRuntime::MANIFEST_SHA256, 'changed_keys' => array_keys($changes),
'settings_before_sha256' => hash('sha256', $before), 'settings_after_sha256' => hash('sha256', $encoded),
'all_database_tables_preserved' => count($baseline), 'unrelated_settings_financial_receipt_env_and_frontend_preserved' => true,
'policies_grants_migrations_or_business_commands' => false, 'application_complete' => false];
file_put_contents($resultPath, json_encode($report, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL);
echo json_encode($report, JSON_THROW_ON_ERROR).PHP_EOL;
} finally {
if (is_file($temporary)) {
unlink($temporary);
}
}