Skip to content
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

docs: samples and tests for database Admin APIs. #2775

Merged
merged 39 commits into from Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 37 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 Feb 6, 2023
49a85df
Merge pull request #1 from arpan14/retryerror
arpan14 Feb 8, 2023
4cd497b
Fixing lint issues.
arpan14 Feb 8, 2023
4a6aa8e
Merge branch 'googleapis:main' into main
arpan14 Mar 13, 2023
b2aa09d
Merge branch 'googleapis:main' into main
arpan14 Mar 15, 2023
8d6d71e
Merge branch 'googleapis:main' into main
arpan14 May 9, 2023
77e6e7d
Merge branch 'googleapis:main' into main
arpan14 Jul 17, 2023
e8b7fad
Merge branch 'googleapis:main' into main
arpan14 Jul 25, 2023
8aa84e1
Merge branch 'googleapis:main' into main
arpan14 Oct 10, 2023
57fd405
Merge branch 'googleapis:main' into main
arpan14 Oct 27, 2023
1253563
Merge branch 'googleapis:main' into main
arpan14 Nov 20, 2023
d4f6a60
Merge branch 'googleapis:main' into main
arpan14 Dec 15, 2023
3efaf7c
Merge branch 'googleapis:main' into main
arpan14 Dec 26, 2023
f41b39f
Merge branch 'googleapis:main' into main
arpan14 Jan 3, 2024
7e3287f
Merge branch 'googleapis:main' into main
arpan14 Jan 13, 2024
2862112
chore: adding a few samples with auto-gen clients.
arpan14 Nov 29, 2023
313b6ee
chore: adding integration tests for samples.
arpan14 Dec 18, 2023
e9d0556
chore: fixing the end-point for staging.
arpan14 Dec 21, 2023
2416cd6
chore: modified test for CreateDatabaseWithDefaultLeaderSample.
arpan14 Dec 21, 2023
87cbb89
chore: adding sample and integration test for CreateInstanceSample.
arpan14 Dec 21, 2023
9feb1ab
chore: adding license headers.
arpan14 Dec 21, 2023
596fb26
chore: fix lint errors.
arpan14 Dec 21, 2023
d9a5ae0
chore: rename file and add sample tags.
arpan14 Dec 23, 2023
56dcdb2
chore: address comments.
arpan14 Dec 23, 2023
c7f998b
Update samples/snippets/src/main/java/com/example/spanner/v2/CreateDa…
arpan14 Dec 23, 2023
cd70008
chore: rename file path.
arpan14 Dec 26, 2023
b8f2000
chore: remove admin settings.
arpan14 Dec 26, 2023
3f7309e
chore: address comments.
arpan14 Dec 26, 2023
f3d4f66
chore: copy relevant apis.
arpan14 Dec 30, 2023
57aef57
chore: updating integration tests.
arpan14 Dec 30, 2023
ee79d74
chore: rewrite database admin APIs and tests.
arpan14 Jan 9, 2024
fe5b0a8
chore: fix broken integration tests.
arpan14 Jan 9, 2024
bad0fc0
chore: fix CreateDatabaseWithVersionRetentionPeriodSampleIT.
arpan14 Jan 10, 2024
259bb39
chore: fix lint errors.
arpan14 Jan 10, 2024
deb7bb6
chore: rebase fixes.
arpan14 Jan 13, 2024
3d24ef7
chore: fix broken intergration test for AddAndDropDatabaseRole.
arpan14 Feb 9, 2024
e27f2cc
chore: fix lint error.
arpan14 Feb 9, 2024
cbc6875
chore: add comment around jsonb
arpan14 Feb 15, 2024
24321b9
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions samples/pom.xml
Expand Up @@ -19,6 +19,7 @@
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
<relativePath></relativePath>
</parent>

<properties>
Expand Down
@@ -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();
olavloite marked this conversation as resolved.
Show resolved Hide resolved
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]
@@ -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]
@@ -0,0 +1,49 @@
/*
* 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();

// 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]
@@ -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]
@@ -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]
@@ -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]