Skip to content

Commit

Permalink
fix: Timestamp bound queries were not applied when in transaction (#213)
Browse files Browse the repository at this point in the history
fix/staleness-read-on-tx
# Conflicts:
#	CHANGELOG.md
#	tests/ConnectionTest.php
  • Loading branch information
taka-oyama committed Jun 3, 2024
1 parent 8ab4279 commit 4529a9f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# v7.4.2 (2024-06-03)

Fixed
- Timestamp bound queries were not applied when in transaction (#213)

# v7.4.1 (2024-05-21)

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


# v7.4.0 (2024-05-13)

Added
Expand Down
10 changes: 9 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,17 @@ protected function executeQuery(string $query, array $bindings, array $options):
$options['requestOptions']['requestTag'] = $tag;
}

if ($transaction = $this->getCurrentTransaction()) {
$forceReadOnlyTransaction =
($options['exactStaleness'] ?? false) ||
($options['maxStaleness'] ?? false) ||
($options['minReadTimestamp'] ?? false) ||
($options['readTimestamp'] ?? false) ||
($options['strong'] ?? false);

if (!$forceReadOnlyTransaction && $transaction = $this->getCurrentTransaction()) {
return $transaction->execute($query, $options)->rows();
}

return $this->getSpannerDatabase()->execute($query, $options)->rows();
}

Expand Down
35 changes: 30 additions & 5 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,34 +478,59 @@ public function test_stale_reads(): void
$this->assertNotEmpty($timestamp);

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

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

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

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

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

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

public function test_stale_reads_in_transaction(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_USER;
$uuid = $this->generateUuid();

$conn->transaction(function(Connection $conn) use ($tableName, $uuid) {
$conn->insert("INSERT INTO {$tableName} (`userId`, `name`) VALUES ('{$uuid}', 'first')");

$oldDatetime = now()->subSecond();

$bounds = [
new StrongRead(),
new ExactStaleness(10),
new MaxStaleness(10),
new ReadTimestamp($oldDatetime),
new MinReadTimestamp($oldDatetime),
];
foreach ($bounds as $timestampBound) {
$rows = $conn->selectWithOptions("SELECT * FROM {$tableName} WHERE userId = ?", [$uuid], $timestampBound->transactionOptions());
$this->assertEmpty($rows);
}
});
}

public function testEventListenOrder(): void
{
$receivedEventClasses = [];
Expand Down

0 comments on commit 4529a9f

Please sign in to comment.