Skip to content

Commit e667708

Browse files
hegemonicbshaffer
authored andcommitted
Fix Cloud Spanner budget-transfer samples (GoogleCloudPlatform#895)
1 parent f4b89b1 commit e667708

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

spanner/src/read_write_transaction.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* Performs a read-write transaction to update two sample records in the
3333
* database.
3434
*
35-
* This will transfer 200,000 from the `MarketingBudget` field for the second
35+
* This will transfer 100,000 from the `MarketingBudget` field for the second
3636
* Album to the first Album. If the `MarketingBudget` is too low, it will
3737
* raise an exception.
3838
*
@@ -68,7 +68,7 @@ function read_write_transaction($instanceId, $databaseId)
6868
if ($secondAlbumBudget < 300000) {
6969
// Throwing an exception will automatically roll back the transaction.
7070
throw new UnexpectedValueException(
71-
'The second album doesn\'t have enough funds to transfer'
71+
'The second album\'s budget doesn\'t meet the required minimum'
7272
);
7373
}
7474

@@ -86,22 +86,24 @@ function read_write_transaction($instanceId, $databaseId)
8686
$firstAlbumBudget = $firstRow['MarketingBudget'];
8787

8888
// Update the budgets.
89-
$transferAmmount = 200000;
90-
$secondAlbumBudget -= $transferAmmount;
91-
$firstAlbumBudget += $transferAmmount;
92-
printf('Setting first album\'s budget to %s and the second album\'s ' .
93-
'budget to %s.' . PHP_EOL, $firstAlbumBudget, $secondAlbumBudget);
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);
9495

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

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

105-
print('Transaction complete.' . PHP_EOL);
105+
print('Transaction complete.' . PHP_EOL);
106+
}
107+
});
106108
}
107109
// [END spanner_read_write_transaction]

spanner/src/write_data_with_dml_transaction.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
* Performs a read-write transaction to update two sample records in the
3232
* database.
3333
*
34-
* This will transfer 200,000 from the `MarketingBudget` field for the first
34+
* This will transfer 100,000 from the `MarketingBudget` field for the first
3535
* Album to the second Album. If the `MarketingBudget` is too low, it will
3636
* raise an exception.
3737
*
3838
* Before running this sample, you will need to run the `update_data` sample
3939
* to populate the fields.
4040
* Example:
4141
* ```
42-
* read_write_transaction($instanceId, $databaseId);
42+
* write_data_with_dml_transaction($instanceId, $databaseId);
4343
* ```
4444
*
4545
* @param string $instanceId The Spanner instance ID.
@@ -55,20 +55,22 @@ function write_data_with_dml_transaction($instanceId, $databaseId)
5555
// Transfer marketing budget from one album to another. We do it in a transaction to
5656
// ensure that the transfer is atomic.
5757
$results = $t->execute(
58-
"SELECT MarketingBudget from Albums WHERE SingerId = 1 and AlbumId = 1");
58+
"SELECT MarketingBudget from Albums WHERE SingerId = 1 and AlbumId = 1"
59+
);
5960
$resultsRow = $results->rows()->current();
6061
$album1budget = $resultsRow['MarketingBudget'];
62+
$transferAmount = 100000;
6163

6264
// Transaction will only be committed if this condition still holds at the time of
6365
// commit. Otherwise it will be aborted and the callable will be rerun by the
6466
// client library.
65-
if ($album1budget > 300000) {
67+
if ($album1budget >= $transferAmount) {
6668
$results = $t->execute(
67-
"SELECT MarketingBudget from Albums WHERE SingerId = 2 and AlbumId = 2");
69+
"SELECT MarketingBudget from Albums WHERE SingerId = 2 and AlbumId = 2"
70+
);
6871
$resultsRow = $results->rows()->current();
6972
$album2budget = $resultsRow['MarketingBudget'];
7073

71-
$transferAmount = 200000;
7274
$album2budget += $transferAmount;
7375
$album1budget -= $transferAmount;
7476

spanner/test/spannerTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function setUpBeforeClass()
4848
self::markTestSkipped('GOOGLE_PROJECT_ID must be set.');
4949
}
5050
if (!$instanceId = getenv('GOOGLE_SPANNER_INSTANCE_ID')) {
51-
self::markTestSkipped('GOOGLE_PROJECT_ID must be set.');
51+
self::markTestSkipped('GOOGLE_SPANNER_INSTANCE_ID must be set.');
5252
}
5353

5454
$spanner = new SpannerClient([
@@ -156,8 +156,9 @@ public function testQueryDataWithNewColumn()
156156
*/
157157
public function testReadWriteTransaction()
158158
{
159+
$this->runCommand('update-data');
159160
$output = $this->runCommand('read-write-transaction');
160-
$this->assertContains('Setting first album\'s budget to 300000 and the second album\'s budget to 300000', $output);
161+
$this->assertContains('Setting first album\'s budget to 0 and the second album\'s budget to 600000', $output);
161162
$this->assertContains('Transaction complete.', $output);
162163
}
163164

0 commit comments

Comments
 (0)