Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [Spanner] support excludeTxnFromChangeStreams option #7749

Merged
merged 10 commits into from
Nov 6, 2024
Prev Previous commit
Next Next commit
add test for Operation::transaction
bshaffer committed Oct 16, 2024
commit c3f94371c23a304c2be90827f46cacf9c3a7d035
4 changes: 3 additions & 1 deletion Spanner/src/Connection/Grpc.php
Original file line number Diff line number Diff line change
@@ -1105,7 +1105,9 @@ public function beginTransaction(array $args)

// NOTE: if set for read-only actions, will throw exception
if (isset($transactionOptions['excludeTxnFromChangeStreams'])) {
$options->setExcludeTxnFromChangeStreams($args['excludeTxnFromChangeStreams']);
$options->setExcludeTxnFromChangeStreams(
$transactionOptions['excludeTxnFromChangeStreams']
);
}

$requestOptions = $this->pluck('requestOptions', $args, false) ?: [];
1 change: 0 additions & 1 deletion Spanner/src/Operation.php
Original file line number Diff line number Diff line change
@@ -270,7 +270,6 @@ public function executeUpdate(
iterator_to_array($res->rows());

$stats = $res->stats();

if (!$stats) {
throw new InvalidArgumentException(
'Partitioned DML response missing stats, possible due to non-DML statement as input.'
16 changes: 6 additions & 10 deletions Spanner/tests/Unit/DatabaseTest.php
Original file line number Diff line number Diff line change
@@ -2021,16 +2021,12 @@ public function testRunTransactionWithExcludeTxnFromChangeStreams()
$gapic = $this->prophesize(SpannerClient::class);
$gapic->createSession(Argument::cetera())->shouldBeCalled()->willReturn($session);
$gapic->deleteSession(Argument::cetera())->shouldBeCalled();
$gapic->executeStreamingSql(
$sessName,
$sql,
Argument::that(function (array $options) {
$this->assertArrayHasKey('transaction', $options);
$this->assertNotNull($transactionOptions = $options['transaction']->getBegin());
$this->assertTrue($transactionOptions->getExcludeTxnFromChangeStreams());
return true;
})
)
$gapic->executeStreamingSql($sessName, $sql, Argument::that(function (array $options) {
$this->assertArrayHasKey('transaction', $options);
$this->assertNotNull($transactionOptions = $options['transaction']->getBegin());
$this->assertTrue($transactionOptions->getExcludeTxnFromChangeStreams());
return true;
}))
->shouldBeCalledOnce()
->willReturn($stream->reveal());

30 changes: 30 additions & 0 deletions Spanner/tests/Unit/OperationTest.php
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
use Google\Cloud\Spanner\Admin\Database\V1\DatabaseAdminClient;
use Google\Cloud\Spanner\Batch\QueryPartition;
use Google\Cloud\Spanner\Batch\ReadPartition;
use Google\Cloud\Spanner\Connection\Grpc;
use Google\Cloud\Spanner\Database;
use Google\Cloud\Spanner\Duration;
use Google\Cloud\Spanner\KeyRange;
@@ -35,6 +36,9 @@
use Google\Cloud\Spanner\Timestamp;
use Google\Cloud\Spanner\Transaction;
use Google\Cloud\Spanner\V1\CommitResponse;
use Google\Cloud\Spanner\V1\SpannerClient;
use Google\Cloud\Spanner\V1\Transaction as TransactionProto;
use Google\Cloud\Spanner\V1\TransactionOptions;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
@@ -354,6 +358,32 @@ public function testTransactionNoTag()
$this->assertEquals(self::TRANSACTION, $t->id());
}

public function testTransactionWithExcludeTxnFromChangeStreams()
{
$gapic = $this->prophesize(SpannerClient::class);
$gapic->beginTransaction(
self::SESSION,
Argument::that(function (TransactionOptions $options) {
$this->assertTrue($options->getExcludeTxnFromChangeStreams());
return true;
}),
Argument::type('array')
)
->shouldBeCalled()
->willReturn(new TransactionProto(['id' => 'foo']));

$operation = new Operation(
new Grpc(['gapicSpannerClient' => $gapic->reveal()]),
true
);

$transaction = $operation->transaction($this->session, [
'transactionOptions' => ['excludeTxnFromChangeStreams' => true]
]);

$this->assertEquals('foo', $transaction->id());
}

public function testSnapshot()
{
$this->connection->beginTransaction(Argument::allOf(