-
Notifications
You must be signed in to change notification settings - Fork 132
docs: samples and tests for database Admin APIs. #2775
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 39 commits into
googleapis:main
from
arpan14:admin-database-apis
Feb 15, 2024
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
edc5bbf
fix: prevent illegal negative timeout values into thread sleep() meth…
arpan14 49a85df
Merge pull request #1 from arpan14/retryerror
arpan14 4cd497b
Fixing lint issues.
arpan14 4a6aa8e
Merge branch 'googleapis:main' into main
arpan14 b2aa09d
Merge branch 'googleapis:main' into main
arpan14 8d6d71e
Merge branch 'googleapis:main' into main
arpan14 77e6e7d
Merge branch 'googleapis:main' into main
arpan14 e8b7fad
Merge branch 'googleapis:main' into main
arpan14 8aa84e1
Merge branch 'googleapis:main' into main
arpan14 57fd405
Merge branch 'googleapis:main' into main
arpan14 1253563
Merge branch 'googleapis:main' into main
arpan14 d4f6a60
Merge branch 'googleapis:main' into main
arpan14 3efaf7c
Merge branch 'googleapis:main' into main
arpan14 f41b39f
Merge branch 'googleapis:main' into main
arpan14 7e3287f
Merge branch 'googleapis:main' into main
arpan14 2862112
chore: adding a few samples with auto-gen clients.
arpan14 313b6ee
chore: adding integration tests for samples.
arpan14 e9d0556
chore: fixing the end-point for staging.
arpan14 2416cd6
chore: modified test for CreateDatabaseWithDefaultLeaderSample.
arpan14 87cbb89
chore: adding sample and integration test for CreateInstanceSample.
arpan14 9feb1ab
chore: adding license headers.
arpan14 596fb26
chore: fix lint errors.
arpan14 d9a5ae0
chore: rename file and add sample tags.
arpan14 56dcdb2
chore: address comments.
arpan14 c7f998b
Update samples/snippets/src/main/java/com/example/spanner/v2/CreateDa…
arpan14 cd70008
chore: rename file path.
arpan14 b8f2000
chore: remove admin settings.
arpan14 3f7309e
chore: address comments.
arpan14 f3d4f66
chore: copy relevant apis.
arpan14 57aef57
chore: updating integration tests.
arpan14 ee79d74
chore: rewrite database admin APIs and tests.
arpan14 fe5b0a8
chore: fix broken integration tests.
arpan14 bad0fc0
chore: fix CreateDatabaseWithVersionRetentionPeriodSampleIT.
arpan14 259bb39
chore: fix lint errors.
arpan14 deb7bb6
chore: rebase fixes.
arpan14 3d24ef7
chore: fix broken intergration test for AddAndDropDatabaseRole.
arpan14 e27f2cc
chore: fix lint error.
arpan14 cbc6875
chore: add comment around jsonb
arpan14 24321b9
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
75 changes: 75 additions & 0 deletions
75
...es/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.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,75 @@ | ||
/* | ||
* Copyright 2022 Google Inc. | ||
* | ||
* 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.admin.generated; | ||
|
||
// [START spanner_add_and_drop_database_role] | ||
|
||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.spanner.admin.database.v1.DatabaseName; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class AddAndDropDatabaseRole { | ||
|
||
static void addAndDropDatabaseRole() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
String parentRole = "parent_role"; | ||
String childRole = "child_role"; | ||
addAndDropDatabaseRole(projectId, instanceId, databaseId, parentRole, childRole); | ||
} | ||
|
||
static void addAndDropDatabaseRole( | ||
String projectId, String instanceId, String databaseId, String parentRole, String childRole) | ||
throws IOException { | ||
final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); | ||
try { | ||
System.out.println("Waiting for role create operation to complete..."); | ||
databaseAdminClient.updateDatabaseDdlAsync( | ||
DatabaseName.of(projectId, instanceId, databaseId), | ||
ImmutableList.of( | ||
String.format("CREATE ROLE %s", parentRole), | ||
String.format("GRANT SELECT ON TABLE Albums TO ROLE %s", parentRole), | ||
String.format("CREATE ROLE %s", childRole), | ||
String.format("GRANT ROLE %s TO ROLE %s", parentRole, childRole))) | ||
.get(5, TimeUnit.MINUTES); | ||
System.out.printf( | ||
"Created roles %s and %s and granted privileges%n", parentRole, childRole); | ||
// Delete role and membership. | ||
System.out.println("Waiting for role revoke & drop operation to complete..."); | ||
databaseAdminClient.updateDatabaseDdlAsync( | ||
DatabaseName.of(projectId, instanceId, databaseId), | ||
ImmutableList.of( | ||
String.format("REVOKE ROLE %s FROM ROLE %s", parentRole, childRole), | ||
String.format("DROP ROLE %s", childRole))).get(5, TimeUnit.MINUTES); | ||
System.out.printf("Revoked privileges and dropped role %s%n", childRole); | ||
} catch (ExecutionException | TimeoutException e) { | ||
System.out.printf( | ||
"Error: AddAndDropDatabaseRole failed with error message %s\n", e.getMessage()); | ||
e.printStackTrace(); | ||
} catch (InterruptedException e) { | ||
System.out.println( | ||
"Error: Waiting for AddAndDropDatabaseRole operation to finish was interrupted"); | ||
} | ||
} | ||
} | ||
// [END spanner_add_and_drop_database_role] |
49 changes: 49 additions & 0 deletions
49
samples/snippets/src/main/java/com/example/spanner/admin/generated/AddJsonColumnSample.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,49 @@ | ||
/* | ||
* Copyright 2021 Google Inc. | ||
* | ||
* 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.admin.generated; | ||
|
||
// [START spanner_add_json_column] | ||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.spanner.admin.database.v1.DatabaseName; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class AddJsonColumnSample { | ||
|
||
static void addJsonColumn() throws InterruptedException, ExecutionException, IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
|
||
addJsonColumn(projectId, instanceId, databaseId); | ||
} | ||
|
||
static void addJsonColumn(String projectId, String instanceId, String databaseId) | ||
throws InterruptedException, ExecutionException, IOException { | ||
final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); | ||
|
||
// Wait for the operation to finish. | ||
// This will throw an ExecutionException if the operation fails. | ||
databaseAdminClient.updateDatabaseDdlAsync( | ||
DatabaseName.of(projectId, instanceId, databaseId), | ||
ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSON")).get(); | ||
System.out.printf("Successfully added column `VenueDetails`%n"); | ||
} | ||
} | ||
// [END spanner_add_json_column] |
50 changes: 50 additions & 0 deletions
50
samples/snippets/src/main/java/com/example/spanner/admin/generated/AddJsonbColumnSample.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,50 @@ | ||
/* | ||
* Copyright 2022 Google Inc. | ||
* | ||
* 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.admin.generated; | ||
|
||
// [START spanner_postgresql_jsonb_add_column] | ||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.spanner.admin.database.v1.DatabaseName; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class AddJsonbColumnSample { | ||
|
||
static void addJsonbColumn() throws InterruptedException, ExecutionException, IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
|
||
addJsonbColumn(projectId, instanceId, databaseId); | ||
} | ||
|
||
static void addJsonbColumn(String projectId, String instanceId, String databaseId) | ||
throws InterruptedException, ExecutionException, IOException { | ||
final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); | ||
|
||
// JSONB datatype is only supported with PostgreSQL-dialect databases. | ||
// Wait for the operation to finish. | ||
// This will throw an ExecutionException if the operation fails. | ||
databaseAdminClient.updateDatabaseDdlAsync( | ||
DatabaseName.of(projectId, instanceId, databaseId), | ||
ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSONB")).get(); | ||
olavloite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
System.out.printf("Successfully added column `VenueDetails`%n"); | ||
} | ||
} | ||
// [END spanner_postgresql_jsonb_add_column] |
50 changes: 50 additions & 0 deletions
50
...es/snippets/src/main/java/com/example/spanner/admin/generated/AddNumericColumnSample.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,50 @@ | ||
/* | ||
* Copyright 2020 Google Inc. | ||
* | ||
* 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.admin.generated; | ||
|
||
// [START spanner_add_numeric_column] | ||
|
||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.spanner.admin.database.v1.DatabaseName; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class AddNumericColumnSample { | ||
|
||
static void addNumericColumn() throws InterruptedException, ExecutionException, IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
|
||
addNumericColumn(projectId, instanceId, databaseId); | ||
} | ||
|
||
static void addNumericColumn(String projectId, String instanceId, String databaseId) | ||
throws InterruptedException, ExecutionException, IOException { | ||
final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); | ||
|
||
// Wait for the operation to finish. | ||
// This will throw an ExecutionException if the operation fails. | ||
databaseAdminClient.updateDatabaseDdlAsync( | ||
DatabaseName.of(projectId, instanceId, databaseId), | ||
ImmutableList.of("ALTER TABLE Venues ADD COLUMN Revenue NUMERIC")).get(); | ||
System.out.printf("Successfully added column `Revenue`%n"); | ||
} | ||
} | ||
// [END spanner_add_numeric_column] |
98 changes: 98 additions & 0 deletions
98
samples/snippets/src/main/java/com/example/spanner/admin/generated/AlterSequenceSample.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,98 @@ | ||
/* | ||
* 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.admin.generated; | ||
|
||
// [START spanner_alter_sequence] | ||
|
||
import com.google.cloud.spanner.DatabaseClient; | ||
import com.google.cloud.spanner.DatabaseId; | ||
import com.google.cloud.spanner.ResultSet; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerExceptionFactory; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import com.google.cloud.spanner.Statement; | ||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.spanner.admin.database.v1.DatabaseName; | ||
import java.io.IOException; | ||
import java.util.Objects; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class AlterSequenceSample { | ||
|
||
static void alterSequence() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
final String projectId = "my-project"; | ||
final String instanceId = "my-instance"; | ||
final String databaseId = "my-database"; | ||
alterSequence(projectId, instanceId, databaseId); | ||
} | ||
|
||
static void alterSequence(String projectId, String instanceId, String databaseId) | ||
throws IOException { | ||
DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); | ||
try (Spanner spanner = | ||
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
|
||
databaseAdminClient | ||
.updateDatabaseDdlAsync(DatabaseName.of(projectId, instanceId, databaseId), | ||
ImmutableList.of( | ||
"ALTER SEQUENCE Seq SET OPTIONS " | ||
+ "(skip_range_min = 1000, skip_range_max = 5000000)")) | ||
.get(5, TimeUnit.MINUTES); | ||
|
||
System.out.println( | ||
"Altered Seq sequence to skip an inclusive range between 1000 and 5000000"); | ||
|
||
final DatabaseClient dbClient = | ||
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); | ||
|
||
Long insertCount = | ||
dbClient | ||
.readWriteTransaction() | ||
.run( | ||
transaction -> { | ||
try (ResultSet rs = | ||
transaction.executeQuery( | ||
Statement.of( | ||
"INSERT INTO Customers (CustomerName) VALUES " | ||
+ "('Lea'), ('Catalina'), ('Smith') " | ||
+ "THEN RETURN CustomerId"))) { | ||
while (rs.next()) { | ||
System.out.printf( | ||
"Inserted customer record with CustomerId: %d\n", rs.getLong(0)); | ||
} | ||
return Objects.requireNonNull(rs.getStats()).getRowCountExact(); | ||
} | ||
}); | ||
System.out.printf("Number of customer records inserted is: %d\n", insertCount); | ||
} catch (ExecutionException 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); | ||
} catch (TimeoutException e) { | ||
// If the operation timed out propagate the timeout | ||
throw SpannerExceptionFactory.propagateTimeout(e); | ||
} | ||
} | ||
} | ||
// [END spanner_alter_sequence] |
56 changes: 56 additions & 0 deletions
56
...java/com/example/spanner/admin/generated/AlterTableWithForeignKeyDeleteCascadeSample.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,56 @@ | ||
/* | ||
* 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.admin.generated; | ||
|
||
// [START spanner_alter_table_with_foreign_key_delete_cascade] | ||
|
||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.spanner.admin.database.v1.DatabaseName; | ||
import java.io.IOException; | ||
|
||
class AlterTableWithForeignKeyDeleteCascadeSample { | ||
|
||
static void alterForeignKeyDeleteCascadeConstraint() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
|
||
alterForeignKeyDeleteCascadeConstraint(projectId, instanceId, databaseId); | ||
} | ||
|
||
static void alterForeignKeyDeleteCascadeConstraint( | ||
String projectId, String instanceId, String databaseId) throws IOException { | ||
DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); | ||
|
||
databaseAdminClient.updateDatabaseDdlAsync(DatabaseName.of(projectId, instanceId, | ||
databaseId), | ||
ImmutableList.of( | ||
"ALTER TABLE ShoppingCarts\n" | ||
+ " ADD CONSTRAINT FKShoppingCartsCustomerName\n" | ||
+ " FOREIGN KEY (CustomerName)\n" | ||
+ " REFERENCES Customers(CustomerName)\n" | ||
+ " ON DELETE CASCADE\n")); | ||
System.out.printf( | ||
String.format( | ||
"Altered ShoppingCarts table with FKShoppingCartsCustomerName\n" | ||
+ "foreign key constraint on database %s on instance %s", | ||
databaseId, instanceId)); | ||
} | ||
} | ||
// [END spanner_alter_table_with_foreign_key_delete_cascade] |
Oops, something went wrong.
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.