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

java CDK: skip dropping database in PostgresTestDatabase::close #32377

Merged
merged 6 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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,39 @@ 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,9 +13,9 @@ java {
}

airbyteJavaConnector {
cdkVersionRequired = '0.4.7'
cdkVersionRequired = '0.4.8'
features = ['db-sources']
useLocalCdk = false
useLocalCdk = true
}

airbyteJavaConnector.addCdkDependencies()
Expand Down