Skip to content

Commit df03e29

Browse files
authored
Merge pull request GoogleCloudPlatform#1290 from larkee/spanner-pitr-samples
feat(spanner): add samples for PITR
2 parents 172930b + 0f282fd commit df03e29

File tree

4 files changed

+109
-4
lines changed

4 files changed

+109
-4
lines changed

spanner/src/create_backup.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ function create_backup($instanceId, $databaseId, $backupId)
4545
$database = $instance->database($databaseId);
4646

4747
$expireTime = new \DateTime('+14 days');
48+
$versionTime = new \DateTime($database->info()['earliestVersionTime']);
4849
$backup = $instance->backup($backupId);
49-
$operation = $backup->create($database->name(), $expireTime);
50+
$operation = $backup->create($database->name(), $expireTime, [
51+
'versionTime' => $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/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: 20 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,6 +97,9 @@ public function testCancelBackup()
8197
$this->assertStringContainsString('Cancel backup operation complete', $output);
8298
}
8399

100+
/**
101+
* @depends testCreateDatabaseWithVersionRetentionPeriod
102+
*/
84103
public function testCreateBackup()
85104
{
86105
$output = $this->runFunctionSnippet('create_backup', [

0 commit comments

Comments
 (0)