Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@ If you are using Maven without BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies:

```Groovy
implementation platform('com.google.cloud:libraries-bom:26.1.0')
implementation platform('com.google.cloud:libraries-bom:26.1.1')

implementation 'com.google.cloud:google-cloud-spanner'
```
If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-spanner:6.28.0'
implementation 'com.google.cloud:google-cloud-spanner:6.29.0'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.28.0"
libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.29.0"
```

## Authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import com.google.cloud.spanner.DatabaseAdminClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.Instance;
import com.google.cloud.spanner.InstanceAdminClient;
import com.google.cloud.spanner.InstanceConfigId;
import com.google.cloud.spanner.InstanceId;
import com.google.cloud.spanner.InstanceInfo;
import com.google.cloud.spanner.Options;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.SpannerExceptionFactory;
Expand Down Expand Up @@ -68,6 +70,8 @@ public class SpannerSampleIT {
private static final String encryptedDatabaseId = formatForTest(baseDbId);
private static final String encryptedBackupId = formatForTest(baseDbId);
private static final String encryptedRestoreId = formatForTest(baseDbId);
private static final long STALE_INSTANCE_THRESHOLD_SECS =
TimeUnit.SECONDS.convert(24L, TimeUnit.HOURS);
static Spanner spanner;
static DatabaseId dbId;
static DatabaseAdminClient dbClient;
Expand All @@ -93,10 +97,41 @@ public static void setUp() throws Exception {
dbId = DatabaseId.of(options.getProjectId(), instanceId, databaseId);
// Delete stale test databases that have been created earlier by this test, but not deleted.
deleteStaleTestDatabases(instanceId, baseDbId);
key = String.format("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s",
options.getProjectId(), keyLocation, keyRing, keyName);
key =
String.format(
"projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s",
options.getProjectId(), keyLocation, keyRing, keyName);

/*
* Delete stale instances that have been created earlier by this test but not deleted.
* Backups needed to be deleted from the instance first, as the instance can only be
* deleted once all backups have been deleted.
* */
deleteStaleEncryptedTestInstances();
}


/**
* Deleting all the test instances with name starting with 'encrypted-test-' and were created
* before 24 hours.
*
* @throws InterruptedException If Thread.sleep() interrupted
*/
private static void deleteStaleEncryptedTestInstances() throws InterruptedException {
Timestamp now = Timestamp.now();

for (Instance instance :
spanner
.getInstanceAdminClient()
.listInstances(Options.filter("name:encrypted-test-"))
.iterateAll()) {
if ((now.getSeconds() - instance.getCreateTime().getSeconds())
> STALE_INSTANCE_THRESHOLD_SECS) {
deleteAllBackups(instanceId);
instance.delete();
}
}
}

static void deleteStaleTestDatabases(String instanceId, String baseDbId) {
Timestamp now = Timestamp.now();
Pattern samplePattern = getTestDbIdPattern(baseDbId);
Expand Down Expand Up @@ -410,7 +445,7 @@ public void testSample() throws Exception {
out = runSample("deletebackup");
assertThat(out).contains("Deleted backup [" + backupId + "]");
}

@Test
public void testEncryptedDatabaseAndBackupSamples() throws Exception {
String projectId = spanner.getOptions().getProjectId();
Expand Down Expand Up @@ -459,7 +494,7 @@ public void testEncryptedDatabaseAndBackupSamples() throws Exception {
}
}

private void deleteAllBackups(String instanceId) throws InterruptedException {
private static void deleteAllBackups(String instanceId) throws InterruptedException {
for (Backup backup : dbClient.listBackups(instanceId).iterateAll()) {
int attempts = 0;
while (attempts < 30) {
Expand Down Expand Up @@ -513,7 +548,7 @@ public void testCreateInstanceSample() {
private static int countOccurrences(String input, String search) {
return input.split(search).length - 1;
}

private static String toComparableId(String baseId, String existingId) {
String zeroUuid = "00000000-0000-0000-0000-0000-00000000";
int shouldBeLength = (baseId + "-" + zeroUuid).length();
Expand All @@ -526,7 +561,7 @@ private static Pattern getTestDbIdPattern(String baseDbId) {
baseDbId + "-[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{8}",
Pattern.CASE_INSENSITIVE);
}

static String formatForTest(String name) {
return name + "-" + UUID.randomUUID().toString().substring(0, DBID_LENGTH);
}
Expand Down