Skip to content

Commit 4529a9f

Browse files
committed
fix: Timestamp bound queries were not applied when in transaction (#213)
fix/staleness-read-on-tx # Conflicts: # CHANGELOG.md # tests/ConnectionTest.php
1 parent 8ab4279 commit 4529a9f

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
# v7.4.2 (2024-06-03)
2+
3+
Fixed
4+
- Timestamp bound queries were not applied when in transaction (#213)
5+
16
# v7.4.1 (2024-05-21)
27

38
Fixed
49
- authCache needs namespace for each connection (#210)
510

6-
711
# v7.4.0 (2024-05-13)
812

913
Added

src/Connection.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,17 @@ protected function executeQuery(string $query, array $bindings, array $options):
578578
$options['requestOptions']['requestTag'] = $tag;
579579
}
580580

581-
if ($transaction = $this->getCurrentTransaction()) {
581+
$forceReadOnlyTransaction =
582+
($options['exactStaleness'] ?? false) ||
583+
($options['maxStaleness'] ?? false) ||
584+
($options['minReadTimestamp'] ?? false) ||
585+
($options['readTimestamp'] ?? false) ||
586+
($options['strong'] ?? false);
587+
588+
if (!$forceReadOnlyTransaction && $transaction = $this->getCurrentTransaction()) {
582589
return $transaction->execute($query, $options)->rows();
583590
}
591+
584592
return $this->getSpannerDatabase()->execute($query, $options)->rows();
585593
}
586594

tests/ConnectionTest.php

+30-5
Original file line numberDiff line numberDiff line change
@@ -478,34 +478,59 @@ public function test_stale_reads(): void
478478
$this->assertNotEmpty($timestamp);
479479

480480
$timestampBound = new StrongRead();
481-
$rows = $conn->selectWithOptions("SELECT * FROM ${tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
481+
$rows = $conn->selectWithOptions("SELECT * FROM {$tableName} WHERE userId = ?", [$uuid], $timestampBound->transactionOptions());
482482
$this->assertCount(1, $rows);
483483
$this->assertSame($uuid, $rows[0]['userId']);
484484
$this->assertSame('first', $rows[0]['name']);
485485

486486
$oldDatetime = Carbon::instance($timestamp->get())->subSecond();
487487

488488
$timestampBound = new ReadTimestamp($oldDatetime);
489-
$rows = $conn->selectWithOptions("SELECT * FROM ${tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
489+
$rows = $conn->selectWithOptions("SELECT * FROM {$tableName} WHERE userId = ?", [$uuid], $timestampBound->transactionOptions());
490490
$this->assertEmpty($rows);
491491

492492
$timestampBound = new ExactStaleness(10);
493-
$rows = $conn->selectWithOptions("SELECT * FROM ${tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
493+
$rows = $conn->selectWithOptions("SELECT * FROM {$tableName} WHERE userId = ?", [$uuid], $timestampBound->transactionOptions());
494494
$this->assertEmpty($rows);
495495

496496
$timestampBound = new MaxStaleness(10);
497-
$rows = $conn->selectWithOptions("SELECT * FROM ${tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
497+
$rows = $conn->selectWithOptions("SELECT * FROM {$tableName} WHERE userId = ?", [$uuid], $timestampBound->transactionOptions());
498498
$this->assertCount(1, $rows);
499499
$this->assertSame($uuid, $rows[0]['userId']);
500500
$this->assertSame('first', $rows[0]['name']);
501501

502502
$timestampBound = new MinReadTimestamp($oldDatetime);
503-
$rows = $conn->selectWithOptions("SELECT * FROM ${tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
503+
$rows = $conn->selectWithOptions("SELECT * FROM {$tableName} WHERE userId = ?", [$uuid], $timestampBound->transactionOptions());
504504
$this->assertCount(1, $rows);
505505
$this->assertSame($uuid, $rows[0]['userId']);
506506
$this->assertSame('first', $rows[0]['name']);
507507
}
508508

509+
public function test_stale_reads_in_transaction(): void
510+
{
511+
$conn = $this->getDefaultConnection();
512+
$tableName = self::TABLE_NAME_USER;
513+
$uuid = $this->generateUuid();
514+
515+
$conn->transaction(function(Connection $conn) use ($tableName, $uuid) {
516+
$conn->insert("INSERT INTO {$tableName} (`userId`, `name`) VALUES ('{$uuid}', 'first')");
517+
518+
$oldDatetime = now()->subSecond();
519+
520+
$bounds = [
521+
new StrongRead(),
522+
new ExactStaleness(10),
523+
new MaxStaleness(10),
524+
new ReadTimestamp($oldDatetime),
525+
new MinReadTimestamp($oldDatetime),
526+
];
527+
foreach ($bounds as $timestampBound) {
528+
$rows = $conn->selectWithOptions("SELECT * FROM {$tableName} WHERE userId = ?", [$uuid], $timestampBound->transactionOptions());
529+
$this->assertEmpty($rows);
530+
}
531+
});
532+
}
533+
509534
public function testEventListenOrder(): void
510535
{
511536
$receivedEventClasses = [];

0 commit comments

Comments
 (0)