Skip to content

Commit d1ec2ef

Browse files
author
Ace Nassri
authored
Merge branch 'master' into php74-yaml
2 parents 5734c9d + dd253fe commit d1ec2ef

File tree

7 files changed

+196
-7
lines changed

7 files changed

+196
-7
lines changed

spanner/src/create_backup.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,25 @@
3131
* Create a backup.
3232
* Example:
3333
* ```
34-
* create_backup($instanceId, $databaseId, $backupId);
34+
* create_backup($instanceId, $databaseId, $backupId, $versionTime);
3535
* ```
3636
*
3737
* @param string $instanceId The Spanner instance ID.
3838
* @param string $databaseId The Spanner database ID.
3939
* @param string $backupId The Spanner backup ID.
40+
* @param string $versionTime The version of the database to backup.
4041
*/
41-
function create_backup($instanceId, $databaseId, $backupId)
42+
function create_backup($instanceId, $databaseId, $backupId, $versionTime)
4243
{
4344
$spanner = new SpannerClient();
4445
$instance = $spanner->instance($instanceId);
4546
$database = $instance->database($databaseId);
4647

4748
$expireTime = new \DateTime('+14 days');
4849
$backup = $instance->backup($backupId);
49-
$operation = $backup->create($database->name(), $expireTime);
50+
$operation = $backup->create($database->name(), $expireTime, [
51+
'versionTime' => new \DateTime($versionTime)
52+
]);
5053

5154
print('Waiting for operation to complete...' . PHP_EOL);
5255
$operation->pollUntilComplete();
@@ -57,7 +60,9 @@ function create_backup($instanceId, $databaseId, $backupId)
5760
if ($ready) {
5861
print('Backup is ready!' . PHP_EOL);
5962
$info = $backup->info();
60-
printf('Backup %s of size %d bytes was created at %s' . PHP_EOL, basename($info['name']), $info['sizeBytes'], $info['createTime']);
63+
printf(
64+
'Backup %s of size %d bytes was created at %s for version of database at %s' . PHP_EOL,
65+
basename($info['name']), $info['sizeBytes'], $info['createTime'], $info['versionTime']);
6166
} else {
6267
print('Backup is not ready!' . PHP_EOL);
6368
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START spanner_create_database_with_version_retention_period]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Creates a database with data retention for Point In Time Restore.
31+
* Example:
32+
* ```
33+
* create_database_with_version_retention_period($instanceId, $databaseId, $retentionPeriod);
34+
* ```
35+
*
36+
* @param string $instanceId The Spanner instance ID.
37+
* @param string $databaseId The Spanner database ID.
38+
* @param string $retentionPeriod The data retention period for the database.
39+
*/
40+
function create_database_with_version_retention_period($instanceId, $databaseId, $retentionPeriod)
41+
{
42+
$spanner = new SpannerClient();
43+
$instance = $spanner->instance($instanceId);
44+
45+
if (!$instance->exists()) {
46+
throw new \LogicException("Instance $instanceId does not exist");
47+
}
48+
49+
$operation = $instance->createDatabase($databaseId, ['statements' => [
50+
"CREATE TABLE Singers (
51+
SingerId INT64 NOT NULL,
52+
FirstName STRING(1024),
53+
LastName STRING(1024),
54+
SingerInfo BYTES(MAX)
55+
) PRIMARY KEY (SingerId)",
56+
"CREATE TABLE Albums (
57+
SingerId INT64 NOT NULL,
58+
AlbumId INT64 NOT NULL,
59+
AlbumTitle STRING(MAX)
60+
) PRIMARY KEY (SingerId, AlbumId),
61+
INTERLEAVE IN PARENT Singers ON DELETE CASCADE",
62+
"ALTER DATABASE `$databaseId` SET OPTIONS (
63+
version_retention_period = '$retentionPeriod')"
64+
]]);
65+
66+
print('Waiting for operation to complete...' . PHP_EOL);
67+
$operation->pollUntilComplete();
68+
69+
$database = $instance->database($databaseId);
70+
$databaseInfo = $database->info();
71+
72+
printf('Database %s created with version retention period %s and earliest version time %s' . PHP_EOL,
73+
$databaseId, $databaseInfo['versionRetentionPeriod'], $databaseInfo['earliestVersionTime']);
74+
}
75+
// [END spanner_create_database_with_version_retention_period]
76+
77+
require_once __DIR__ . '/../../testing/sample_helpers.php';
78+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/src/get_commit_stats.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START spanner_get_commit_stats]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Transaction;
29+
30+
/**
31+
* Creates a database and tables for sample data.
32+
* Example:
33+
* ```
34+
* create_database($instanceId, $databaseId);
35+
* ```
36+
*
37+
* @param string $instanceId The Spanner instance ID.
38+
* @param string $databaseId The Spanner database ID.
39+
*/
40+
function get_commit_stats($instanceId, $databaseId)
41+
{
42+
$spanner = new SpannerClient();
43+
$instance = $spanner->instance($instanceId);
44+
$database = $instance->database($databaseId);
45+
46+
$commitStats = $database->runTransaction(function (Transaction $t) use ($spanner) {
47+
$t->updateBatch('Albums', [
48+
[
49+
'SingerId' => 1,
50+
'AlbumId' => 1,
51+
'MarketingBudget' => 200000,
52+
],
53+
[
54+
'SingerId' => 2,
55+
'AlbumId' => 2,
56+
'MarketingBudget' => 400000,
57+
]
58+
]);
59+
$t->commit(['returnCommitStats' => true]);
60+
return $t->getCommitStats();
61+
});
62+
63+
print('Updated data with ' . $commitStats['mutationCount'] . ' mutations.' . PHP_EOL);
64+
}
65+
// [END spanner_get_commit_stats]
66+
67+
require_once __DIR__ . '/../../testing/sample_helpers.php';
68+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/src/restore_backup.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ function restore_backup($instanceId, $databaseId, $backupId)
5252
$restoreInfo = $database->info()['restoreInfo'];
5353
$sourceDatabase = $restoreInfo['backupInfo']['sourceDatabase'];
5454
$sourceBackup = $restoreInfo['backupInfo']['backup'];
55+
$versionTime = $restoreInfo['backupInfo']['versionTime'];
5556

56-
print("Database $sourceDatabase restored from backup $sourceBackup." . PHP_EOL);
57+
printf(
58+
"Database %s restored from backup %s with version time %s" . PHP_EOL,
59+
$sourceDatabase, $sourceBackup, $versionTime);
5760
}
5861
// [END spanner_restore_backup]
5962

spanner/test/spannerBackupTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class spannerBackupTest extends TestCase
4747
/** @var string databaseId */
4848
protected static $databaseId;
4949

50+
/** @var string retentionPeriod */
51+
protected static $retentionPeriod;
52+
5053
/** @var string restoredDatabaseId */
5154
protected static $restoredDatabaseId;
5255

@@ -66,13 +69,26 @@ public static function setUpBeforeClass(): void
6669
'projectId' => self::$projectId,
6770
]);
6871

72+
self::$retentionPeriod = '7d';
6973
self::$databaseId = 'test-' . time() . rand();
7074
self::$backupId = 'backup-' . self::$databaseId;
7175
self::$restoredDatabaseId = self::$databaseId . '-res';
7276
self::$instance = $spanner->instance(self::$instanceId);
73-
self::$instance->database(self::$databaseId)->create();
7477
}
7578

79+
public function testCreateDatabaseWithVersionRetentionPeriod()
80+
{
81+
$output = $this->runFunctionSnippet('create_database_with_version_retention_period', [
82+
self::$databaseId,
83+
self::$retentionPeriod,
84+
]);
85+
$this->assertStringContainsString(self::$databaseId, $output);
86+
$this->assertStringContainsString(self::$retentionPeriod, $output);
87+
}
88+
89+
/**
90+
* @depends testCreateDatabaseWithVersionRetentionPeriod
91+
*/
7692
public function testCancelBackup()
7793
{
7894
$output = $this->runFunctionSnippet('cancel_backup', [
@@ -81,11 +97,20 @@ public function testCancelBackup()
8197
$this->assertStringContainsString('Cancel backup operation complete', $output);
8298
}
8399

100+
/**
101+
* @depends testCreateDatabaseWithVersionRetentionPeriod
102+
*/
84103
public function testCreateBackup()
85104
{
105+
$database = self::$instance->database(self::$databaseId);
106+
$results = $database->execute("SELECT TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MICROSECOND) as Timestamp");
107+
$row = $results->rows()->current();
108+
$versionTime = $row['Timestamp'];
109+
86110
$output = $this->runFunctionSnippet('create_backup', [
87111
self::$databaseId,
88112
self::$backupId,
113+
$versionTime,
89114
]);
90115
$this->assertStringContainsString(self::$backupId, $output);
91116
}

spanner/test/spannerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,16 @@ public function testUpdateDataWithBatchDML()
511511
$this->assertStringContainsString('Executed 2 SQL statements using Batch DML', $output);
512512
}
513513

514+
/**
515+
* @depends testAddColumn
516+
*/
517+
public function testGetCommitStats()
518+
{
519+
$output = $this->runFunctionSnippet('get_commit_stats');
520+
$this->assertStringContainsString('Updated data with 10 mutations.', $output);
521+
}
522+
523+
514524
/**
515525
* @depends testCreateDatabase
516526
*/

texttospeech/src/synthesize_text_file.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
3939

4040
/** Uncomment and populate these variables in your code */
41-
// $path = 'Path to file to synthesize';
41+
// $path = 'The text file to be synthesized. (e.g., hello.txt)';
4242

4343
// create client object
4444
$client = new TextToSpeechClient();

0 commit comments

Comments
 (0)