Skip to content

Commit 853e06f

Browse files
committed
feat(spanner): add samples for PITR
1 parent b28a3aa commit 853e06f

File tree

4 files changed

+105
-3
lines changed

4 files changed

+105
-3
lines changed

spanner/src/create_backup.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ function create_backup($instanceId, $databaseId, $backupId)
4646

4747
$expireTime = new \DateTime('+14 days');
4848
$backup = $instance->backup($backupId);
49-
$operation = $backup->create($database->name(), $expireTime);
49+
$operation = $backup->create($database->name(), $expireTime, [
50+
'versionTime' => $database->info()['earliestVersionTime']
51+
]);
5052

5153
print('Waiting for operation to complete...' . PHP_EOL);
5254
$operation->pollUntilComplete();
@@ -57,7 +59,9 @@ function create_backup($instanceId, $databaseId, $backupId)
5759
if ($ready) {
5860
print('Backup is ready!' . PHP_EOL);
5961
$info = $backup->info();
60-
printf('Backup %s of size %d bytes was created at %s' . PHP_EOL, basename($info['name']), $info['sizeBytes'], $info['createTime']);
62+
printf(
63+
'Backup %s of size %d bytes was created at %s for version of database as %s' . PHP_EOL,
64+
basename($info['name']), $info['sizeBytes'], $info['createTime'], $info['versionTime']);
6165
} else {
6266
print('Backup is not ready!' . PHP_EOL);
6367
}
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);
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: 17 additions & 0 deletions
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,6 +69,7 @@ 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';
@@ -81,6 +85,19 @@ public function testCancelBackup()
8185
$this->assertStringContainsString('Cancel backup operation complete', $output);
8286
}
8387

88+
public function testCreateDatabaseWithVersionRetentionPeriod()
89+
{
90+
$output = $this->runFunctionSnippet('create_database_with_version_retention_period', [
91+
self::$databaseId,
92+
self::$retentionPeriod,
93+
]);
94+
$this->assertStringContainsString(self::$databaseId, $output);
95+
$this->assertStringContainsString(self::$retentionPeriod, $output);
96+
}
97+
98+
/**
99+
* @depends testCreateDatabaseWithVersionRetentionPeriod
100+
*/
84101
public function testCreateBackup()
85102
{
86103
$output = $this->runFunctionSnippet('create_backup', [

0 commit comments

Comments
 (0)