Skip to content

Commit 6aadc2f

Browse files
authored
samples: add support for updateDatabase in Cloud Spanner (#2346)
1 parent 832bc4b commit 6aadc2f

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/
302302
| Statement Timeout Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/StatementTimeoutExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/StatementTimeoutExample.java) |
303303
| Tag Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/TagSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/TagSample.java) |
304304
| Tracing Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/TracingSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/TracingSample.java) |
305+
| Update Database Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/UpdateDatabaseSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/UpdateDatabaseSample.java) |
305306
| Update Database With Default Leader Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/UpdateDatabaseWithDefaultLeaderSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/UpdateDatabaseWithDefaultLeaderSample.java) |
306307
| Update Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/UpdateInstanceConfigSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/UpdateInstanceConfigSample.java) |
307308
| Update Json Data Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/UpdateJsonDataSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/UpdateJsonDataSample.java) |
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
// [START spanner_update_database]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.spanner.Database;
22+
import com.google.cloud.spanner.DatabaseAdminClient;
23+
import com.google.cloud.spanner.DatabaseId;
24+
import com.google.cloud.spanner.DatabaseInfo.DatabaseField;
25+
import com.google.cloud.spanner.Spanner;
26+
import com.google.cloud.spanner.SpannerExceptionFactory;
27+
import com.google.cloud.spanner.SpannerOptions;
28+
import com.google.spanner.admin.database.v1.UpdateDatabaseMetadata;
29+
import java.util.concurrent.ExecutionException;
30+
import java.util.concurrent.TimeUnit;
31+
import java.util.concurrent.TimeoutException;
32+
33+
public class UpdateDatabaseSample {
34+
35+
static void updateDatabase() {
36+
// TODO(developer): Replace these variables before running the sample.
37+
final String projectId = "my-project";
38+
final String instanceId = "my-instance";
39+
final String databaseId = "my-database";
40+
updateDatabase(projectId, instanceId, databaseId);
41+
}
42+
43+
static void updateDatabase(String projectId, String instanceId, String databaseId) {
44+
try (Spanner spanner =
45+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
46+
final DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient();
47+
48+
DatabaseId dbId = DatabaseId.of(projectId, instanceId, databaseId);
49+
Database databaseToUpdate =
50+
databaseAdminClient.newDatabaseBuilder(dbId).enableDropProtection().build();
51+
OperationFuture<Database, UpdateDatabaseMetadata> operation =
52+
databaseAdminClient.updateDatabase(databaseToUpdate, DatabaseField.DROP_PROTECTION);
53+
System.out.printf("Waiting for update operation for %s to complete...\n", dbId);
54+
Database updatedDb = operation.get(5, TimeUnit.MINUTES);
55+
System.out.printf("Updated database %s.\n", updatedDb.getId().getName());
56+
} catch (ExecutionException | TimeoutException e) {
57+
// If the operation failed during execution, expose the cause.
58+
throw SpannerExceptionFactory.asSpannerException(e.getCause());
59+
} catch (InterruptedException e) {
60+
// Throw when a thread is waiting, sleeping, or otherwise occupied,
61+
// and the thread is interrupted, either before or during the activity.
62+
throw SpannerExceptionFactory.propagateInterrupt(e);
63+
}
64+
}
65+
}
66+
// [END spanner_update_database]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
import static org.junit.Assert.assertFalse;
20+
import static org.junit.Assert.assertTrue;
21+
22+
import com.google.api.gax.longrunning.OperationFuture;
23+
import com.google.cloud.spanner.DatabaseInfo.DatabaseField;
24+
import com.google.spanner.admin.database.v1.UpdateDatabaseMetadata;
25+
import java.util.Collections;
26+
import java.util.concurrent.TimeUnit;
27+
import org.junit.Test;
28+
29+
public class UpdateDatabaseSampleIT extends SampleTestBase {
30+
31+
@Test
32+
public void testUpdateDatabase() throws Exception {
33+
// Create database
34+
final String databaseId = idGenerator.generateDatabaseId();
35+
databaseAdminClient
36+
.createDatabase(instanceId, databaseId, Collections.emptyList())
37+
.get(5, TimeUnit.MINUTES);
38+
39+
// Runs sample
40+
final String out =
41+
SampleRunner.runSample(
42+
() -> UpdateDatabaseSample.updateDatabase(projectId, instanceId, databaseId));
43+
44+
DatabaseId dbId = DatabaseId.of(projectId, instanceId, databaseId);
45+
assertTrue(
46+
"Expected that database would have been updated. Output received was " + out,
47+
out.contains(String.format("Updated database %s", dbId)));
48+
49+
// Cleanup
50+
Database databaseToUpdate =
51+
databaseAdminClient.newDatabaseBuilder(dbId).disableDropProtection().build();
52+
OperationFuture<Database, UpdateDatabaseMetadata> operation =
53+
databaseAdminClient.updateDatabase(databaseToUpdate, DatabaseField.DROP_PROTECTION);
54+
Database updatedDb = operation.get(5, TimeUnit.MINUTES);
55+
assertFalse(updatedDb.isDropProtectionEnabled());
56+
}
57+
}

0 commit comments

Comments
 (0)