|
| 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); |
0 commit comments