Add operation to txn meta

This commit is contained in:
Jake Barnby
2025-06-17 22:08:08 -04:00
parent 75469eed7d
commit 910f27016b
2 changed files with 27 additions and 3 deletions
+10
View File
@@ -2527,6 +2527,16 @@ return [
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('operations'),
'type' => Database::VAR_INTEGER,
'size' => 0,
'signed' => false,
'required' => true,
'default' => 0,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('expiresAt'),
'type' => Database::VAR_DATETIME,
+17 -3
View File
@@ -1564,6 +1564,7 @@ App::post('/v1/databases/transactions')
$transaction = $dbForProject->createDocument('transactions', new Document([
'$id' => ID::unique(),
'status' => 'pending',
'operations' => 0,
'expiresAt' => DateTime::addSeconds(new \DateTime(), $ttl),
]));
@@ -1598,11 +1599,19 @@ App::post('/v1/databases/transactions/:transactionId/operations')
->inject('plan')
->action(function (string $transactionId, array $operations, Response $response, Database $dbForProject, array $plan) {
$transaction = $dbForProject->getDocument('transactions', $transactionId);
if ($transaction->isEmpty() || $transaction['status'] !== 'pending') {
if ($transaction->isEmpty() || $transaction->getAttribute('status', '') !== 'pending') {
throw new Exception(Exception::GENERAL_BAD_REQUEST, 'Invalid or nonpending transaction');
}
$maxBatch = $plan['databasesBatchSize'] ?? APP_LIMIT_DATABASE_BATCH;
$existing = $transaction->getAttribute('operations', 0);
if (($existing + \count($operations)) > $maxBatch) {
throw new Exception(
Exception::TRANSACTION_LIMIT_EXCEEDED,
'Transaction already has ' . $existing . ' operations, adding ' . \count($operations) . ' would exceed the maximum of ' . $maxBatch
);
}
$databases = $collections = $staged = [];
foreach ($operations as $operation) {
@@ -1627,7 +1636,12 @@ App::post('/v1/databases/transactions/:transactionId/operations')
]);
}
$dbForProject->createDocuments('transactionLogs', $staged);
$dbForProject->withTransaction(function () use ($dbForProject, $transactionId, $staged, $existing, $operations) {
$dbForProject->createDocuments('transactionLogs', $staged);
$dbForProject->updateDocument('transactions', $transactionId, new Document([
'operations' => $existing + \count($operations),
]));
});
$response
->setStatusCode(Response::STATUS_CODE_CREATED)