Skip to content

Commit

Permalink
fix: all issues with PgSpannerSample.
Browse files Browse the repository at this point in the history
  • Loading branch information
arpan14 committed Feb 16, 2024
1 parent aa8faca commit b806ef1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -206,7 +205,6 @@ static void createPostgreSqlDatabase(
// Initiate the request which returns an OperationFuture.
Database db = dbAdminClient.createDatabaseAsync(request).get();
System.out.println("Created database [" + db.getName() + "]");
createTableUsingDdl(dbAdminClient, DatabaseName.parse(db.toString()));
} catch (ExecutionException e) {
// If the operation failed during execution, expose the cause.
throw (SpannerException) e.getCause();
Expand Down Expand Up @@ -673,7 +671,7 @@ static void createTableUsingDdl(DatabaseAdminClient dbAdminClient, DatabaseName
System.out.println("Created Singers & Albums tables in database: [" + databaseName + "]");
} catch (ExecutionException e) {
// If the operation failed during execution, expose the cause.
throw (SpannerException) e.getCause();
throw SpannerExceptionFactory.asSpannerException(e);
} catch (InterruptedException e) {
// Throw when a thread is waiting, sleeping, or otherwise occupied,
// and the thread is interrupted, either before or during the activity.
Expand Down Expand Up @@ -1305,7 +1303,7 @@ static void run(
DatabaseName databaseName = DatabaseName.of(database.getInstanceId().getProject(),
database.getInstanceId().getInstance(), database.getDatabase());
switch (command) {
case "createdatabase":
case "createpgdatabase":
createPostgreSqlDatabase(dbAdminClient, database.getInstanceId().getProject(),
database.getInstanceId().getInstance(), database.getDatabase());
break;
Expand Down Expand Up @@ -1505,6 +1503,7 @@ public static void main(String[] args) throws Exception {
// [START spanner_init_client]
SpannerOptions options = SpannerOptions.newBuilder().build();
Spanner spanner = options.getService();
DatabaseAdminClient dbAdminClient = null;
try {
// [END spanner_init_client]
String command = args[0];
Expand All @@ -1521,14 +1520,19 @@ public static void main(String[] args) throws Exception {
}
// [START spanner_init_client]
DatabaseClient dbClient = spanner.getDatabaseClient(db);
DatabaseAdminClient dbAdminClient = DatabaseAdminClient.create();
dbAdminClient = DatabaseAdminClient.create();
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
// [END spanner_init_client]

// Use client here...
run(dbClient, dbAdminClient, instanceAdminClient, command, db);
// [START spanner_init_client]
} finally {
if(dbAdminClient != null) {
if(!dbAdminClient.isShutdown() || !dbAdminClient.isTerminated()) {
dbAdminClient.close();
}
}
spanner.close();
}
// [END spanner_init_client]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,15 @@ static void deleteStaleTestDatabases() {
Pattern samplePattern = getTestDbIdPattern(PgSpannerSampleIT.baseDbId);
Pattern restoredPattern = getTestDbIdPattern("restored");
for (Database db : dbClient.listDatabases(InstanceName.of(projectId, instanceId)).iterateAll()) {
DatabaseName databaseName = DatabaseName.parse(db.getName());
if (TimeUnit.HOURS.convert(now.getSeconds() - db.getCreateTime().getSeconds(),
TimeUnit.SECONDS) > 24) {
if (db.getName().length() >= DBID_LENGTH) {
if (databaseName.getDatabase().length() >= DBID_LENGTH) {
if (samplePattern.matcher(
toComparableId(
DatabaseName.of(projectId, instanceId, PgSpannerSampleIT.baseDbId).toString(),
db.getName())).matches()) {
toComparableId(PgSpannerSampleIT.baseDbId, databaseName.getDatabase())).matches()) {
dbClient.dropDatabase(db.getName());
}
if (restoredPattern.matcher(toComparableId(
DatabaseName.of(projectId, instanceId, "restored").toString(), db.getName()))
if (restoredPattern.matcher(toComparableId("restored", databaseName.getDatabase()))
.matches()) {
dbClient.dropDatabase(db.getName());
}
Expand Down

0 comments on commit b806ef1

Please sign in to comment.