@@ -19,6 +19,7 @@ if (! $app->environment('testing') || $db->getDriverName() !== 'pgsql' || $db->g
|
||||
}
|
||||
require_once __DIR__.'/../Support/ReceiptFixture.php';
|
||||
require_once __DIR__.'/../Support/BudgetDistributionSchema.php';
|
||||
$orderParts = ($argv[2] ?? '') === 'order-parts';
|
||||
$f = receiptFixture();
|
||||
$author = User::factory()->create(['tenant_id' => $f['tenant']->id, 'purchasing_role' => PurchasingRole::Finance]);
|
||||
$reviewer = User::factory()->create(['tenant_id' => $f['tenant']->id, 'purchasing_role' => PurchasingRole::Admin]);
|
||||
@@ -63,17 +64,23 @@ $preDump = $directory.'/before.dump';
|
||||
$postDump = $directory.'/after.dump';
|
||||
$pg('pg_dump', ['--format=custom', '--file='.$preDump, '--dbname='.$database]);
|
||||
budgetDistributionFixtureSchema();
|
||||
if ($orderParts) {
|
||||
(require __DIR__.'/../../database/qa-migrations/2026_11_22_000007_create_budget_order_parts.php')->up();
|
||||
}
|
||||
if ($fingerprint($db, $columns) !== $before || app(CommonBudget::class)->for($author)->balance($budget) !== $balanceBefore
|
||||
|| $db->table('budget_distributions')->exists() || $db->table('requisitions')->whereNotNull('budget_distribution_preparation')->exists()) {
|
||||
throw new RuntimeException('Optional schema changed pre-existing data or native balances.');
|
||||
}
|
||||
$prepare = new Process([PHP_BINARY, __DIR__.'/budget-distribution-worker.php', $database, 'prepare'], dirname(__DIR__, 2), timeout: 30);
|
||||
$prepare = new Process([PHP_BINARY, __DIR__.($orderParts ? '/budget-order-parts-worker.php' : '/budget-distribution-worker.php'), $database, 'prepare'], dirname(__DIR__, 2), timeout: 30);
|
||||
$prepare->mustRun();
|
||||
$input = json_decode($prepare->getOutput(), true, 32, JSON_THROW_ON_ERROR);
|
||||
$actor = User::query()->findOrFail($input['actor']);
|
||||
auth()->guard()->setUser($actor);
|
||||
$engine = app(CommonBudget::class)->for($actor);
|
||||
$engine->amendDistribution($input['origin'], $input['plan'], $input['hash'], 'Explicit synthetic amendment preserved in backup.', $input['operation']);
|
||||
$apply = static fn ($engine) => $orderParts
|
||||
? $engine->commitOrderParts($input['order'], $input['sources'], $input['operation'])
|
||||
: $engine->amendDistribution($input['origin'], $input['plan'], $input['hash'], 'Explicit synthetic amendment preserved in backup.', $input['operation']);
|
||||
$apply($engine);
|
||||
$afterColumns = [];
|
||||
foreach (array_column($db->select("select tablename from pg_tables where schemaname='public' order by tablename"), 'tablename') as $table) {
|
||||
$afterColumns[$table] = $db->getSchemaBuilder()->getColumnListing($table);
|
||||
@@ -100,8 +107,7 @@ try {
|
||||
DB::setDefaultConnection('distribution_restore');
|
||||
$restoredActor = User::query()->findOrFail($input['actor']);
|
||||
auth()->guard()->setUser($restoredActor);
|
||||
$again = app(CommonBudget::class)->for($restoredActor)->amendDistribution($input['origin'], $input['plan'], $input['hash'],
|
||||
'Explicit synthetic amendment preserved in backup.', $input['operation']);
|
||||
$again = $apply(app(CommonBudget::class)->for($restoredActor));
|
||||
if (! $again['replayed'] || $fingerprint($restored, $afterColumns) !== $after) {
|
||||
throw new RuntimeException('Recovered command was not replayed without new effects.');
|
||||
}
|
||||
@@ -122,7 +128,7 @@ try {
|
||||
if (app(CommonBudget::class)->for(auth()->user())->balance($budget) !== $balanceBefore) {
|
||||
throw new RuntimeException('Pre-extension recovery changed the native balance.');
|
||||
}
|
||||
echo json_encode(['previous_tables_preserved' => count($before), 'optional_tables' => 4, 'existing_balances_preserved' => true,
|
||||
echo json_encode(['previous_tables_preserved' => count($before), 'optional_tables' => $orderParts ? 8 : 4, 'existing_balances_preserved' => true,
|
||||
'post_extension_restore_all_tables_equal' => true, 'recovered_operation_replay_no_effects' => true, 'pre_extension_schema_data_balance_restored' => true,
|
||||
'before_dump_sha256' => hash_file('sha256', $preDump), 'after_dump_sha256' => hash_file('sha256', $postDump)], JSON_THROW_ON_ERROR).PHP_EOL;
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user