-
Notifications
You must be signed in to change notification settings - Fork 132
samples: add support for updateDatabase in Cloud Spanner #2346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gcf-merge-on-green
merged 12 commits into
googleapis:main
from
aayushimalik:drop-database-protection-samples
May 16, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1bd0aa9
feat: drop database protection sample and sample test
aayushimalik fb18534
formatting fixes
aayushimalik e82e47a
Reviewer-suggested changes.
aayushimalik fd17fec
Reviewer-suggested changes.
aayushimalik f0ad7dc
Rename database field `ENABLE_DROP_PROTECTION` to `DROP_PROTECTION`.
aayushimalik 619e75f
Reviewer suggested changes.
aayushimalik ff28cd6
Reviewer suggested changes.
aayushimalik 015c49a
Remove unnecessary comment.
aayushimalik 8cd0247
Merge branch 'googleapis:main' into drop-database-protection-samples
rajatbhatta 3d73056
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] d8325f6
Merge branch 'googleapis:main' into drop-database-protection-samples
rajatbhatta 303c710
add missing imports and linting fixes
rajatbhatta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
samples/snippets/src/main/java/com/example/spanner/UpdateDatabaseSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_update_database] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.spanner.Database; | ||
import com.google.cloud.spanner.DatabaseAdminClient; | ||
import com.google.cloud.spanner.DatabaseId; | ||
import com.google.cloud.spanner.DatabaseInfo.DatabaseField; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerExceptionFactory; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import com.google.spanner.admin.database.v1.UpdateDatabaseMetadata; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class UpdateDatabaseSample { | ||
|
||
static void updateDatabase() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
final String projectId = "my-project"; | ||
final String instanceId = "my-instance"; | ||
final String databaseId = "my-database"; | ||
updateDatabase(projectId, instanceId, databaseId); | ||
} | ||
|
||
static void updateDatabase(String projectId, String instanceId, String databaseId) { | ||
try (Spanner spanner = | ||
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
final DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient(); | ||
|
||
DatabaseId dbId = DatabaseId.of(projectId, instanceId, databaseId); | ||
Database databaseToUpdate = | ||
databaseAdminClient.newDatabaseBuilder(dbId).enableDropProtection().build(); | ||
OperationFuture<Database, UpdateDatabaseMetadata> operation = | ||
databaseAdminClient.updateDatabase(databaseToUpdate, DatabaseField.DROP_PROTECTION); | ||
System.out.printf("Waiting for update operation for %s to complete...\n", dbId); | ||
Database updatedDb = operation.get(5, TimeUnit.MINUTES); | ||
System.out.printf("Updated database %s.\n", updatedDb.getId().getName()); | ||
rajatbhatta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (ExecutionException | TimeoutException e) { | ||
// If the operation failed during execution, expose the cause. | ||
throw SpannerExceptionFactory.asSpannerException(e.getCause()); | ||
} catch (InterruptedException e) { | ||
// Throw when a thread is waiting, sleeping, or otherwise occupied, | ||
// and the thread is interrupted, either before or during the activity. | ||
throw SpannerExceptionFactory.propagateInterrupt(e); | ||
} | ||
} | ||
} | ||
// [END spanner_update_database] |
57 changes: 57 additions & 0 deletions
57
samples/snippets/src/test/java/com/example/spanner/UpdateDatabaseSampleIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.spanner.DatabaseInfo.DatabaseField; | ||
import com.google.spanner.admin.database.v1.UpdateDatabaseMetadata; | ||
import java.util.Collections; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Test; | ||
|
||
public class UpdateDatabaseSampleIT extends SampleTestBase { | ||
|
||
@Test | ||
public void testUpdateDatabase() throws Exception { | ||
// Create database | ||
final String databaseId = idGenerator.generateDatabaseId(); | ||
databaseAdminClient | ||
.createDatabase(instanceId, databaseId, Collections.emptyList()) | ||
.get(5, TimeUnit.MINUTES); | ||
|
||
// Runs sample | ||
final String out = | ||
SampleRunner.runSample( | ||
() -> UpdateDatabaseSample.updateDatabase(projectId, instanceId, databaseId)); | ||
|
||
DatabaseId dbId = DatabaseId.of(projectId, instanceId, databaseId); | ||
assertTrue( | ||
"Expected that database would have been updated. Output received was " + out, | ||
out.contains(String.format("Updated database %s", dbId))); | ||
|
||
// Cleanup | ||
Database databaseToUpdate = | ||
databaseAdminClient.newDatabaseBuilder(dbId).disableDropProtection().build(); | ||
OperationFuture<Database, UpdateDatabaseMetadata> operation = | ||
databaseAdminClient.updateDatabase(databaseToUpdate, DatabaseField.DROP_PROTECTION); | ||
Database updatedDb = operation.get(5, TimeUnit.MINUTES); | ||
assertFalse(updatedDb.isDropProtectionEnabled()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.