Skip to content

Commit 5d52043

Browse files
hegemonicbshaffer
authored andcommitted
More changes to Cloud Spanner budget-transfer samples (GoogleCloudPlatform#902)
1 parent a9337dd commit 5d52043

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

spanner/src/read_write_transaction.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
* Performs a read-write transaction to update two sample records in the
3333
* database.
3434
*
35-
* This will transfer 100,000 from the `MarketingBudget` field for the second
36-
* Album to the first Album. If the `MarketingBudget` is too low, it will
37-
* raise an exception.
35+
* This will transfer 200,000 from the `MarketingBudget` field for the second
36+
* Album to the first Album. If the `MarketingBudget` for the second Album is
37+
* too low, it will raise an exception.
3838
*
3939
* Before running this sample, you will need to run the `update_data` sample
4040
* to populate the fields.
@@ -53,6 +53,8 @@ function read_write_transaction($instanceId, $databaseId)
5353
$database = $instance->database($databaseId);
5454

5555
$database->runTransaction(function (Transaction $t) use ($spanner) {
56+
$transferAmount = 200000;
57+
5658
// Read the second album's budget.
5759
$secondAlbumKey = [2,2];
5860
$secondAlbumKeySet = $spanner->keySet(['keys' => [$secondAlbumKey]]);
@@ -65,10 +67,10 @@ function read_write_transaction($instanceId, $databaseId)
6567

6668
$firstRow = $secondAlbumResult->rows()->current();
6769
$secondAlbumBudget = $firstRow['MarketingBudget'];
68-
if ($secondAlbumBudget < 300000) {
70+
if ($secondAlbumBudget < $transferAmount) {
6971
// Throwing an exception will automatically roll back the transaction.
7072
throw new UnexpectedValueException(
71-
'The second album\'s budget doesn\'t meet the required minimum'
73+
'The second album\'s budget is lower than the transfer amount: ' . $transferAmount
7274
);
7375
}
7476

@@ -86,24 +88,21 @@ function read_write_transaction($instanceId, $databaseId)
8688
$firstAlbumBudget = $firstRow['MarketingBudget'];
8789

8890
// Update the budgets.
89-
$transferAmount = 100000;
90-
if ($firstAlbumBudget >= $transferAmount) {
91-
$secondAlbumBudget += $transferAmount;
92-
$firstAlbumBudget -= $transferAmount;
93-
printf('Setting first album\'s budget to %s and the second album\'s ' .
94-
'budget to %s.' . PHP_EOL, $firstAlbumBudget, $secondAlbumBudget);
91+
$secondAlbumBudget -= $transferAmount;
92+
$firstAlbumBudget += $transferAmount;
93+
printf('Setting first album\'s budget to %s and the second album\'s ' .
94+
'budget to %s.' . PHP_EOL, $firstAlbumBudget, $secondAlbumBudget);
9595

96-
// Update the rows.
97-
$t->updateBatch('Albums', [
98-
['SingerId' => 1, 'AlbumId' => 1, 'MarketingBudget' => $firstAlbumBudget],
99-
['SingerId' => 2, 'AlbumId' => 2, 'MarketingBudget' => $secondAlbumBudget],
100-
]);
96+
// Update the rows.
97+
$t->updateBatch('Albums', [
98+
['SingerId' => 1, 'AlbumId' => 1, 'MarketingBudget' => $firstAlbumBudget],
99+
['SingerId' => 2, 'AlbumId' => 2, 'MarketingBudget' => $secondAlbumBudget],
100+
]);
101101

102-
// Commit the transaction!
103-
$t->commit();
102+
// Commit the transaction!
103+
$t->commit();
104104

105-
print('Transaction complete.' . PHP_EOL);
106-
}
105+
print('Transaction complete.' . PHP_EOL);
107106
});
108107
}
109108
// [END spanner_read_write_transaction]

spanner/src/write_data_with_dml_transaction.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
* Performs a read-write transaction to update two sample records in the
3232
* database.
3333
*
34-
* This will transfer 100,000 from the `MarketingBudget` field for the first
35-
* Album to the second Album. If the `MarketingBudget` is too low, it will
36-
* raise an exception.
34+
* This will transfer 200,000 from the `MarketingBudget` field for the second
35+
* Album to the first Album. If the `MarketingBudget` for the second Album is
36+
* too low, it will raise an exception.
3737
*
3838
* Before running this sample, you will need to run the `update_data` sample
3939
* to populate the fields.
@@ -54,25 +54,26 @@ function write_data_with_dml_transaction($instanceId, $databaseId)
5454
$database->runTransaction(function (Transaction $t) use ($spanner) {
5555
// Transfer marketing budget from one album to another. We do it in a transaction to
5656
// ensure that the transfer is atomic.
57+
$transferAmount = 200000;
58+
5759
$results = $t->execute(
58-
"SELECT MarketingBudget from Albums WHERE SingerId = 1 and AlbumId = 1"
60+
"SELECT MarketingBudget from Albums WHERE SingerId = 2 and AlbumId = 2"
5961
);
6062
$resultsRow = $results->rows()->current();
61-
$album1budget = $resultsRow['MarketingBudget'];
62-
$transferAmount = 100000;
63+
$album2budget = $resultsRow['MarketingBudget'];
6364

6465
// Transaction will only be committed if this condition still holds at the time of
6566
// commit. Otherwise it will be aborted and the callable will be rerun by the
6667
// client library.
67-
if ($album1budget >= $transferAmount) {
68+
if ($album2budget >= $transferAmount) {
6869
$results = $t->execute(
69-
"SELECT MarketingBudget from Albums WHERE SingerId = 2 and AlbumId = 2"
70+
"SELECT MarketingBudget from Albums WHERE SingerId = 1 and AlbumId = 1"
7071
);
7172
$resultsRow = $results->rows()->current();
72-
$album2budget = $resultsRow['MarketingBudget'];
73+
$album1budget = $resultsRow['MarketingBudget'];
7374

74-
$album2budget += $transferAmount;
75-
$album1budget -= $transferAmount;
75+
$album2budget -= $transferAmount;
76+
$album1budget += $transferAmount;
7677

7778
// Update the albums
7879
$t->executeUpdate(

spanner/test/spannerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function testReadWriteTransaction()
158158
{
159159
$this->runCommand('update-data');
160160
$output = $this->runCommand('read-write-transaction');
161-
$this->assertContains('Setting first album\'s budget to 0 and the second album\'s budget to 600000', $output);
161+
$this->assertContains('Setting first album\'s budget to 300000 and the second album\'s budget to 300000', $output);
162162
$this->assertContains('Transaction complete.', $output);
163163
}
164164

0 commit comments

Comments
 (0)