Skip to content

Commit

Permalink
build: drop stale test databases
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed May 27, 2024
1 parent a305a6b commit 7ca1cb3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ env:
GOOGLE_CLOUD_INSTANCE: "pgadapter-testing"
GOOGLE_CLOUD_DATABASE: "testdb_integration"
GOOGLE_CLOUD_ENDPOINT: "spanner.googleapis.com"
PG_ADAPTER_DROP_STALE_DATABASES: true
jobs:
check-env:
outputs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -58,6 +60,7 @@
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -67,6 +70,7 @@ public class PgAdapterTestEnv {
private static final int INIT_PROTOCOL = 80877103;
private static final int OPTIONS_PROTOCOL = 196608;
private static final String CHANNEL_PROVIDER_PROPERTY = "CHANNEL_PROVIDER";
private static final AtomicBoolean DELETED_STALE_DATABASES = new AtomicBoolean();

// GCP credentials file should be set through the 'GOOGLE_APPLICATION_CREDENTIALS' environment
// variable.
Expand Down Expand Up @@ -189,6 +193,26 @@ public void setUp() {
spannerHost = getSpannerUrl();
logger.info("Using Spanner host: " + spannerHost);
options = createSpannerOptions();
deleteStaleTestDatabases();
}

private void deleteStaleTestDatabases() {
if (isDropStaleTestDatabases() && DELETED_STALE_DATABASES.compareAndSet(false, true)) {
DatabaseAdminClient client = getSpanner().getDatabaseAdminClient();
long thresholdTime = Instant.now().getEpochSecond() - Duration.ofHours(6L).getSeconds();
for (Database database : client.listDatabases(getInstanceId()).iterateAll()) {
if ((database.getId().getDatabase().startsWith("testdb_")
|| database.getId().getDatabase().startsWith(getDatabaseId()))
&& database.getCreateTime().getSeconds() >= thresholdTime) {
logger.info("Dropping stale test database " + database.getId().getName());
database.drop();
}
}
}
}

private boolean isDropStaleTestDatabases() {
return Boolean.parseBoolean(System.getenv("PG_ADAPTER_DROP_STALE_DATABASES"));
}

public void startPGAdapterServer(Iterable<String> additionalPGAdapterOptions) {
Expand Down

0 comments on commit 7ca1cb3

Please sign in to comment.