Skip to content

Commit

Permalink
java CDK: skip dropping database in PostgresTestDatabase::close (#32377)
Browse files Browse the repository at this point in the history
Co-authored-by: postamar <[email protected]>
  • Loading branch information
Marius Posta and postamar authored Nov 9, 2023
1 parent 3f42418 commit f604110
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.4.7
version=0.4.8
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@
import io.airbyte.cdk.db.factory.DatabaseDriver;
import io.airbyte.cdk.db.jdbc.JdbcUtils;
import io.airbyte.cdk.integrations.util.HostPortResolver;
import io.airbyte.commons.io.IOs;
import io.airbyte.commons.string.Strings;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;
import org.jooq.DSLContext;
import org.jooq.SQLDialect;
import org.slf4j.Logger;
Expand All @@ -42,6 +38,8 @@
*/
public class PostgresTestDatabase implements AutoCloseable {

static private final Logger LOGGER = LoggerFactory.getLogger(PostgresTestDatabase.class);

/**
* Create a new {@link PostgresTestDatabase} instance.
*
Expand All @@ -61,32 +59,15 @@ static public PostgresTestDatabase make(String imageName, String... methods) {

private PostgresTestDatabase(PostgreSQLContainer<?> sharedContainer) {
this.container = sharedContainer;

this.suffix = Strings.addRandomSuffix("", "_", 10);
try {
this.tmpDir = Files.createTempDirectory("dir" + suffix);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
final var dir = this.tmpDir.toFile();
Runtime.getRuntime().addShutdownHook(new Thread(() -> FileUtils.deleteQuietly(dir)));

this.dbName = "db" + suffix;
this.userName = "test_user" + suffix;
this.password = "test_password" + suffix;

final Path script = this.tmpDir.resolve("create" + suffix + ".sql");
IOs.writeFile(script, String.format("""
CREATE DATABASE %s;
CREATE USER %s PASSWORD '%s';
GRANT ALL PRIVILEGES ON DATABASE %s TO %s;
ALTER USER %s WITH SUPERUSER;
""",
dbName,
userName, password,
dbName, userName,
userName));
PostgreSQLContainerHelper.runSqlScript(MountableFile.forHostPath(script), sharedContainer);
execSQL(
String.format("CREATE DATABASE %s", dbName),
String.format("CREATE USER %s PASSWORD '%s'", userName, password),
String.format("GRANT ALL PRIVILEGES ON DATABASE %s TO %s", dbName, userName),
String.format("ALTER USER %s WITH SUPERUSER", userName));

this.jdbcUrl = String.format(
DatabaseDriver.POSTGRESQL.getUrlFormatString(),
Expand All @@ -103,13 +84,10 @@ private PostgresTestDatabase(PostgreSQLContainer<?> sharedContainer) {
}

public final PostgreSQLContainer<?> container;
public final String dbName, userName, password, jdbcUrl;
public final String suffix, dbName, userName, password, jdbcUrl;
public final DSLContext dslContext;
public final Database database;

private final Path tmpDir;
private final String suffix;

/**
* Convenience method for building identifiers which are unique to this instance.
*/
Expand Down Expand Up @@ -147,17 +125,38 @@ public PostgresUtils.Certificate getCertificate() {
return new PostgresUtils.Certificate(caCert, clientCert, clientKey);
}

private void execSQL(String... stmts) {
final List<String> cmd = Stream.concat(
Stream.of("psql", "-a", "-d", container.getDatabaseName(), "-U", container.getUsername()),
Stream.of(stmts).flatMap(stmt -> Stream.of("-c", stmt)))
.toList();
try {
LOGGER.debug("executing {}", Strings.join(cmd, " "));
final var exec = container.execInContainer(cmd.toArray(new String[0]));
LOGGER.debug("exit code: {}\nstdout:\n{}\nstderr:\n{}", exec.getExitCode(), exec.getStdout(), exec.getStderr());
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

/**
* Drop the database owned by this instance.
*/
public void dropDatabase() {
execSQL(String.format("DROP DATABASE %s", dbName));
}

/**
* Close resources held by this instance. This deliberately avoids dropping the database, which is
* really expensive in Postgres. This is because a DROP DATABASE in Postgres triggers a CHECKPOINT.
* Call {@link #dropDatabase} to explicitly drop the database.
*/
@Override
public void close() {
dslContext.close();
final Path script = this.tmpDir.resolve("drop" + suffix + ".sql");
IOs.writeFile(script, String.format("""
DROP USER %s;
DROP DATABASE %s;
""",
userName,
dbName));
PostgreSQLContainerHelper.runSqlScript(MountableFile.forHostPath(script), container);
execSQL(String.format("DROP USER %s", userName));
}

static private class ContainerFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ java {
}

airbyteJavaConnector {
cdkVersionRequired = '0.4.7'
cdkVersionRequired = '0.4.8'
features = ['db-sources']
useLocalCdk = false
}
Expand Down
9 changes: 5 additions & 4 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ pluginManagement {
// Configure the gradle enterprise plugin to enable build scans. Enabling the plugin at the top of the settings file allows the build scan to record
// as much information as possible.
plugins {
id "com.gradle.enterprise" version "3.13.1"
id "com.gradle.enterprise" version "3.15.1"
id 'com.github.burrunan.s3-build-cache' version "1.5"
}

ext.isCiServer = System.getenv().containsKey("CI")
ext.isAirbyteCI = System.getenv().containsKey("RUN_IN_AIRBYTE_CI")

dependencyResolutionManagement {
// Set FAIL_ON_PROJECT_REPOS to ensure there are no more `repositories { ... }` blocks than necessary.
repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS
Expand Down Expand Up @@ -129,15 +132,13 @@ gradleEnterprise {
buildScan {
termsOfServiceUrl = "https://gradle.com/terms-of-service"
termsOfServiceAgree = "yes"
uploadInBackground = (!isCiServer && !isAirbyteCI) // Disable in CI or scan URLs may not work.
buildScanPublished { PublishedBuildScan scan ->
file("scan-journal.log") << "${new Date()} - ${scan.buildScanId} - ${scan.buildScanUri}\n"
}
}
}

ext.isCiServer = System.getenv().containsKey("CI")
ext.isAirbyteCI = System.getenv().containsKey("RUN_IN_AIRBYTE_CI")

if (isCiServer || isAirbyteCI) {
buildCache {
local {
Expand Down

0 comments on commit f604110

Please sign in to comment.