Skip to content

Commit fcfe833

Browse files
authored
feat(spanner): add samples for CMMR (GoogleCloudPlatform#1457)
* feat(spanner): add samples for CMMR * test: add tests for CMMR samples * style: fix lint * fix: use correct option in alter statement * test: address review comments * chore: address review comments Co-authored-by: larkee
1 parent 332aac5 commit fcfe833

8 files changed

+507
-1
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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_default_leader]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Creates a database with a default leader.
31+
* Example:
32+
* ```
33+
* create_database_with_default_leader($instanceId, $databaseId, $defaultLeader);
34+
* ```
35+
*
36+
* @param string $instanceId The Spanner instance ID.
37+
* @param string $databaseId The Spanner database ID.
38+
* @param string $defaultLeader The leader instance configuration used by default.
39+
*/
40+
function create_database_with_default_leader($instanceId, $databaseId, $defaultLeader)
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+
default_leader = '$defaultLeader')"
64+
]]);
65+
66+
print('Waiting for operation to complete...' . PHP_EOL);
67+
$operation->pollUntilComplete();
68+
69+
$database = $instance->database($databaseId);
70+
printf('Created database %s on instance %s with default leader %s' . PHP_EOL,
71+
$databaseId, $instanceId, $database->info()['defaultLeader']);
72+
}
73+
// [END spanner_create_database_with_default_leader]
74+
75+
// The following 2 lines are only needed to run the samples
76+
require_once __DIR__ . '/../../testing/sample_helpers.php';
77+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/src/get_database_ddl.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_database_ddl]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Gets the database DDL statements.
31+
* Example:
32+
* ```
33+
* get_database_ddl($instanceId, $databaseId);
34+
* ```
35+
*
36+
* @param string $instanceId The Spanner instance ID.
37+
* @param string $databaseId The Spanner database ID.
38+
*/
39+
function get_database_ddl($instanceId, $databaseId)
40+
{
41+
$spanner = new SpannerClient();
42+
$instance = $spanner->instance($instanceId);
43+
$database = $instance->database($databaseId);
44+
45+
printf("Retrieved database DDL for $databaseId" . PHP_EOL);
46+
foreach ($database->ddl() as $statement) {
47+
printf("%s" . PHP_EOL, $statement);
48+
}
49+
}
50+
// [END spanner_get_database_ddl]
51+
52+
// The following 2 lines are only needed to run the samples
53+
require_once __DIR__ . '/../../testing/sample_helpers.php';
54+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/src/get_instance_config.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
/**
3+
* Copyright 2021 Google LLC.
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_instance_config]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Gets the leader options for the instance configuration.
31+
*
32+
* @param string $instanceConfig The name of the instance configuration.
33+
*/
34+
function get_instance_config($instanceConfig)
35+
{
36+
$spanner = new SpannerClient();
37+
$config = $spanner->instanceConfiguration($instanceConfig);
38+
printf("Available leader options for instance config %s: %s" . PHP_EOL,
39+
$instanceConfig, $config->info()['leaderOptions']
40+
);
41+
}
42+
// [END spanner_get_instance_config]
43+
44+
// The following 2 lines are only needed to run the samples
45+
require_once __DIR__ . '/../../testing/sample_helpers.php';
46+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/src/list_databases.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
/**
3+
* Copyright 2021 Google LLC.
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_list_databases]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Lists the databases and their leader options.
31+
* Example:
32+
* ```
33+
* list_databases($instanceId);
34+
* ```
35+
*
36+
* @param $instanceId The Spanner instance ID.
37+
*/
38+
function list_databases($instanceId)
39+
{
40+
$spanner = new SpannerClient();
41+
$instance = $spanner->instance($instanceId);
42+
printf("Databases for %s" . PHP_EOL, $instance->name());
43+
foreach ($instance->databases() as $database) {
44+
if (isset($database->info()['defaultLeader'])) {
45+
printf("\t%s (default leader = %s)" . PHP_EOL,
46+
$database->info()['name'], $database->info()['defaultLeader']);
47+
} else {
48+
printf("\t%s" . PHP_EOL, $database->info()['name']);
49+
}
50+
}
51+
}
52+
// [END spanner_list_databases]
53+
54+
// The following 2 lines are only needed to run the samples
55+
require_once __DIR__ . '/../../testing/sample_helpers.php';
56+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/src/list_instance_configs.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
/**
3+
* Copyright 2021 Google LLC.
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_list_instance_configs]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Lists the available instance configurations.
31+
* Example:
32+
* ```
33+
* list_instance_configs();
34+
* ```
35+
*/
36+
function list_instance_configs()
37+
{
38+
$spanner = new SpannerClient();
39+
foreach ($spanner->instanceConfigurations() as $config) {
40+
printf("Available leader options for instance config %s: %s" . PHP_EOL,
41+
$config->info()['displayName'], $config->info()['leaderOptions']
42+
);
43+
}
44+
}
45+
// [END spanner_list_instance_configs]
46+
47+
// The following 2 lines are only needed to run the samples
48+
require_once __DIR__ . '/../../testing/sample_helpers.php';
49+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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_query_information_schema_database_options]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Queries the default leader of a database.
31+
* Example:
32+
* ```
33+
* query_information_schema_database_options($instanceId, $databaseId);
34+
* ```
35+
*
36+
* @param string $instanceId The Spanner instance ID.
37+
* @param string $databaseId The Spanner database ID.
38+
*/
39+
function query_information_schema_database_options($instanceId, $databaseId)
40+
{
41+
$spanner = new SpannerClient();
42+
$instance = $spanner->instance($instanceId);
43+
$database = $instance->database($databaseId);
44+
45+
$results = $database->execute(
46+
"SELECT s.OPTION_NAME, s.OPTION_VALUE
47+
FROM INFORMATION_SCHEMA.DATABASE_OPTIONS s
48+
WHERE s.OPTION_NAME = 'default_leader'"
49+
);
50+
51+
foreach ($results as $row) {
52+
$optionName = $row['OPTION_NAME'];
53+
$optionValue = $row['OPTION_VALUE'];
54+
printf("The $optionName for $databaseId is $optionValue" . PHP_EOL);
55+
}
56+
if (!$results->stats()['rowCountExact']) {
57+
printf("Database $databaseId does not have a value for option 'default_leader'");
58+
}
59+
}
60+
// [END spanner_query_information_schema_database_options]
61+
62+
// The following 2 lines are only needed to run the samples
63+
require_once __DIR__ . '/../../testing/sample_helpers.php';
64+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)