From feb0b95df59d7fac957fa67a1583715446a2847f Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Fri, 20 Sep 2024 14:54:05 +0900 Subject: [PATCH] feat: allow commit options to be set --- src/Concerns/ManagesTransactions.php | 24 ++++++++++++++++++++- tests/TransactionTest.php | 32 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/Concerns/ManagesTransactions.php b/src/Concerns/ManagesTransactions.php index bffbe3a..5388351 100644 --- a/src/Concerns/ManagesTransactions.php +++ b/src/Concerns/ManagesTransactions.php @@ -39,6 +39,11 @@ trait ManagesTransactions protected ?int $maxAttempts = null; + /** + * @var array|null $commitOptions + */ + protected ?array $commitOptions = null; + /** * @inheritDoc * @template T @@ -158,7 +163,7 @@ protected function performSpannerCommit(): void { if ($this->transactions === 1 && $this->currentTransaction !== null) { $this->fireConnectionEvent('committing'); - $this->currentTransaction->commit(); + $this->currentTransaction->commit($this->getCommitOptions()); } [$levelBeingCommitted, $this->transactions] = [ @@ -263,4 +268,21 @@ public function setDefaultMaxTransactionAttempts(int $attempts): static $this->maxAttempts = $attempts; return $this; } + + /** + * @return array + */ + public function getCommitOptions(): array + { + return $this->commitOptions ??= $this->getConfig('commit') ?? []; + } + + /** + * @param array $options + * @return void + */ + public function setCommitOptions(array $options): void + { + $this->commitOptions = $options; + } } diff --git a/tests/TransactionTest.php b/tests/TransactionTest.php index deefc49..489dd3e 100644 --- a/tests/TransactionTest.php +++ b/tests/TransactionTest.php @@ -90,6 +90,38 @@ public function testCommit(): void Event::assertDispatchedTimes(TransactionRolledBack::class, 0); } + public function test_commit_with_options(): void + { + $conn = $this->getDefaultConnection(); + /** @var Transaction $tx */ + $tx = $conn->transaction(function (Connection $conn) { + return $conn->getCurrentTransaction(); + }); + $this->assertNotNull($tx); + dump($tx->getCommitStats()); + $this->assertSame([], $tx->getCommitStats()); + $this->assertSame([], $conn->getCommitOptions()); + + $newOptions = ['returnCommitStats' => true]; + $conn->setCommitOptions($newOptions); + $this->assertSame($newOptions, $conn->getCommitOptions()); + + // True test for commit with options only works on the real Spanner. + if (getenv('SPANNER_EMULATOR_HOST')) { + $this->markTestSkipped( + 'Cannot fully verify commit options on emulator. ' . + 'Feature request for emulator: https://github.com/GoogleCloudPlatform/cloud-spanner-emulator/issues/184', + ); + } + + /** @var Transaction $tx */ + $tx = $conn->transaction(function (Connection $conn) { + $conn->table(self::TABLE_NAME_USER)->insert(['userId' => $this->generateUuid(), 'name' => 'test']); + return $conn->getCurrentTransaction(); + }); + $this->assertSame(['mutationCount' => 2], $tx->getCommitStats()); + } + public function testRollbackBeforeCommit(): void { Event::fake();